Revive http service for serving assets. Streamlined flagset.

This commit is contained in:
Benedikt Kulmann
2020-04-21 22:43:22 +02:00
parent 4b612965d9
commit 2f42fcb35a
9 changed files with 148 additions and 194 deletions

View File

File diff suppressed because one or more lines are too long

View File

@@ -10,8 +10,8 @@ compression:
custom:
- files:
- "../../ui/"
base: "../../ui/"
- "../../assets/"
base: "../../assets/"
prefix: ""
...

View File

@@ -2,7 +2,6 @@ package command
import (
"context"
"github.com/owncloud/ocis-settings/pkg/server/grpc"
"os"
"os/signal"
"strings"
@@ -18,6 +17,8 @@ import (
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-settings/pkg/flagset"
"github.com/owncloud/ocis-settings/pkg/server/debug"
"github.com/owncloud/ocis-settings/pkg/server/grpc"
"github.com/owncloud/ocis-settings/pkg/server/http"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
)
@@ -39,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 {
@@ -132,14 +132,31 @@ func Server(cfg *config.Config) *cli.Command {
defer cancel()
{
server := http.Server(
http.Name("settings"),
http.Logger(logger),
http.Context(ctx),
http.Config(cfg),
)
gr.Add(func() error {
return server.Run()
}, func(_ error) {
logger.Info().
Str("server", "http").
Msg("Shutting down server")
cancel()
})
}
{
server := grpc.Server(
grpc.Name("ocis-settings"),
grpc.Address(":9190"),
grpc.Name("settings"),
grpc.Logger(logger),
grpc.Context(ctx),
grpc.Config(cfg),
grpc.Namespace(httpNamespace),
)
gr.Add(func() error {

View File

@@ -22,6 +22,12 @@ type HTTP struct {
Root string
}
// GRPC defines the available grpc configuration.
type GRPC struct {
Addr string
Namespace string
}
// Tracing defines the available tracing configuration.
type Tracing struct {
Enabled bool
@@ -48,6 +54,7 @@ type Config struct {
Log Log
Debug Debug
HTTP HTTP
GRPC GRPC
Tracing Tracing
Asset Asset
}

View File

@@ -136,6 +136,20 @@ func ServerWithConfig(cfg *config.Config) []cli.Flag {
EnvVars: []string{"SETTINGS_HTTP_ROOT"},
Destination: &cfg.HTTP.Root,
},
&cli.StringFlag{
Name: "grpc-addr",
Value: "0.0.0.0:9191",
Usage: "Address to bind grpc server",
EnvVars: []string{"HELLO_GRPC_ADDR"},
Destination: &cfg.GRPC.Addr,
},
&cli.StringFlag{
Name: "grpc-namespace",
Value: "com.owncloud.api",
Usage: "Set the base namespace for the grpc namespace",
EnvVars: []string{"SETTINGS_GRPC_NAMESPACE"},
Destination: &cfg.GRPC.Namespace,
},
&cli.StringFlag{
Name: "mount-path",
Usage: "Mount path for the storage",

View File

@@ -3,6 +3,8 @@ package grpc
import (
"context"
"github.com/micro/cli/v2"
"github.com/owncloud/ocis-settings/pkg/metrics"
"github.com/owncloud/ocis-settings/pkg/config"
"github.com/owncloud/ocis-pkg/v2/log"
)
@@ -13,11 +15,11 @@ type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Name string
Address string
Logger log.Logger
Context context.Context
Config *config.Config
Namespace string
Metrics *metrics.Metrics
Flags []cli.Flag
}
// newOptions initializes the available default options.
@@ -45,13 +47,6 @@ func Name(val string) Option {
}
}
// Address provides an address for the service.
func Address(val string) Option {
return func(o *Options) {
o.Address = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
@@ -66,9 +61,16 @@ func Config(val *config.Config) Option {
}
}
// Namespace provides a function to set the namespace option.
func Namespace(val string) Option {
// Metrics provides a function to set the metrics option.
func Metrics(val *metrics.Metrics) Option {
return func(o *Options) {
o.Namespace = val
o.Metrics = val
}
}
// Flags provides a function to set the flags option.
func Flags(val []cli.Flag) Option {
return func(o *Options) {
o.Flags = append(o.Flags, val...)
}
}

View File

@@ -4,6 +4,7 @@ import (
"github.com/owncloud/ocis-settings/pkg/proto/v0"
svc "github.com/owncloud/ocis-settings/pkg/service/v0"
"github.com/owncloud/ocis-pkg/v2/service/grpc"
"github.com/owncloud/ocis-settings/pkg/version"
)
// NewService initializes a new go-micro service ready to run
@@ -11,15 +12,17 @@ func Server(opts ...Option) grpc.Service {
options := newOptions(opts...)
service := grpc.NewService(
grpc.Name(options.Name),
grpc.Context(options.Context),
grpc.Address(options.Address),
grpc.Namespace(options.Namespace),
grpc.Logger(options.Logger),
grpc.Name(options.Name),
grpc.Version(version.String),
grpc.Address(options.Config.GRPC.Addr),
grpc.Namespace(options.Config.GRPC.Namespace),
grpc.Context(options.Context),
grpc.Flags(options.Flags...),
)
hdlr := svc.NewService(options.Config)
if err := proto.RegisterBundleServiceHandler(service.Server(), hdlr); err != nil {
handle := svc.NewService(options.Config)
if err := proto.RegisterBundleServiceHandler(service.Server(), handle); err != nil {
options.Logger.Fatal().Err(err).Msg("could not register service handler")
}

View File

@@ -14,7 +14,7 @@ type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Namespace string
Name string
Logger log.Logger
Context context.Context
Config *config.Config
@@ -40,6 +40,13 @@ func Logger(val log.Logger) Option {
}
}
// Name provides a name for the service.
func Name(val string) Option {
return func(o *Options) {
o.Name = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
@@ -67,10 +74,3 @@ func Flags(val []cli.Flag) Option {
o.Flags = append(o.Flags, val...)
}
}
// Namespace provides a function to set the Namespace option.
func Namespace(val string) Option {
return func(o *Options) {
o.Namespace = val
}
}

View File

@@ -1,56 +1,74 @@
package http
// import (
// svc "github.com/owncloud/ocis-settings/pkg/service/v0"
// "github.com/owncloud/ocis-settings/pkg/version"
// "github.com/owncloud/ocis-pkg/v2/middleware"
// "github.com/owncloud/ocis-pkg/v2/service/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-settings/pkg/assets"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
svc "github.com/owncloud/ocis-settings/pkg/service/v0"
"github.com/owncloud/ocis-settings/pkg/version"
)
// // Server initializes the http service and server.
// func Server(opts ...Option) (http.Service, error) {
// options := newOptions(opts...)
// Server initializes the http service and server.
func Server(opts ...Option) http.Service {
options := newOptions(opts...)
// service := http.NewService(
// http.Logger(options.Logger),
// http.Name("settings"),
// http.Version(version.String),
// http.Namespace(options.Config.HTTP.Namespace),
// http.Address(options.Config.HTTP.Addr),
// http.Context(options.Context),
// http.Flags(options.Flags...),
// )
service := http.NewService(
http.Logger(options.Logger),
http.Name(options.Name),
http.Version(version.String),
http.Address(options.Config.HTTP.Addr),
http.Namespace(options.Config.HTTP.Namespace),
http.Context(options.Context),
http.Flags(options.Flags...),
)
// handle := svc.NewService(
// svc.Logger(options.Logger),
// svc.Config(options.Config),
// svc.Middleware(
// middleware.RealIP,
// middleware.RequestID,
// middleware.Cache,
// middleware.Cors,
// middleware.Secure,
// middleware.Version(
// "settings",
// version.String,
// ),
// middleware.Logger(
// options.Logger,
// ),
// ),
// )
handle := svc.NewService(options.Config)
// {
// handle = svc.NewInstrument(handle, options.Metrics)
// handle = svc.NewLogging(handle, options.Logger)
// handle = svc.NewTracing(handle)
// }
{
handle = svc.NewInstrument(handle, options.Metrics)
handle = svc.NewLogging(handle, options.Logger)
handle = svc.NewTracing(handle)
}
// service.Handle(
// "/",
// handle,
// )
mux := chi.NewMux()
// service.Init()
// return service, nil
// }
mux.Use(middleware.RealIP)
mux.Use(middleware.RequestID)
mux.Use(middleware.Cache)
mux.Use(middleware.Cors)
mux.Use(middleware.Secure)
mux.Use(middleware.Version(
"settings",
version.String,
))
mux.Use(middleware.Logger(
options.Logger,
))
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) {
proto.RegisterBundleServiceWeb(
r,
handle,
)
})
service.Handle(
"/",
mux,
)
service.Init()
return service
}