remove NewStaticContentHandlerFunc

This commit is contained in:
ajatprabha 2021-10-05 11:39:11 +05:30 committed by Ken Hibino
parent cb4ccea025
commit 0cbec9318f
6 changed files with 31 additions and 45 deletions

View File

@ -64,6 +64,8 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/gorilla/mux"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
"github.com/hibiken/asynqmon" "github.com/hibiken/asynqmon"
) )
@ -74,16 +76,16 @@ var staticContents embed.FS
func main() { func main() {
h := asynqmon.New(asynqmon.Options{ h := asynqmon.New(asynqmon.Options{
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
StaticContentHandler: asynqmon.NewStaticContentHandler(
staticContents,
"ui-assets",
"index.html",
),
}) })
defer h.Close() 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{ srv := &http.Server{
Handler: h, Handler: r,
Addr: ":8080", Addr: ":8080",
} }

View File

@ -5,6 +5,7 @@ import (
"embed" "embed"
"flag" "flag"
"fmt" "fmt"
"github.com/gorilla/mux"
"log" "log"
"net/http" "net/http"
"strings" "strings"
@ -107,21 +108,25 @@ func main() {
h := asynqmon.New(asynqmon.Options{ h := asynqmon.New(asynqmon.Options{
RedisConnOpt: redisConnOpt, RedisConnOpt: redisConnOpt,
Middlewares: []asynqmon.MiddlewareFunc{loggingMiddleware},
StaticContentHandler: asynqmon.NewStaticContentHandler(
staticContents,
"ui-assets",
"index.html",
),
}) })
defer h.Close() 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{ c := cors.New(cors.Options{
AllowedMethods: []string{"GET", "POST", "DELETE"}, AllowedMethods: []string{"GET", "POST", "DELETE"},
}) })
srv := &http.Server{ srv := &http.Server{
Handler: c.Handler(h), Handler: c.Handler(r),
Addr: fmt.Sprintf(":%d", flagPort), Addr: fmt.Sprintf(":%d", flagPort),
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,

View File

@ -1,4 +1,4 @@
package asynqmon package main
import ( import (
"embed" "embed"
@ -8,15 +8,6 @@ import (
"path/filepath" "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 // staticFileServer implements the http.Handler interface, so we can use it
// to respond to HTTP requests. The path to the static directory and // to respond to HTTP requests. The path to the static directory and
// path to the index file within that static directory are used to // path to the index file within that static directory are used to

View File

@ -14,7 +14,7 @@ import (
// - conversion function from an external type to an internal type // - 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 { type PayloadFormatter interface {
FormatPayload(taskType string, payload []byte) string FormatPayload(taskType string, payload []byte) string
} }

View File

@ -1,30 +1,28 @@
package asynqmon_test package asynqmon_test
import ( import (
"embed"
"log" "log"
"net/http" "net/http"
"github.com/gorilla/mux"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
"github.com/hibiken/asynqmon" "github.com/hibiken/asynqmon"
) )
//go:embed ui-assets/*
var staticContents embed.FS
func ExampleNew() { func ExampleNew() {
h := asynqmon.New(asynqmon.Options{ h := asynqmon.New(asynqmon.Options{
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
StaticContentHandler: asynqmon.NewStaticContentHandler(
staticContents,
"ui-assets",
"index.html",
),
}) })
defer h.Close() 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{ srv := &http.Server{
Handler: h, Handler: r,
Addr: ":8080", Addr: ":8080",
} }

View File

@ -10,15 +10,10 @@ import (
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
) )
// MiddlewareFunc helps chain http.Handler(s).
type MiddlewareFunc func(http.Handler) http.Handler
// Options can be used to customise HTTPHandler. // Options can be used to customise HTTPHandler.
type Options struct { type Options struct {
RedisConnOpt asynq.RedisConnOpt RedisConnOpt asynq.RedisConnOpt
Middlewares []MiddlewareFunc
PayloadFormatter PayloadFormatter PayloadFormatter PayloadFormatter
StaticContentHandler http.Handler
} }
// HTTPHandler can serve the API and UI required for asynq monitoring. // 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) 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 { func New(opts Options) *HTTPHandler {
rc, ok := opts.RedisConnOpt.MakeRedisClient().(redis.UniversalClient) rc, ok := opts.RedisConnOpt.MakeRedisClient().(redis.UniversalClient)
if !ok { if !ok {
@ -61,10 +56,6 @@ func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspecto
pf = opts.PayloadFormatter pf = opts.PayloadFormatter
} }
for _, mf := range opts.Middlewares {
router.Use(mux.MiddlewareFunc(mf))
}
api := router.PathPrefix("/api").Subrouter() api := router.PathPrefix("/api").Subrouter()
// Queue endpoints. // Queue endpoints.
api.HandleFunc("/queues", newListQueuesHandlerFunc(inspector)).Methods("GET") 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") api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(c)).Methods("GET")
} }
router.PathPrefix("/").Handler(opts.StaticContentHandler)
return router return router
} }