Skip to content

Commit

Permalink
reorganize the moroz service (#8)
Browse files Browse the repository at this point in the history
Did a lot of cleaning up and reorganized code to follow a simple 1 file per endpoint model.
Each method of the santa.Service now corresponds to a svc_*.go file in the moroz package.
The implementation of the service method lives at the top, followed by boilerplate to create a go-kit endpoint and the
necessary http transport decode function. Finally any service middleware is written below.

The middleware.go file is for declaring middleware on santa.Service.

The server.go file is for registering HTTP routes with gorilla/mux.
  • Loading branch information
groob committed Feb 2, 2018
1 parent f797400 commit 2f82816
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 367 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@ cmd/*/*.key
cmd/*/*/*.toml
cmd/*/build/*
cmd/moroz/moroz
server.crt
server.key
35 changes: 25 additions & 10 deletions cmd/moroz/main.go
Expand Up @@ -9,9 +9,12 @@ import (
"net/http/httputil"
"os"

"github.com/groob/moroz/santa"

stdlog "log"

"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/groob/moroz/moroz"
"github.com/groob/moroz/santaconfig"
"github.com/micromdm/go4/version"
Expand Down Expand Up @@ -71,24 +74,36 @@ func main() {
os.Exit(2)
}

repo := santaconfig.NewFileRepo(*flConfigs)
svc, err := moroz.NewService(repo, *flEvents)
if err != nil {
stdlog.Fatal(err)
}
logger := log.NewLogfmtLogger(os.Stderr)
h := moroz.MakeAPIHandler(svc, logger)
if *flHTTPDebug {
h = debugHTTPmiddleware(h)

repo := santaconfig.NewFileRepo(*flConfigs)
var svc santa.Service
{
s, err := moroz.NewService(repo, *flEvents)
if err != nil {
stdlog.Fatal(err)
}
svc = s
svc = moroz.LoggingMiddleware(logger)(svc)
}

http.Handle("/v1/santa/", h)
endpoints := moroz.MakeServerEndpoints(svc)

var h http.Handler
{
r := mux.NewRouter()
h = r
moroz.AddHTTPRoutes(r, endpoints, logger)
if *flHTTPDebug {
h = debugHTTPmiddleware(h)
}
}

go func() { fmt.Println("started server") }()
stdlog.Fatal(http.ListenAndServeTLS(*flAddr,
*flTLSCert,
*flTLSKey,
nil))
h))
}

func validateConfigExists(configsPath string) bool {
Expand Down
48 changes: 0 additions & 48 deletions moroz/endpoint.go

This file was deleted.

31 changes: 0 additions & 31 deletions moroz/eventupload_endpoint.go

This file was deleted.

20 changes: 0 additions & 20 deletions moroz/eventupload_transport.go

This file was deleted.

63 changes: 0 additions & 63 deletions moroz/handler.go

This file was deleted.

19 changes: 19 additions & 0 deletions moroz/middleware.go
@@ -0,0 +1,19 @@
package moroz

import (
"github.com/go-kit/kit/log"
"github.com/groob/moroz/santa"
)

type Middleware func(santa.Service) santa.Service

func LoggingMiddleware(logger log.Logger) Middleware {
return func(next santa.Service) santa.Service {
return logmw{logger, next}
}
}

type logmw struct {
logger log.Logger
next santa.Service
}
31 changes: 0 additions & 31 deletions moroz/preflight_endpoint.go

This file was deleted.

41 changes: 0 additions & 41 deletions moroz/preflight_transport.go

This file was deleted.

30 changes: 0 additions & 30 deletions moroz/rule_endpoint.go

This file was deleted.

16 changes: 0 additions & 16 deletions moroz/rule_transport.go

This file was deleted.

0 comments on commit 2f82816

Please sign in to comment.