searcherside/handlers/api/v1/routes.go
Peter Kurfer 9ea9a8f658
Some checks failed
Go build / build (push) Failing after 1m58s
feat: continue basic setup
- setup ent scheme
- add command to create users
- document API
- add helpers to create migrations
- add command to run migrations
- add basic compose file
2024-06-19 21:19:37 +02:00

42 lines
1.2 KiB
Go

package v1
import (
"net/http"
"code.icb4dc0.de/prskr/searcherside/handlers/graphql"
"code.icb4dc0.de/prskr/searcherside/internal/ent"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/go-chi/jwtauth/v5"
"github.com/lestrrat-go/jwx/v2/jwa"
)
func Mount(
r chi.Router,
authSecret []byte,
indexHandler IndexHandler,
searchHandler SearchHandler,
dbClient *ent.Client,
) {
graphQlServer := handler.NewDefaultServer(graphql.NewSchema(dbClient))
r.Mount("/graphql", graphQlServer)
r.Mount("/graphql/playground", playground.Handler("SearcherSide", "/api/v1/graphql"))
r.Group(func(r chi.Router) {
jwtAuth := jwtauth.New(jwa.HS256.String(), authSecret, nil)
r.Use(jwtauth.Verify(jwtAuth, jwtauth.TokenFromHeader), jwtauth.Authenticator(jwtAuth))
r.Put("/index/{module}/{instance}", indexHandler.IngestIndex)
})
// Routes requiring CORS
r.Group(func(r chi.Router) {
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{http.MethodGet},
}))
r.Get("/preview-search/{module}/{instance}", searchHandler.PreviewSearch)
})
}