2023-02-09 12:16:36 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2023-04-02 17:59:33 +02:00
|
|
|
"net/http"
|
2023-02-09 12:16:36 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus/memory"
|
2023-04-18 17:57:16 +02:00
|
|
|
"github.com/go-chi/chi/v5"
|
2023-02-09 12:16:36 +01:00
|
|
|
"github.com/igm/sockjs-go/v3/sockjs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HandlerOptions struct {
|
|
|
|
Bus bus.Bus
|
|
|
|
SockJS sockjs.Options
|
|
|
|
ServerModuleFactories []app.ServerModuleFactory
|
2023-04-02 17:59:33 +02:00
|
|
|
HTTPClient *http.Client
|
2023-04-18 17:57:16 +02:00
|
|
|
HTTPMounts []func(r chi.Router)
|
2023-09-20 17:23:53 +02:00
|
|
|
HTTPMiddlewares []func(next http.Handler) http.Handler
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultHandlerOptions() *HandlerOptions {
|
|
|
|
sockjsOptions := func() sockjs.Options {
|
|
|
|
return sockjs.DefaultOptions
|
|
|
|
}()
|
|
|
|
sockjsOptions.DisconnectDelay = 10 * time.Second
|
|
|
|
|
|
|
|
return &HandlerOptions{
|
|
|
|
Bus: memory.NewBus(),
|
|
|
|
SockJS: sockjsOptions,
|
|
|
|
ServerModuleFactories: make([]app.ServerModuleFactory, 0),
|
2023-04-02 17:59:33 +02:00
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: time.Second * 30,
|
|
|
|
},
|
2023-09-20 17:23:53 +02:00
|
|
|
HTTPMounts: make([]func(r chi.Router), 0),
|
|
|
|
HTTPMiddlewares: make([]func(http.Handler) http.Handler, 0),
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HandlerOptionFunc func(*HandlerOptions)
|
|
|
|
|
|
|
|
func WithServerModules(factories ...app.ServerModuleFactory) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.ServerModuleFactories = factories
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSockJS(options sockjs.Options) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.SockJS = options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBus(bus bus.Bus) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.Bus = bus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-02 17:59:33 +02:00
|
|
|
func WithHTTPClient(client *http.Client) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.HTTPClient = client
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 17:57:16 +02:00
|
|
|
|
|
|
|
func WithHTTPMounts(mounts ...func(r chi.Router)) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.HTTPMounts = mounts
|
|
|
|
}
|
|
|
|
}
|
2023-09-20 17:23:53 +02:00
|
|
|
|
|
|
|
func WithHTTPMiddlewares(middlewares ...func(http.Handler) http.Handler) HandlerOptionFunc {
|
|
|
|
return func(opts *HandlerOptions) {
|
|
|
|
opts.HTTPMiddlewares = middlewares
|
|
|
|
}
|
|
|
|
}
|