rename API => HTTPHandler

This commit is contained in:
ajatprabha 2021-10-04 00:00:40 +05:30 committed by Ken Hibino
parent bb2f7788f6
commit 4b54ec1548
6 changed files with 15 additions and 15 deletions

View File

@ -72,7 +72,7 @@ import (
var staticContents embed.FS var staticContents embed.FS
func main() { func main() {
api := asynqmon.NewAPI(asynqmon.APIOptions{ api := asynqmon.NewHTTPHandler(asynqmon.Options{
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
StaticContentHandler: asynqmon.NewStaticContentHandler( StaticContentHandler: asynqmon.NewStaticContentHandler(
staticContents, staticContents,

16
api.go
View File

@ -13,32 +13,32 @@ import (
// MiddlewareFunc helps chain http.Handler(s). // MiddlewareFunc helps chain http.Handler(s).
type MiddlewareFunc func(http.Handler) http.Handler type MiddlewareFunc func(http.Handler) http.Handler
type APIOptions struct { type Options struct {
RedisConnOpt asynq.RedisConnOpt RedisConnOpt asynq.RedisConnOpt
Middlewares []MiddlewareFunc Middlewares []MiddlewareFunc
PayloadFormatter PayloadFormatter PayloadFormatter PayloadFormatter
StaticContentHandler http.Handler StaticContentHandler http.Handler
} }
type API struct { type HTTPHandler struct {
router *mux.Router router *mux.Router
closers []func() error closers []func() error
} }
func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (a *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.router.ServeHTTP(w, r) a.router.ServeHTTP(w, r)
} }
func NewAPI(opts APIOptions) *API { func NewHTTPHandler(opts Options) *HTTPHandler {
rc, ok := opts.RedisConnOpt.MakeRedisClient().(redis.UniversalClient) rc, ok := opts.RedisConnOpt.MakeRedisClient().(redis.UniversalClient)
if !ok { if !ok {
panic(fmt.Sprintf("asnyqmon.API: unsupported RedisConnOpt type %T", opts.RedisConnOpt)) panic(fmt.Sprintf("asnyqmon.HTTPHandler: unsupported RedisConnOpt type %T", opts.RedisConnOpt))
} }
i := asynq.NewInspector(opts.RedisConnOpt) i := asynq.NewInspector(opts.RedisConnOpt)
return &API{router: muxRouter(opts, rc, i), closers: []func() error{rc.Close, i.Close}} return &HTTPHandler{router: muxRouter(opts, rc, i), closers: []func() error{rc.Close, i.Close}}
} }
func (a *API) Close() error { func (a *HTTPHandler) Close() error {
for _, f := range a.closers { for _, f := range a.closers {
if err := f(); err != nil { if err := f(); err != nil {
return err return err
@ -48,7 +48,7 @@ func (a *API) Close() error {
return nil return nil
} }
func muxRouter(opts APIOptions, rc redis.UniversalClient, inspector *asynq.Inspector) *mux.Router { func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspector) *mux.Router {
router := mux.NewRouter() router := mux.NewRouter()
var pf PayloadFormatter = defaultPayloadFormatter var pf PayloadFormatter = defaultPayloadFormatter

View File

@ -105,7 +105,7 @@ func main() {
} }
} }
api := asynqmon.NewAPI(asynqmon.APIOptions{ api := asynqmon.NewHTTPHandler(asynqmon.Options{
RedisConnOpt: redisConnOpt, RedisConnOpt: redisConnOpt,
Middlewares: []asynqmon.MiddlewareFunc{loggingMiddleware}, Middlewares: []asynqmon.MiddlewareFunc{loggingMiddleware},
StaticContentHandler: asynqmon.NewStaticContentHandler( StaticContentHandler: asynqmon.NewStaticContentHandler(

View File

@ -12,8 +12,8 @@ import (
//go:embed ui-assets/* //go:embed ui-assets/*
var staticContents embed.FS var staticContents embed.FS
func ExampleNewAPI() { func ExampleNewHTTPHandler() {
api := asynqmon.NewAPI(asynqmon.APIOptions{ api := asynqmon.NewHTTPHandler(asynqmon.Options{
RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"}, RedisConnOpt: asynq.RedisClientOpt{Addr: ":6379"},
StaticContentHandler: asynqmon.NewStaticContentHandler( StaticContentHandler: asynqmon.NewStaticContentHandler(
staticContents, staticContents,

2
go.mod
View File

@ -5,6 +5,6 @@ go 1.16
require ( require (
github.com/go-redis/redis/v8 v8.11.3 github.com/go-redis/redis/v8 v8.11.3
github.com/gorilla/mux v1.8.0 github.com/gorilla/mux v1.8.0
github.com/hibiken/asynq v0.18.6-0.20210902125602-b3ef9e91a9ce github.com/hibiken/asynq v0.18.6
github.com/rs/cors v1.7.0 github.com/rs/cors v1.7.0
) )

4
go.sum
View File

@ -44,8 +44,8 @@ github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/hibiken/asynq v0.18.6-0.20210902125602-b3ef9e91a9ce h1:pIvLWFWEkd6HcNQ3yVNb71hNcLBiwSFepxqJHOroNnk= github.com/hibiken/asynq v0.18.6 h1:pBjtGh2QhDe1+/0yaSc56ANpdQ77BQgVfMIrj+NJrUM=
github.com/hibiken/asynq v0.18.6-0.20210902125602-b3ef9e91a9ce/go.mod h1:tyc63ojaW8SJ5SBm8mvI4DDONsguP5HE85EEl4Qr5Ig= github.com/hibiken/asynq v0.18.6/go.mod h1:tyc63ojaW8SJ5SBm8mvI4DDONsguP5HE85EEl4Qr5Ig=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=