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.

49 lines
1015 B

  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. "github.com/99designs/gqlgen/handler"
  13. )
  14. func main() {
  15. err := store.Init()
  16. if err != nil {
  17. log.Fatalln("Failed to init store:", err)
  18. }
  19. http.Handle("/", handler.Playground("RPData API", "/graphql"))
  20. http.Handle("/graphql", queryHandler())
  21. log.Fatal(http.ListenAndServe(":8081", nil))
  22. }
  23. func queryHandler() http.HandlerFunc {
  24. handler := handler.GraphQL(
  25. graph2.New(),
  26. handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
  27. // send this panic somewhere
  28. log.Println(err)
  29. log.Println(string(debug.Stack()))
  30. return fmt.Errorf("shit")
  31. }),
  32. )
  33. return func(w http.ResponseWriter, r *http.Request) {
  34. l := loader.New()
  35. r = r.WithContext(l.ToContext(r.Context()))
  36. r = auth.RequestWithToken(r)
  37. handler.ServeHTTP(w, r)
  38. }
  39. }