Basic service boilerplate for settings bundles

This commit is contained in:
Benedikt Kulmann
2020-04-16 10:07:45 +02:00
parent a8af03c338
commit 5b89729d1e
18 changed files with 1006 additions and 141 deletions
+1 -16
View File
@@ -1,30 +1,15 @@
package svc
import (
"net/http"
"github.com/owncloud/ocis-settings/pkg/metrics"
)
// NewInstrument returns a service that instruments metrics.
func NewInstrument(next Service, metrics *metrics.Metrics) Service {
return instrument{
next: next,
metrics: metrics,
}
return Service{}
}
type instrument struct {
next Service
metrics *metrics.Metrics
}
// ServeHTTP implements the Service interface.
func (i instrument) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i.next.ServeHTTP(w, r)
}
// Dummy implements the Service interface.
func (i instrument) Dummy(w http.ResponseWriter, r *http.Request) {
i.next.Dummy(w, r)
}
+1 -16
View File
@@ -1,30 +1,15 @@
package svc
import (
"net/http"
"github.com/owncloud/ocis-pkg/v2/log"
)
// NewLogging returns a service that logs messages.
func NewLogging(next Service, logger log.Logger) Service {
return logging{
next: next,
logger: logger,
}
return Service{}
}
type logging struct {
next Service
logger log.Logger
}
// ServeHTTP implements the Service interface.
func (l logging) ServeHTTP(w http.ResponseWriter, r *http.Request) {
l.next.ServeHTTP(w, r)
}
// Dummy implements the Service interface.
func (l logging) Dummy(w http.ResponseWriter, r *http.Request) {
l.next.Dummy(w, r)
}
+39 -33
View File
@@ -1,52 +1,58 @@
package svc
import (
"net/http"
"context"
"github.com/owncloud/ocis-settings/pkg/settings"
store "github.com/owncloud/ocis-settings/pkg/store/filesystem"
"github.com/go-chi/chi"
"github.com/owncloud/ocis-settings/pkg/proto/v0"
"github.com/owncloud/ocis-settings/pkg/config"
)
// Service defines the extension handlers.
type Service interface {
ServeHTTP(http.ResponseWriter, *http.Request)
Dummy(http.ResponseWriter, *http.Request)
type Service struct {
config *config.Config
manager settings.Manager
}
// NewService returns a service implementation for Service.
func NewService(opts ...Option) Service {
options := newOptions(opts...)
m := chi.NewMux()
m.Use(options.Middleware...)
svc := Settings{
config: options.Config,
mux: m,
func NewService(cfg *config.Config) Service {
return Service{
config:cfg,
manager:store.New(cfg),
}
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/", svc.Dummy)
})
return svc
}
// Settings defines implements the business logic for Service.
type Settings struct {
config *config.Config
mux *chi.Mux
func (g Service) CreateSettingsBundle(c context.Context, req *proto.CreateSettingsBundleRequest, res *proto.CreateSettingsBundleResponse) error {
r, err := g.manager.Write(req.SettingsBundle)
if err != nil {
return err
}
res.SettingsBundle = r
return nil
}
// ServeHTTP implements the Service interface.
func (g Settings) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
func (g Service) GetSettingsBundle(c context.Context, req *proto.GetSettingsBundleRequest, res *proto.GetSettingsBundleResponse) error {
r, err := g.manager.Read(req.Extension, req.Key)
if err != nil {
return err
}
res.SettingsBundle = r
return nil
}
// Dummy implements the Service interface.
func (g Settings) Dummy(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
func (g Service) ListSettingsBundles(c context.Context, req *proto.ListSettingsBundlesRequest, res *proto.ListSettingsBundlesResponse) error {
r, err := listSettingsBundles(g, req.Extension)
if err != nil {
return err
}
res.SettingsBundles = r
return nil
}
w.Write([]byte("Hello ocis-settings!"))
func listSettingsBundles(g Service, extension string) ([]*proto.SettingsBundle, error) {
if len(extension) == 0 {
return g.manager.List()
} else {
return g.manager.ListByExtension(extension)
}
}
+1 -17
View File
@@ -1,26 +1,10 @@
package svc
import (
"net/http"
)
// NewTracing returns a service that instruments traces.
func NewTracing(next Service) Service {
return tracing{
next: next,
}
return Service{}
}
type tracing struct {
next Service
}
// ServeHTTP implements the Service interface.
func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.next.ServeHTTP(w, r)
}
// Dummy implements the Service interface.
func (t tracing) Dummy(w http.ResponseWriter, r *http.Request) {
t.next.Dummy(w, r)
}