diff --git a/README.md b/README.md index e291e4c..76825a4 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ import ( "log" "net/http" + "github.com/gorilla/mux" + "github.com/hibiken/asynq" "github.com/hibiken/asynqmon" ) @@ -74,16 +76,16 @@ var staticContents embed.FS func main() { h := asynqmon.New(asynqmon.Options{ RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, - StaticContentHandler: asynqmon.NewStaticContentHandler( - staticContents, - "ui-assets", - "index.html", - ), }) defer h.Close() + r := mux.NewRouter() + r.PathPrefix("/api").Handler(h) + // Add static content handler or other handlers + // r.PathPrefix("/").Handler( /* &staticContentHandler{staticContents} */ ) + srv := &http.Server{ - Handler: h, + Handler: r, Addr: ":8080", } diff --git a/cmd/asynqmon/main.go b/cmd/asynqmon/main.go index 67ff457..de80caf 100644 --- a/cmd/asynqmon/main.go +++ b/cmd/asynqmon/main.go @@ -5,6 +5,7 @@ import ( "embed" "flag" "fmt" + "github.com/gorilla/mux" "log" "net/http" "strings" @@ -107,21 +108,25 @@ func main() { h := asynqmon.New(asynqmon.Options{ RedisConnOpt: redisConnOpt, - Middlewares: []asynqmon.MiddlewareFunc{loggingMiddleware}, - StaticContentHandler: asynqmon.NewStaticContentHandler( - staticContents, - "ui-assets", - "index.html", - ), }) defer h.Close() + r := mux.NewRouter() + r.PathPrefix("/api").Handler(h) + r.PathPrefix("/").Handler(&staticContentHandler{ + contents: staticContents, + staticDirPath: "ui-assets", + indexFileName: "index.html", + }) + + r.Use(loggingMiddleware) + c := cors.New(cors.Options{ AllowedMethods: []string{"GET", "POST", "DELETE"}, }) srv := &http.Server{ - Handler: c.Handler(h), + Handler: c.Handler(r), Addr: fmt.Sprintf(":%d", flagPort), WriteTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second, diff --git a/static.go b/cmd/asynqmon/static.go similarity index 82% rename from static.go rename to cmd/asynqmon/static.go index 8590557..3947e06 100644 --- a/static.go +++ b/cmd/asynqmon/static.go @@ -1,4 +1,4 @@ -package asynqmon +package main import ( "embed" @@ -8,15 +8,6 @@ import ( "path/filepath" ) -// NewStaticContentHandler creates a http.Handler which can be used to serve static files. -func NewStaticContentHandler(contents embed.FS, staticDirPath, indexFileName string) http.Handler { - return &staticContentHandler{ - contents: contents, - staticDirPath: staticDirPath, - indexFileName: indexFileName, - } -} - // staticFileServer implements the http.Handler interface, so we can use it // to respond to HTTP requests. The path to the static directory and // path to the index file within that static directory are used to diff --git a/conversion_helpers.go b/conversion_helpers.go index 0abd71c..1113a72 100644 --- a/conversion_helpers.go +++ b/conversion_helpers.go @@ -14,7 +14,7 @@ import ( // - conversion function from an external type to an internal type // **************************************************************************** -// PayloadFormatter can be used to convert payload bytes to string to show in web ui. +// PayloadFormatter can be used to convert payload bytes to string to show in web UI. type PayloadFormatter interface { FormatPayload(taskType string, payload []byte) string } diff --git a/example_test.go b/example_test.go index 7f49545..9bfa99b 100644 --- a/example_test.go +++ b/example_test.go @@ -1,30 +1,28 @@ package asynqmon_test import ( - "embed" "log" "net/http" + "github.com/gorilla/mux" + "github.com/hibiken/asynq" "github.com/hibiken/asynqmon" ) -//go:embed ui-assets/* -var staticContents embed.FS - func ExampleNew() { h := asynqmon.New(asynqmon.Options{ RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, - StaticContentHandler: asynqmon.NewStaticContentHandler( - staticContents, - "ui-assets", - "index.html", - ), }) defer h.Close() + r := mux.NewRouter() + r.PathPrefix("/api").Handler(h) + // Add static content handler or other handlers + // r.PathPrefix("/").Handler(h) + srv := &http.Server{ - Handler: h, + Handler: r, Addr: ":8080", } diff --git a/handler.go b/handler.go index 4aa5d97..e7ceb0f 100644 --- a/handler.go +++ b/handler.go @@ -10,15 +10,10 @@ import ( "github.com/hibiken/asynq" ) -// MiddlewareFunc helps chain http.Handler(s). -type MiddlewareFunc func(http.Handler) http.Handler - // Options can be used to customise HTTPHandler. type Options struct { RedisConnOpt asynq.RedisConnOpt - Middlewares []MiddlewareFunc PayloadFormatter PayloadFormatter - StaticContentHandler http.Handler } // HTTPHandler can serve the API and UI required for asynq monitoring. @@ -32,7 +27,7 @@ func (a *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { a.router.ServeHTTP(w, r) } -// New creates an HTTPHandler that can be used to serve asynqmon web API with static contents. +// New creates an HTTPHandler that can be used to serve asynqmon web API, it is prefixed with `/api`. func New(opts Options) *HTTPHandler { rc, ok := opts.RedisConnOpt.MakeRedisClient().(redis.UniversalClient) if !ok { @@ -61,10 +56,6 @@ func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspecto pf = opts.PayloadFormatter } - for _, mf := range opts.Middlewares { - router.Use(mux.MiddlewareFunc(mf)) - } - api := router.PathPrefix("/api").Subrouter() // Queue endpoints. api.HandleFunc("/queues", newListQueuesHandlerFunc(inspector)).Methods("GET") @@ -137,6 +128,5 @@ func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspecto api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(c)).Methods("GET") } - router.PathPrefix("/").Handler(opts.StaticContentHandler) return router }