86 lines
2.1 KiB
Go
86 lines
2.1 KiB
Go
package http
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
"forge.cadoles.com/arcad/edge/pkg/bus"
|
|
"forge.cadoles.com/arcad/edge/pkg/bus/memory"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/igm/sockjs-go/v3/sockjs"
|
|
)
|
|
|
|
type HandlerOptions struct {
|
|
Bus bus.Bus
|
|
SockJS sockjs.Options
|
|
ServerModuleFactories []app.ServerModuleFactory
|
|
UploadMaxFileSize int64
|
|
HTTPClient *http.Client
|
|
HTTPMounts []func(r chi.Router)
|
|
HTTPMiddlewares []func(next http.Handler) http.Handler
|
|
}
|
|
|
|
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),
|
|
UploadMaxFileSize: 10 << (10 * 2), // 10Mb
|
|
HTTPClient: &http.Client{
|
|
Timeout: time.Second * 30,
|
|
},
|
|
HTTPMounts: make([]func(r chi.Router), 0),
|
|
HTTPMiddlewares: make([]func(http.Handler) http.Handler, 0),
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
func WithUploadMaxFileSize(size int64) HandlerOptionFunc {
|
|
return func(opts *HandlerOptions) {
|
|
opts.UploadMaxFileSize = size
|
|
}
|
|
}
|
|
|
|
func WithHTTPClient(client *http.Client) HandlerOptionFunc {
|
|
return func(opts *HandlerOptions) {
|
|
opts.HTTPClient = client
|
|
}
|
|
}
|
|
|
|
func WithHTTPMounts(mounts ...func(r chi.Router)) HandlerOptionFunc {
|
|
return func(opts *HandlerOptions) {
|
|
opts.HTTPMounts = mounts
|
|
}
|
|
}
|
|
|
|
func WithHTTPMiddlewares(middlewares ...func(http.Handler) http.Handler) HandlerOptionFunc {
|
|
return func(opts *HandlerOptions) {
|
|
opts.HTTPMiddlewares = middlewares
|
|
}
|
|
}
|