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.

109 lines
2.9 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/instrumentation"
  12. "git.aiterp.net/rpdata/api/internal/loader"
  13. "git.aiterp.net/rpdata/api/internal/store"
  14. "git.aiterp.net/rpdata/api/models"
  15. "git.aiterp.net/rpdata/api/models/changes"
  16. "git.aiterp.net/rpdata/api/models/logs"
  17. "github.com/99designs/gqlgen/handler"
  18. "github.com/prometheus/client_golang/prometheus/promhttp"
  19. )
  20. func main() {
  21. err := store.Init()
  22. if err != nil {
  23. log.Fatalln("Failed to init store:", err)
  24. }
  25. instrumentation.Register()
  26. http.Handle("/", handler.Playground("RPData API", "/graphql"))
  27. http.Handle("/graphql", queryHandler())
  28. http.Handle("/metrics", promhttp.Handler())
  29. go func() {
  30. err := logs.RunFullUpdate()
  31. if err != nil {
  32. log.Println(err)
  33. }
  34. log.Println("Characters updated")
  35. }()
  36. go logListedChanges()
  37. log.Fatal(http.ListenAndServe(":8081", nil))
  38. }
  39. func logListedChanges() {
  40. sub := changes.Subscribe(context.Background(), nil, true)
  41. for change := range sub {
  42. log.Printf("Change: Author=%#+v Model=%#+v Op=%#+v", change.Author, change.Model, change.Op)
  43. for _, object := range change.Objects() {
  44. switch object := object.(type) {
  45. case models.Log:
  46. log.Printf("\tLog: %s (%s)", object.ID, object.ShortID)
  47. case models.Character:
  48. log.Printf("\tCharacter: %s", object.ID)
  49. case models.Channel:
  50. log.Printf("\tChannel: %s", object.Name)
  51. case models.Post:
  52. log.Printf("\tPost: %s", object.ID)
  53. case models.Story:
  54. log.Printf("\tStory: %s", object.ID)
  55. case models.Tag:
  56. log.Printf("\tTag: (%s,%s)", object.Kind, object.Name)
  57. case models.Chapter:
  58. log.Printf("\tChapter: %s", object.ID)
  59. case models.Comment:
  60. log.Printf("\tComment: %s", object.ID)
  61. }
  62. }
  63. }
  64. log.Println("Change subscription closed.")
  65. }
  66. func queryHandler() http.HandlerFunc {
  67. handler := handler.GraphQL(
  68. graph2.New(),
  69. handler.RecoverFunc(func(ctx context.Context, err interface{}) error {
  70. log.Println(err)
  71. log.Println(string(debug.Stack()))
  72. return fmt.Errorf("The server failed to serve your request due to an internal error, it has been logged")
  73. }),
  74. handler.ComplexityLimit(300),
  75. handler.RequestMiddleware(instrumentation.RequestMiddleware()),
  76. handler.ResolverMiddleware(instrumentation.ResolverMiddleware()),
  77. )
  78. return func(w http.ResponseWriter, r *http.Request) {
  79. l := loader.New()
  80. r = r.WithContext(l.ToContext(r.Context()))
  81. // >_>
  82. if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer of the curse") {
  83. w.Header().Set("X-Emerald-Herald", "Seek souls. Larger, more powerful souls.")
  84. w.Header().Add("X-Emerald-Herald", "Seek the king, that is the only way.")
  85. w.Header().Add("X-Emerald-Herald", "Lest this land swallow you whole... As it has so many others.")
  86. }
  87. r = auth.RequestWithToken(r)
  88. handler.ServeHTTP(w, r)
  89. }
  90. }