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.

115 lines
3.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/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/store"
  15. "git.aiterp.net/rpdata/api/models"
  16. "git.aiterp.net/rpdata/api/services"
  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. db, err := database.Init(config.Global().Database)
  26. if err != nil {
  27. log.Fatalln("Failed to init db:", err)
  28. }
  29. defer db.Close(context.Background())
  30. serviceBundle := services.NewBundle(db)
  31. instrumentation.Register()
  32. http.Handle("/", handler.Playground("RPData API", "/graphql"))
  33. http.Handle("/graphql", queryHandler(serviceBundle))
  34. http.Handle("/metrics", promhttp.Handler())
  35. go func() {
  36. err := serviceBundle.Logs.RefreshAllLogCharacters(context.Background())
  37. if err != nil {
  38. log.Println("Refresh Characters:", err)
  39. }
  40. err = serviceBundle.Logs.FixImportDateBug(context.Background())
  41. if err != nil {
  42. log.Println("Fix date bug:", err)
  43. }
  44. }()
  45. go logListedChanges(serviceBundle.Changes)
  46. log.Fatal(http.ListenAndServe(":8081", nil))
  47. }
  48. func logListedChanges(changes *services.ChangeService) {
  49. sub := changes.Subscribe(context.Background(), models.ChangeFilter{})
  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. // >_>
  89. if strings.HasPrefix(r.Header.Get("Authorization"), "Bearer of the curse") {
  90. w.Header().Set("X-Emerald-Herald", "Seek souls. Larger, more powerful souls.")
  91. w.Header().Add("X-Emerald-Herald", "Seek the king, that is the only way.")
  92. w.Header().Add("X-Emerald-Herald", "Lest this land swallow you whole... As it has so many others.")
  93. }
  94. r = auth.RequestWithToken(r)
  95. handler.ServeHTTP(w, r)
  96. }
  97. }