diff --git a/auth/flags.go b/auth/flags.go index 57d1bf448..6cb6f4018 100644 --- a/auth/flags.go +++ b/auth/flags.go @@ -2,56 +2,58 @@ package auth import ( "github.com/pkg/errors" + + "github.com/crusttech/crust/config" ) type ( - configuration struct { - http *httpFlags - db *dbFlags - jwt *jwtFlags + appFlags struct { + http *config.HTTP + db *config.Database + jwt *config.JWT } ) -var config *configuration +var flags *appFlags -func (c *configuration) validate() error { +func (c *appFlags) Validate() error { if c == nil { - return errors.New("CRM config is not initialized, need to call Flags() or FullFlags()") + return errors.New("AUTH flags are not initialized, need to call Flags() or FullFlags()") } - if err := c.http.validate(); err != nil { + if err := c.http.Validate(); err != nil { return err } - if err := c.db.validate(); err != nil { + if err := c.db.Validate(); err != nil { return err } - if err := c.jwt.validate(); err != nil { + if err := c.jwt.Validate(); err != nil { return err } return nil } func Flags(prefix ...string) { - if config != nil { + if flags != nil { return } if len(prefix) == 0 { - panic("crm.Flags() needs prefix on first call") + panic("auth.Flags() needs prefix on first call") } - config = &configuration{ - jwt: new(jwtFlags).flags(prefix...), + flags = &appFlags{ + jwt: new(config.JWT).Init(prefix...), } } func FullFlags(prefix ...string) { - if config != nil { + if flags != nil { return } if len(prefix) == 0 { - panic("crm.Flags() needs prefix on first call") + panic("auth.Flags() needs prefix on first call") } - config = &configuration{ - new(httpFlags).flags(prefix...), - new(dbFlags).flags(prefix...), - new(jwtFlags).flags(prefix...), + flags = &appFlags{ + new(config.HTTP).Init(prefix...), + new(config.Database).Init(prefix...), + new(config.JWT).Init(prefix...), } } diff --git a/auth/flags_db.go b/auth/flags_db.go deleted file mode 100644 index c26bad4df..000000000 --- a/auth/flags_db.go +++ /dev/null @@ -1,34 +0,0 @@ -package auth - -import ( - "github.com/namsral/flag" - "github.com/pkg/errors" -) - -type ( - dbFlags struct { - dsn string - profiler string - } -) - -func (c *dbFlags) validate() error { - if c == nil { - return nil - } - if c.dsn == "" { - return errors.New("No DB DSN is set, can't connect to database") - } - return nil -} - -func (c *dbFlags) flags(prefix ...string) *dbFlags { - p := func(s string) string { - return prefix[0] + "-" + s - } - - flag.StringVar(&c.dsn, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection") - flag.StringVar(&c.profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)") - - return c -} diff --git a/auth/flags_http.go b/auth/flags_http.go deleted file mode 100644 index f4f908918..000000000 --- a/auth/flags_http.go +++ /dev/null @@ -1,40 +0,0 @@ -package auth - -import ( - "github.com/namsral/flag" - "github.com/pkg/errors" -) - -type ( - httpFlags struct { - addr string - logging bool - pretty bool - tracing bool - metrics bool - } -) - -func (c *httpFlags) validate() error { - if c == nil { - return nil - } - if c.addr == "" { - return errors.New("No HTTP Addr is set, can't listen for HTTP") - } - return nil -} - -func (c *httpFlags) flags(prefix ...string) *httpFlags { - p := func(s string) string { - return prefix[0] + "-" + s - } - - flag.StringVar(&c.addr, p("http-addr"), ":3000", "Listen address for HTTP server") - flag.BoolVar(&c.logging, p("http-log"), true, "Enable/disable HTTP request log") - flag.BoolVar(&c.pretty, p("http-pretty-json"), false, "Prettify returned JSON output") - flag.BoolVar(&c.tracing, p("http-error-tracing"), false, "Return error stack frame") - flag.BoolVar(&c.metrics, p("http-metrics"), false, "Provide metrics export for prometheus") - - return c -} diff --git a/auth/flags_jwt.go b/auth/flags_jwt.go deleted file mode 100644 index bf526ebf4..000000000 --- a/auth/flags_jwt.go +++ /dev/null @@ -1,31 +0,0 @@ -package auth - -import ( - "github.com/namsral/flag" - "github.com/pkg/errors" -) - -type ( - jwtFlags struct { - secret string - expiry int64 - debugToken bool - } -) - -func (c *jwtFlags) validate() error { - if c == nil { - return nil - } - if c.secret == "" { - return errors.New("JWT Secret not set for AUTH") - } - return nil -} - -func (c *jwtFlags) flags(prefix ...string) *jwtFlags { - flag.StringVar(&c.secret, "auth-jwt-secret", "", "JWT Secret") - flag.Int64Var(&c.expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes") - flag.BoolVar(&c.debugToken, "auth-jwt-debug", false, "Generate debug JWT key") - return c -} diff --git a/auth/jwt.go b/auth/jwt.go index 9ea2d44af..c52cad7a3 100644 --- a/auth/jwt.go +++ b/auth/jwt.go @@ -16,13 +16,13 @@ type jwt struct { } func JWT() (*jwt, error) { - if err := config.validate(); err != nil { + if err := flags.Validate(); err != nil { return nil, err } - jwt := &jwt{tokenAuth: jwtauth.New("HS256", []byte(config.jwt.secret), nil)} + jwt := &jwt{tokenAuth: jwtauth.New("HS256", []byte(flags.jwt.Secret), nil)} - if config.jwt.debugToken { + if flags.jwt.DebugToken { log.Println("DEBUG JWT TOKEN:", jwt.Encode(NewIdentity(1))) } @@ -38,7 +38,7 @@ func (t *jwt) Encode(identity types.Identifiable) string { // @todo Set expiry claims := jwtauth.Claims{} claims.Set("sub", strconv.FormatUint(identity.Identity(), 10)) - claims.SetExpiryIn(time.Duration(config.jwt.expiry) * time.Minute) + claims.SetExpiryIn(time.Duration(flags.jwt.Expiry) * time.Minute) _, jwt, _ := t.tokenAuth.Encode(claims) return jwt diff --git a/auth/routes.go b/auth/routes.go index 7ae92ae51..6a2c95e3e 100644 --- a/auth/routes.go +++ b/auth/routes.go @@ -7,13 +7,15 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + + "github.com/crusttech/crust/config" ) -func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router)) { - if opts.http.logging { +func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router)) { + if opts.Logging { r.Use(middleware.Logger) } - if opts.http.metrics { + if opts.Metrics { r.Use(metrics{}.Middleware("crm")) } @@ -22,14 +24,14 @@ func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router) } } -func mountSystemRoutes(r chi.Router, opts *configuration) { - if opts.http.metrics { +func mountSystemRoutes(r chi.Router, opts *config.HTTP) { + if opts.Metrics { r.Handle("/metrics", metrics{}.Handler()) } r.Mount("/debug", middleware.Profiler()) } -func printRoutes(r chi.Router, opts *configuration) { +func printRoutes(r chi.Router, opts *config.HTTP) { var printRoutes func(chi.Routes, string, string) printRoutes = func(r chi.Routes, indent string, prefix string) { routes := r.Routes() diff --git a/auth/start.go b/auth/start.go index 0b8ea7adb..efad96f20 100644 --- a/auth/start.go +++ b/auth/start.go @@ -20,18 +20,18 @@ import ( func Init() error { // validate configuration - if err := config.validate(); err != nil { + if err := flags.Validate(); err != nil { return err } // start/configure database connection - factory.Database.Add("default", config.db.dsn) + factory.Database.Add("default", flags.db.DSN) db, err := factory.Database.Get() if err != nil { return err } // @todo: profiling as an external service? - switch config.db.profiler { + switch flags.db.Profiler { case "stdout": db.Profiler = &factory.Database.ProfilerStdout default: @@ -40,8 +40,8 @@ func Init() error { // configure resputil options resputil.SetConfig(resputil.Options{ - Pretty: config.http.pretty, - Trace: config.http.tracing, + Pretty: flags.http.Pretty, + Trace: flags.http.Tracing, Logger: func(err error) { // @todo: error logging }, @@ -53,10 +53,10 @@ func Init() error { func Start() error { var ctx = sigctx.New() - log.Println("Starting http server on address " + config.http.addr) - listener, err := net.Listen("tcp", config.http.addr) + log.Println("Starting http server on address " + flags.http.Addr) + listener, err := net.Listen("tcp", flags.http.Addr) if err != nil { - return errors.Wrap(err, fmt.Sprintf("Can't listen on addr %s", config.http.addr)) + return errors.Wrap(err, fmt.Sprintf("Can't listen on addr %s", flags.http.Addr)) } // JWT Auth @@ -71,11 +71,11 @@ func Start() error { // Only protect application routes with JWT r.Group(func(r chi.Router) { r.Use(jwtAuth.Verifier(), jwtAuth.Authenticator()) - mountRoutes(r, config, rest.MountRoutes(jwtAuth)) + mountRoutes(r, flags.http, rest.MountRoutes(jwtAuth)) }) - printRoutes(r, config) - mountSystemRoutes(r, config) + printRoutes(r, flags.http) + mountSystemRoutes(r, flags.http) go http.Serve(listener, r) <-ctx.Done()