Restructure based on hello reference

This commit is contained in:
Thomas Boerger
2019-12-06 13:18:26 +01:00
parent ac202d5458
commit dfa78aff7c
53 changed files with 774 additions and 3041 deletions
+19 -5
View File
@@ -4,8 +4,20 @@ import (
"context"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-pkg/log"
)
// Option defines a single option function.
type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Config *config.Config
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
@@ -16,19 +28,21 @@ func newOptions(opts ...Option) Options {
return opt
}
type Option func(o *Options)
type Options struct {
Context context.Context
Config *config.Config
// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}
// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
+33 -58
View File
@@ -3,74 +3,49 @@ package debug
import (
"io"
"net/http"
"net/http/pprof"
"github.com/justinas/alice"
"github.com/micro/go-micro/util/log"
"github.com/owncloud/ocis-graph/pkg/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opencensus.io/zpages"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/version"
"github.com/owncloud/ocis-pkg/service/debug"
)
type Handler struct {
*http.ServeMux
}
func (h *Handler) healthz(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
}
func (h *Handler) readyz(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
}
// Server initializes the debug service and server.
func Server(opts ...Option) (*http.Server, error) {
options := newOptions(opts...)
log.Infof("Server [debug] listening on [%s]", options.Config.Debug.Addr)
mux := http.NewServeMux()
handler := &Handler{mux}
return debug.NewService(
debug.Logger(options.Logger),
debug.Name("graph"),
debug.Version(version.String),
debug.Address(options.Config.Debug.Addr),
debug.Token(options.Config.Debug.Token),
debug.Pprof(options.Config.Debug.Pprof),
debug.Zpages(options.Config.Debug.Zpages),
debug.Health(health(options.Config)),
debug.Ready(ready(options.Config)),
), nil
}
handler.Handle("/metrics", alice.New(
middleware.Token(
options.Config.Debug.Token,
),
).Then(
promhttp.Handler(),
))
// health implements the health check.
func health(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
handler.HandleFunc("/healthz", handler.healthz)
handler.HandleFunc("/readyz", handler.readyz)
// TODO(tboerger): check if services are up and running
if options.Config.Debug.Pprof {
handler.HandleFunc("/debug/pprof/", pprof.Index)
handler.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
handler.HandleFunc("/debug/pprof/profile", pprof.Profile)
handler.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
handler.HandleFunc("/debug/pprof/trace", pprof.Trace)
io.WriteString(w, http.StatusText(http.StatusOK))
}
}
if options.Config.Debug.Zpages {
zpages.Handle(mux, "/debug")
// ready implements the ready check.
func ready(cfg *config.Config) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
// TODO(tboerger): check if services are up and running
io.WriteString(w, http.StatusText(http.StatusOK))
}
return &http.Server{
Addr: options.Config.Debug.Addr,
Handler: alice.New(
middleware.RealIP,
middleware.RequestID,
middleware.Cache,
middleware.Cors,
middleware.Secure,
middleware.Version,
).Then(
handler,
),
}, nil
}
+28 -5
View File
@@ -4,8 +4,22 @@ import (
"context"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/metrics"
"github.com/owncloud/ocis-pkg/log"
)
// Option defines a single option function.
type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Context context.Context
Config *config.Config
Metrics *metrics.Metrics
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
@@ -16,21 +30,30 @@ func newOptions(opts ...Option) Options {
return opt
}
type Option func(o *Options)
type Options struct {
Context context.Context
Config *config.Config
// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}
// Context provides a function to set the context option.
func Context(val context.Context) Option {
return func(o *Options) {
o.Context = val
}
}
// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
// Metrics provides a function to set the metrics option.
func Metrics(val *metrics.Metrics) Option {
return func(o *Options) {
o.Metrics = val
}
}
+41 -105
View File
@@ -1,123 +1,59 @@
package http
import (
"encoding/json"
"net/http"
"time"
"github.com/micro/go-micro/util/log"
"github.com/micro/go-micro/web"
"github.com/owncloud/ocis-graph/pkg/config"
"github.com/owncloud/ocis-graph/pkg/flagset"
"github.com/owncloud/ocis-graph/pkg/service/v0"
"github.com/owncloud/ocis-graph/pkg/version"
msgraph "github.com/yaegashi/msgraph.go/v1.0"
ldap "gopkg.in/ldap.v3"
"github.com/owncloud/ocis-pkg/middleware"
"github.com/owncloud/ocis-pkg/service/http"
)
func createUserModel(displayName string, id string) *msgraph.User {
return &msgraph.User{
DisplayName: &displayName,
GivenName: &displayName,
DirectoryObject: msgraph.DirectoryObject{
Entity: msgraph.Entity{
ID: &id,
},
},
}
// Server initializes the http service and server.
func Server(opts ...Option) (http.Service, error) {
options := newOptions(opts...)
}
func writeResponse(v interface{}, writer http.ResponseWriter) {
js, err := json.Marshal(v)
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write(js)
}
func handleMe(writer http.ResponseWriter, req *http.Request) {
me := createUserModel("Alice", "1234-5678-9000-000")
writeResponse(me, writer)
}
func handleUsers(writer http.ResponseWriter, req *http.Request) {
con, err := ldap.Dial("tcp", "localhost:10389")
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap dail failed"))
return
}
err = con.Bind("cn=admin,dc=example,dc=org", "admin")
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap bind failed"))
return
}
// Search for the given username
searchRequest := ldap.NewSearchRequest(
"ou=groups,dc=example,dc=org",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(objectclass=*)",
[]string{"dn", "uuid", "uid", "givenName", "mail"},
nil,
service := http.NewService(
http.Logger(options.Logger),
http.Namespace("go.micro.web"),
http.Name("graph"),
http.Version(version.String),
http.Address(options.Config.HTTP.Addr),
http.Context(options.Context),
http.Flags(flagset.RootWithConfig(config.New())...),
http.Flags(flagset.ServerWithConfig(config.New())...),
)
sr, err := con.Search(searchRequest)
if err != nil {
//p.srv.Logger().Errorf("owncloud-plugin: error encoding response as json %s", err)
writer.WriteHeader(http.StatusInternalServerError)
writer.Write([]byte("ldap search failed: " + err.Error()))
return
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(
"graph",
version.String,
),
middleware.Logger(
options.Logger,
),
),
)
{
handle = svc.NewInstrument(handle, options.Metrics)
handle = svc.NewLogging(handle, options.Logger)
handle = svc.NewTracing(handle)
}
users := make([]*msgraph.User, len(sr.Entries))
for i := 0; i < len(sr.Entries); i++ {
users[i] = createUserModel(sr.Entries[i].DN, "1234-5678-9000-000")
}
/*
users := make([]*msgraph.User, 4)
users[0] = createUserModel("Alice", "1234-5678-9000-000")
users[1] = createUserModel("Bob", "1234-5678-9000-001")
users[2] = createUserModel("Carol", "1234-5678-9000-002")
users[3] = createUserModel("Dave", "1234-5678-9000-003")
*/
// TODO: the response has to hold a root element named value ...
writeResponse(users, writer)
}
func Server(opts ...Option) (web.Service, error) {
options := newOptions(opts...)
log.Infof("Server [http] listening on [%s]", options.Config.HTTP.Addr)
// &cli.StringFlag{
// Name: "http-addr",
// Value: "0.0.0.0:8380",
// Usage: "Address to bind http server",
// EnvVar: "GRAPH_HTTP_ADDR",
// Destination: &cfg.HTTP.Addr,
// },
service := web.NewService(
web.Name("go.micro.web.graph"),
web.Version(version.String),
web.RegisterTTL(time.Second*30),
web.RegisterInterval(time.Second*10),
web.Context(options.Context),
web.Flags(append(
flagset.RootWithConfig(config.New()),
flagset.ServerWithConfig(config.New())...,
)...),
service.Handle(
"/",
handle,
)
service.Init()
service.HandleFunc("/v1.0/me", handleMe)
service.HandleFunc("/v1.0/users", handleUsers)
return service, nil
}