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.

57 lines
1.3 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/graph2"
  10. "git.aiterp.net/rpdata/api/internal/auth"
  11. "git.aiterp.net/rpdata/api/internal/loader"
  12. "git.aiterp.net/rpdata/api/internal/store"
  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. log.Fatal(http.ListenAndServe(":8081", nil))
  23. }
  24. func queryHandler() http.HandlerFunc {
  25. handler := handler.GraphQL(
  26. graph2.New(),
  27. handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
  28. // send this panic somewhere
  29. log.Println(err)
  30. log.Println(string(debug.Stack()))
  31. return fmt.Errorf("shit")
  32. }),
  33. )
  34. return func(w http.ResponseWriter, r *http.Request) {
  35. l := loader.New()
  36. r = r.WithContext(l.ToContext(r.Context()))
  37. // >_>
  38. if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer of the curse") {
  39. w.Header().Set("X-Emerald-Herald", "Seek souls. Larger, more powerful souls.")
  40. w.Header().Add("X-Emerald-Herald", "Seek the king, that is the only way.")
  41. w.Header().Add("X-Emerald-Herald", "Lest this land swallow you whole... As it has so many others.")
  42. }
  43. r = auth.RequestWithToken(r)
  44. handler.ServeHTTP(w, r)
  45. }
  46. }