Refactor http server
This commit is contained in:
@@ -40,7 +40,6 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
logger := NewLogger(cfg)
|
||||
httpNamespace := c.String("http-namespace")
|
||||
|
||||
if cfg.Tracing.Enabled {
|
||||
switch t := cfg.Tracing.Type; t {
|
||||
@@ -136,8 +135,8 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
|
||||
{
|
||||
server, err := http.Server(
|
||||
http.Name("onlyoffice"),
|
||||
http.Logger(logger),
|
||||
http.Namespace(httpNamespace),
|
||||
http.Context(ctx),
|
||||
http.Config(cfg),
|
||||
http.Metrics(metrics),
|
||||
|
||||
@@ -14,12 +14,12 @@ type Option func(o *Options)
|
||||
|
||||
// Options defines the available options for this package.
|
||||
type Options struct {
|
||||
Namespace string
|
||||
Logger log.Logger
|
||||
Context context.Context
|
||||
Config *config.Config
|
||||
Metrics *metrics.Metrics
|
||||
Flags []cli.Flag
|
||||
Name string
|
||||
Logger log.Logger
|
||||
Context context.Context
|
||||
Config *config.Config
|
||||
Metrics *metrics.Metrics
|
||||
Flags []cli.Flag
|
||||
}
|
||||
|
||||
// newOptions initializes the available default options.
|
||||
@@ -68,9 +68,9 @@ func Flags(val []cli.Flag) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// Namespace provides a function to set the Namespace option.
|
||||
func Namespace(val string) Option {
|
||||
// Name provides a function to set the Name option.
|
||||
func Name(val string) Option {
|
||||
return func(o *Options) {
|
||||
o.Namespace = val
|
||||
o.Name = val
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/owncloud/ocis-pkg/v2/middleware"
|
||||
"github.com/owncloud/ocis-pkg/v2/service/http"
|
||||
"github.com/owncloud/ocis/onlyoffice/pkg/assets"
|
||||
@@ -14,8 +13,8 @@ func Server(opts ...Option) (http.Service, error) {
|
||||
options := newOptions(opts...)
|
||||
|
||||
service := http.NewService(
|
||||
http.Name(options.Name),
|
||||
http.Logger(options.Logger),
|
||||
http.Name("onlyoffice"),
|
||||
http.Version(version.String),
|
||||
http.Namespace(options.Config.HTTP.Namespace),
|
||||
http.Address(options.Config.HTTP.Addr),
|
||||
@@ -33,12 +32,19 @@ func Server(opts ...Option) (http.Service, error) {
|
||||
middleware.Cors,
|
||||
middleware.Secure,
|
||||
middleware.Version(
|
||||
"onlyoffice",
|
||||
options.Name,
|
||||
version.String,
|
||||
),
|
||||
middleware.Logger(
|
||||
options.Logger,
|
||||
),
|
||||
middleware.Static(
|
||||
options.Config.HTTP.Root,
|
||||
assets.New(
|
||||
assets.Logger(options.Logger),
|
||||
assets.Config(options.Config),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -48,21 +54,9 @@ func Server(opts ...Option) (http.Service, error) {
|
||||
handle = svc.NewTracing(handle)
|
||||
}
|
||||
|
||||
mux := chi.NewMux()
|
||||
|
||||
mux.Use(middleware.Static(
|
||||
options.Config.HTTP.Root,
|
||||
assets.New(
|
||||
assets.Logger(options.Logger),
|
||||
assets.Config(options.Config),
|
||||
),
|
||||
))
|
||||
|
||||
mux.Route(options.Config.HTTP.Root, func(r chi.Router) {})
|
||||
|
||||
service.Handle(
|
||||
"/",
|
||||
mux,
|
||||
handle,
|
||||
)
|
||||
|
||||
service.Init()
|
||||
|
||||
@@ -24,6 +24,8 @@ func NewService(opts ...Option) Service {
|
||||
mux: m,
|
||||
}
|
||||
|
||||
m.Route(options.Config.HTTP.Root, func(r chi.Router) {})
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user