GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.2 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "runtime/debug"
  8. "git.aiterp.net/rpdata/api/graph2"
  9. "git.aiterp.net/rpdata/api/internal/auth"
  10. "git.aiterp.net/rpdata/api/internal/loader"
  11. "git.aiterp.net/rpdata/api/internal/store"
  12. logModel "git.aiterp.net/rpdata/api/model/log"
  13. "github.com/99designs/gqlgen/handler"
  14. )
  15. func main() {
  16. err := store.Init()
  17. if err != nil {
  18. log.Fatalln("Failed to init store:", err)
  19. }
  20. http.Handle("/", handler.Playground("RPData API", "/graphql"))
  21. http.Handle("/graphql", queryHandler())
  22. go updateCharacters()
  23. log.Fatal(http.ListenAndServe(":8081", nil))
  24. }
  25. func queryHandler() http.HandlerFunc {
  26. handler := handler.GraphQL(
  27. graph2.New(),
  28. handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
  29. // send this panic somewhere
  30. log.Println(err)
  31. log.Println(string(debug.Stack()))
  32. return fmt.Errorf("shit")
  33. }),
  34. )
  35. return func(w http.ResponseWriter, r *http.Request) {
  36. l := loader.New()
  37. r = r.WithContext(l.ToContext(r.Context()))
  38. r = auth.RequestWithToken(r)
  39. handler.ServeHTTP(w, r)
  40. }
  41. }
  42. func updateCharacters() {
  43. n, err := logModel.UpdateAllCharacters()
  44. if err != nil {
  45. log.Println("Charcter updated stopped:", err)
  46. }
  47. log.Println("Updated characters on", n, "logs")
  48. }