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.

68 lines
1.5 KiB

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "runtime/debug"
  8. "strings"
  9. "git.aiterp.net/rpdata/api/models/logs"
  10. "git.aiterp.net/rpdata/api/graph2"
  11. "git.aiterp.net/rpdata/api/internal/auth"
  12. "git.aiterp.net/rpdata/api/internal/loader"
  13. "git.aiterp.net/rpdata/api/internal/store"
  14. "github.com/99designs/gqlgen/handler"
  15. )
  16. func main() {
  17. err := store.Init()
  18. if err != nil {
  19. log.Fatalln("Failed to init store:", err)
  20. }
  21. http.Handle("/", handler.Playground("RPData API", "/graphql"))
  22. http.Handle("/graphql", queryHandler())
  23. go func() {
  24. err := logs.RunFullUpdate()
  25. if err != nil {
  26. log.Println(err)
  27. }
  28. log.Println("Characters updated")
  29. }()
  30. log.Fatal(http.ListenAndServe(":8081", nil))
  31. }
  32. func queryHandler() http.HandlerFunc {
  33. handler := handler.GraphQL(
  34. graph2.New(),
  35. handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
  36. // send this panic somewhere
  37. log.Println(err)
  38. log.Println(string(debug.Stack()))
  39. return fmt.Errorf("shit")
  40. }),
  41. )
  42. return func(w http.ResponseWriter, r *http.Request) {
  43. l := loader.New()
  44. r = r.WithContext(l.ToContext(r.Context()))
  45. // >_>
  46. if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer of the curse") {
  47. w.Header().Set("X-Emerald-Herald", "Seek souls. Larger, more powerful souls.")
  48. w.Header().Add("X-Emerald-Herald", "Seek the king, that is the only way.")
  49. w.Header().Add("X-Emerald-Herald", "Lest this land swallow you whole... As it has so many others.")
  50. }
  51. r = auth.RequestWithToken(r)
  52. handler.ServeHTTP(w, r)
  53. }
  54. }