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"
"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",
}

View File

@ -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,

View File

@ -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

View File

@ -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
}

View File

@ -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",
}

View File

@ -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
}