upd(crm): use global config pkg
This commit is contained in:
parent
ca1f5330fd
commit
35679fc350
49
crm/flags.go
49
crm/flags.go
@ -1,60 +1,43 @@
|
||||
package crm
|
||||
|
||||
import (
|
||||
"github.com/namsral/flag"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/crusttech/crust/config"
|
||||
)
|
||||
|
||||
type (
|
||||
configuration struct {
|
||||
http struct {
|
||||
addr string
|
||||
logging bool
|
||||
pretty bool
|
||||
tracing bool
|
||||
metrics bool
|
||||
}
|
||||
db struct {
|
||||
dsn string
|
||||
profiler string
|
||||
}
|
||||
appFlags struct {
|
||||
http *config.HTTP
|
||||
db *config.Database
|
||||
}
|
||||
)
|
||||
|
||||
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()")
|
||||
return errors.New("CRM flags are not initialized, need to call Flags()")
|
||||
}
|
||||
if c.http.addr == "" {
|
||||
return errors.New("No HTTP Addr is set, can't listen for HTTP")
|
||||
if err := c.http.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.db.dsn == "" {
|
||||
return errors.New("No DB DSN is set, can't connect to database")
|
||||
if err := c.db.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")
|
||||
}
|
||||
config = new(configuration)
|
||||
|
||||
p := func(s string) string {
|
||||
return prefix[0] + "-" + s
|
||||
flags = &appFlags{
|
||||
new(config.HTTP).Init(prefix...),
|
||||
new(config.Database).Init(prefix...),
|
||||
}
|
||||
|
||||
flag.StringVar(&config.http.addr, p("http-addr"), ":3000", "Listen address for HTTP server")
|
||||
flag.BoolVar(&config.http.logging, p("http-log"), true, "Enable/disable HTTP request log")
|
||||
flag.BoolVar(&config.http.pretty, p("http-pretty-json"), false, "Prettify returned JSON output")
|
||||
flag.BoolVar(&config.http.tracing, p("http-error-tracing"), false, "Return error stack frame")
|
||||
flag.BoolVar(&config.http.metrics, p("http-metrics"), false, "Provide metrics export for prometheus")
|
||||
|
||||
flag.StringVar(&config.db.dsn, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection")
|
||||
flag.StringVar(&config.db.profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)")
|
||||
}
|
||||
|
||||
@ -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()
|
||||
|
||||
22
crm/start.go
22
crm/start.go
@ -21,18 +21,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:
|
||||
@ -41,8 +41,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
|
||||
},
|
||||
@ -54,10 +54,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
|
||||
@ -72,11 +72,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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user