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.

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