M auth/middleware.go => auth/middleware.go +1 -11
@@ 17,7 17,6 @@ import (
"sync/atomic"
"time"
- chimiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/vaughan0/go-ini"
"github.com/vektah/gqlparser/v2/gqlerror"
@@ 698,7 697,7 @@ func WebhookAuth(ctx context.Context, auth *AuthContext,
return context.WithValue(ctx, userCtxKey, &whAuth), nil
}
-func RequireMiddleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
+func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
var internalNet []*net.IPNet
src, ok := conf.Get(apiconf, "internal-ipnet")
if !ok {
@@ 764,15 763,6 @@ func RequireMiddleware(conf ini.File, apiconf string) func(http.Handler) http.Ha
}
}
-func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
- return chimiddleware.Maybe(RequireMiddleware(conf, apiconf), func(r *http.Request) bool {
- return strings.HasPrefix(r.URL.Path, "/query") &&
- r.URL.Path != "/query/metrics" &&
- r.URL.Path != "/query/api-meta.json" &&
- !strings.HasPrefix(r.URL.Path, "/query/external/")
- })
-}
-
func ForContext(ctx context.Context) *AuthContext {
raw, ok := ctx.Value(userCtxKey).(*AuthContext)
if !ok {
M server/server.go => server/server.go +22 -13
@@ 53,6 53,7 @@ type Server struct {
conf ini.File
db *sql.DB
redis *goRedis.Client
+ root chi.Router
router chi.Router
service string
queues []*work.Queue
@@ 63,9 64,11 @@ type Server struct {
// Creates a new common server context for a SourceHut GraphQL daemon.
func NewServer(service string, conf ini.File) *Server {
+ root := chi.NewRouter()
server := &Server{
conf: conf,
- router: chi.NewRouter(),
+ root: root,
+ router: root.Group(func(_ chi.Router) {}),
service: service,
}
return server
@@ 105,19 108,25 @@ func (server *Server) WithSchema(
playground.Handler("GraphQL playground", "/query"))
}
server.router.Handle("/query", srv)
- server.router.Handle("/query/metrics", promhttp.Handler())
- server.router.Get("/query/api-meta.json", func(w http.ResponseWriter, r *http.Request) {
- info := struct {
- Scopes []string `json:"scopes"`
- }{scopes}
- j, err := json.Marshal(&info)
- if err != nil {
- panic(err)
- }
+ // These don't need auth or any other middleware - just log and process
+ server.root.Group(func(r chi.Router) {
+ r.Use(middleware.RealIP)
+ r.Use(middleware.Logger)
+ r.Handle("/query/metrics", promhttp.Handler())
+ r.Get("/query/api-meta.json", func(w http.ResponseWriter, r *http.Request) {
+ info := struct {
+ Scopes []string `json:"scopes"`
+ }{scopes}
+
+ j, err := json.Marshal(&info)
+ if err != nil {
+ panic(err)
+ }
- w.Header().Add("Content-Type", "application/json")
- w.Write(j)
+ w.Header().Add("Content-Type", "application/json")
+ w.Write(j)
+ })
})
return server
}
@@ 263,7 272,7 @@ func (server *Server) Run() {
panic(err)
}
log.Printf("Running on %s", config.Addr)
- qserver := &http.Server{Handler: server.router}
+ qserver := &http.Server{Handler: server.root}
go qserver.Serve(qlisten)
mux := &http.ServeMux{}