fix(all):
- config fixes for singleton values (most) - add monitor into internal/metrics - clean up cmd folders to remove monitor - implement versions on all apps
This commit is contained in:
parent
259e7bac4d
commit
adeefdbeaa
@ -8,10 +8,11 @@ import (
|
||||
|
||||
type (
|
||||
appFlags struct {
|
||||
http *config.HTTP
|
||||
db *config.Database
|
||||
//jwt *config.JWT
|
||||
oidc *config.OIDC
|
||||
http *config.HTTP
|
||||
monitor *config.Monitor
|
||||
db *config.Database
|
||||
oidc *config.OIDC
|
||||
//jwt *config.JWT
|
||||
}
|
||||
)
|
||||
|
||||
@ -24,15 +25,18 @@ func (c *appFlags) Validate() error {
|
||||
if err := c.http.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.monitor.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.db.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.oidc.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := c.jwt.Validate(); err != nil {
|
||||
// return err
|
||||
//}
|
||||
if err := c.oidc.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -45,8 +49,9 @@ func Flags(prefix ...string) {
|
||||
}
|
||||
flags = &appFlags{
|
||||
new(config.HTTP).Init(prefix...),
|
||||
new(config.Monitor).Init(prefix...),
|
||||
new(config.Database).Init(prefix...),
|
||||
//new(config.JWT).Init(prefix...),
|
||||
new(config.OIDC).Init(prefix...),
|
||||
//new(config.JWT).Init(prefix...),
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,7 +16,10 @@ import (
|
||||
migrate "github.com/crusttech/crust/auth/db"
|
||||
"github.com/crusttech/crust/auth/rest"
|
||||
"github.com/crusttech/crust/auth/service"
|
||||
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Init() error {
|
||||
@ -62,6 +65,7 @@ func Init() error {
|
||||
func Start() error {
|
||||
var deadline = sigctx.New()
|
||||
|
||||
log.Printf("Starting auth, version: %v, built on: %v", version.Version, version.BuildTime)
|
||||
log.Println("Starting http server on address " + flags.http.Addr)
|
||||
listener, err := net.Listen("tcp", flags.http.Addr)
|
||||
if err != nil {
|
||||
@ -86,6 +90,10 @@ func Start() error {
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
|
||||
if flags.monitor.Interval > 0 {
|
||||
go metrics.NewMonitor(flags.monitor.Interval)
|
||||
}
|
||||
|
||||
go http.Serve(listener, r)
|
||||
<-deadline.Done()
|
||||
|
||||
|
||||
@ -5,19 +5,9 @@ import (
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
monitorInterval int
|
||||
}
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) configuration {
|
||||
var config configuration
|
||||
|
||||
flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)")
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) {
|
||||
for _, mount := range mountFlags {
|
||||
mount(prefix)
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
return config
|
||||
}
|
||||
|
||||
@ -11,12 +11,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := flags("auth", service.Flags, auth.Flags, rbac.Flags)
|
||||
flags("auth", service.Flags, auth.Flags, rbac.Flags)
|
||||
|
||||
// log to stdout not stderr
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
go NewMonitor(config.monitorInterval)
|
||||
|
||||
if err := service.Init(); err != nil {
|
||||
log.Fatalf("Error initializing auth: %+v", err)
|
||||
|
||||
@ -5,19 +5,9 @@ import (
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
monitorInterval int
|
||||
}
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) configuration {
|
||||
var config configuration
|
||||
|
||||
flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)")
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) {
|
||||
for _, mount := range mountFlags {
|
||||
mount(prefix)
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
return config
|
||||
}
|
||||
|
||||
@ -11,12 +11,11 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := flags("crm", service.Flags, auth.Flags, rbac.Flags)
|
||||
flags("crm", service.Flags, auth.Flags, rbac.Flags)
|
||||
|
||||
// log to stdout not stderr
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
go NewMonitor(config.monitorInterval)
|
||||
|
||||
if err := service.Init(); err != nil {
|
||||
log.Fatalf("Error initializing crm: %+v", err)
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
Alloc,
|
||||
TotalAlloc,
|
||||
Sys,
|
||||
Mallocs,
|
||||
Frees,
|
||||
LiveObjects,
|
||||
PauseTotalNs uint64
|
||||
|
||||
NumGC uint32
|
||||
NumGoroutine int
|
||||
}
|
||||
|
||||
func NewMonitor(duration int) {
|
||||
var (
|
||||
m = Monitor{}
|
||||
rtm runtime.MemStats
|
||||
goroutines = expvar.NewInt("num_goroutine")
|
||||
)
|
||||
var interval = time.Duration(duration) * time.Second
|
||||
for {
|
||||
<-time.After(interval)
|
||||
|
||||
// Read full mem stats
|
||||
runtime.ReadMemStats(&rtm)
|
||||
|
||||
// Number of goroutines
|
||||
m.NumGoroutine = runtime.NumGoroutine()
|
||||
goroutines.Set(int64(m.NumGoroutine))
|
||||
|
||||
// Misc memory stats
|
||||
m.Alloc = rtm.Alloc
|
||||
m.TotalAlloc = rtm.TotalAlloc
|
||||
m.Sys = rtm.Sys
|
||||
m.Mallocs = rtm.Mallocs
|
||||
m.Frees = rtm.Frees
|
||||
|
||||
// Live objects = Mallocs - Frees
|
||||
m.LiveObjects = m.Mallocs - m.Frees
|
||||
|
||||
// GC Stats
|
||||
m.PauseTotalNs = rtm.PauseTotalNs
|
||||
m.NumGC = rtm.NumGC
|
||||
|
||||
// Just encode to json and print
|
||||
b, _ := json.Marshal(m)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
}
|
||||
@ -5,19 +5,9 @@ import (
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
type configuration struct {
|
||||
monitorInterval int
|
||||
}
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) configuration {
|
||||
var config configuration
|
||||
|
||||
flag.IntVar(&config.monitorInterval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)")
|
||||
|
||||
func flags(prefix string, mountFlags ...func(...string)) {
|
||||
for _, mount := range mountFlags {
|
||||
mount(prefix)
|
||||
}
|
||||
|
||||
flag.Parse()
|
||||
return config
|
||||
}
|
||||
|
||||
@ -8,18 +8,14 @@ import (
|
||||
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
"github.com/crusttech/crust/internal/rbac"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := flags("sam", service.Flags, auth.Flags, rbac.Flags)
|
||||
|
||||
log.Printf("Starting sam, version: %v, built on: %v", version.Version, version.BuildTime)
|
||||
flags("sam", service.Flags, auth.Flags, rbac.Flags)
|
||||
|
||||
// log to stdout not stderr
|
||||
log.SetOutput(os.Stdout)
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
go NewMonitor(config.monitorInterval)
|
||||
|
||||
if err := service.Init(); err != nil {
|
||||
log.Fatalf("Error initializing sam: %+v", err)
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
Alloc,
|
||||
TotalAlloc,
|
||||
Sys,
|
||||
Mallocs,
|
||||
Frees,
|
||||
LiveObjects,
|
||||
PauseTotalNs uint64
|
||||
|
||||
NumGC uint32
|
||||
NumGoroutine int
|
||||
}
|
||||
|
||||
func NewMonitor(duration int) {
|
||||
var (
|
||||
m = Monitor{}
|
||||
rtm runtime.MemStats
|
||||
goroutines = expvar.NewInt("num_goroutine")
|
||||
)
|
||||
var interval = time.Duration(duration) * time.Second
|
||||
for {
|
||||
<-time.After(interval)
|
||||
|
||||
// Read full mem stats
|
||||
runtime.ReadMemStats(&rtm)
|
||||
|
||||
// Number of goroutines
|
||||
m.NumGoroutine = runtime.NumGoroutine()
|
||||
goroutines.Set(int64(m.NumGoroutine))
|
||||
|
||||
// Misc memory stats
|
||||
m.Alloc = rtm.Alloc
|
||||
m.TotalAlloc = rtm.TotalAlloc
|
||||
m.Sys = rtm.Sys
|
||||
m.Mallocs = rtm.Mallocs
|
||||
m.Frees = rtm.Frees
|
||||
|
||||
// Live objects = Mallocs - Frees
|
||||
m.LiveObjects = m.Mallocs - m.Frees
|
||||
|
||||
// GC Stats
|
||||
m.PauseTotalNs = rtm.PauseTotalNs
|
||||
m.NumGC = rtm.NumGC
|
||||
|
||||
// Just encode to json and print
|
||||
b, _ := json.Marshal(m)
|
||||
fmt.Println(string(b))
|
||||
}
|
||||
}
|
||||
@ -8,8 +8,9 @@ import (
|
||||
|
||||
type (
|
||||
appFlags struct {
|
||||
http *config.HTTP
|
||||
db *config.Database
|
||||
http *config.HTTP
|
||||
monitor *config.Monitor
|
||||
db *config.Database
|
||||
}
|
||||
)
|
||||
|
||||
@ -22,6 +23,9 @@ func (c *appFlags) Validate() error {
|
||||
if err := c.http.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.monitor.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.db.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -38,6 +42,7 @@ func Flags(prefix ...string) {
|
||||
|
||||
flags = &appFlags{
|
||||
new(config.HTTP).Init(prefix...),
|
||||
new(config.Monitor).Init(prefix...),
|
||||
new(config.Database).Init(prefix...),
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,7 +15,10 @@ import (
|
||||
|
||||
migrate "github.com/crusttech/crust/crm/db"
|
||||
"github.com/crusttech/crust/crm/rest"
|
||||
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Init() error {
|
||||
@ -59,6 +62,7 @@ func Init() error {
|
||||
func Start() error {
|
||||
var deadline = sigctx.New()
|
||||
|
||||
log.Printf("Starting crm, version: %v, built on: %v", version.Version, version.BuildTime)
|
||||
log.Println("Starting http server on address " + flags.http.Addr)
|
||||
listener, err := net.Listen("tcp", flags.http.Addr)
|
||||
if err != nil {
|
||||
@ -83,6 +87,10 @@ func Start() error {
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
|
||||
if flags.monitor.Interval > 0 {
|
||||
go metrics.NewMonitor(flags.monitor.Interval)
|
||||
}
|
||||
|
||||
go http.Serve(listener, r)
|
||||
<-deadline.Done()
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@ func (*Database) Init(prefix ...string) *Database {
|
||||
return prefix[0] + "-" + s
|
||||
}
|
||||
|
||||
db := new(Database)
|
||||
db = new(Database)
|
||||
flag.StringVar(&db.DSN, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection")
|
||||
flag.StringVar(&db.Profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)")
|
||||
return db
|
||||
|
||||
@ -36,7 +36,7 @@ func (*HTTP) Init(prefix ...string) *HTTP {
|
||||
return prefix[0] + "-" + s
|
||||
}
|
||||
|
||||
http := new(HTTP)
|
||||
http = new(HTTP)
|
||||
flag.StringVar(&http.Addr, p("http-addr"), ":3000", "Listen address for HTTP server")
|
||||
flag.BoolVar(&http.Logging, p("http-log"), true, "Enable/disable HTTP request log")
|
||||
flag.BoolVar(&http.Pretty, p("http-pretty-json"), false, "Prettify returned JSON output")
|
||||
|
||||
@ -31,7 +31,7 @@ func (*JWT) Init(prefix ...string) *JWT {
|
||||
return jwt
|
||||
}
|
||||
|
||||
jwt := new(JWT)
|
||||
jwt = new(JWT)
|
||||
flag.StringVar(&jwt.Secret, "auth-jwt-secret", "", "JWT Secret")
|
||||
flag.Int64Var(&jwt.Expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes")
|
||||
flag.StringVar(&jwt.CookieDomain, "auth-jwt-cookie-domain", "", "JWT Cookie domain")
|
||||
|
||||
27
internal/config/monitor.go
Normal file
27
internal/config/monitor.go
Normal file
@ -0,0 +1,27 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
||||
type (
|
||||
Monitor struct {
|
||||
Interval int
|
||||
}
|
||||
)
|
||||
|
||||
var monitor *Monitor
|
||||
|
||||
func (c *Monitor) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Monitor) Init(prefix ...string) *Monitor {
|
||||
if monitor != nil {
|
||||
return monitor
|
||||
}
|
||||
|
||||
monitor = new(Monitor)
|
||||
flag.IntVar(&monitor.Interval, "monitor-interval", 300, "Monitor interval (seconds, 0 = disable)")
|
||||
return monitor
|
||||
}
|
||||
@ -40,7 +40,7 @@ func (*OIDC) Init(prefix ...string) *OIDC {
|
||||
return oidc
|
||||
}
|
||||
|
||||
oidc := new(OIDC)
|
||||
oidc = new(OIDC)
|
||||
flag.StringVar(&oidc.Issuer, "auth-oidc-issuer", "", "OIDC Issuer")
|
||||
flag.StringVar(&oidc.ClientID, "auth-oidc-client-id", "", "OIDC Client ID")
|
||||
flag.StringVar(&oidc.ClientSecret, "auth-oidc-client-secret", "", "OIDC Client Secret")
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@ -10,6 +10,7 @@ import (
|
||||
type (
|
||||
appFlags struct {
|
||||
http *config.HTTP
|
||||
monitor *config.Monitor
|
||||
db *config.Database
|
||||
repository *repository.Flags
|
||||
}
|
||||
@ -24,6 +25,9 @@ func (c *appFlags) Validate() error {
|
||||
if err := c.http.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.monitor.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.db.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -43,6 +47,7 @@ func Flags(prefix ...string) {
|
||||
|
||||
flags = &appFlags{
|
||||
new(config.HTTP).Init(prefix...),
|
||||
new(config.Monitor).Init(prefix...),
|
||||
new(config.Database).Init(prefix...),
|
||||
new(repository.Flags).Init(prefix...),
|
||||
}
|
||||
|
||||
10
sam/start.go
10
sam/start.go
@ -15,11 +15,14 @@ import (
|
||||
"github.com/titpetric/factory/resputil"
|
||||
|
||||
authService "github.com/crusttech/crust/auth/service"
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
migrate "github.com/crusttech/crust/sam/db"
|
||||
"github.com/crusttech/crust/sam/rest"
|
||||
samService "github.com/crusttech/crust/sam/service"
|
||||
"github.com/crusttech/crust/sam/websocket"
|
||||
|
||||
"github.com/crusttech/crust/internal/auth"
|
||||
"github.com/crusttech/crust/internal/metrics"
|
||||
"github.com/crusttech/crust/internal/version"
|
||||
)
|
||||
|
||||
func Init() error {
|
||||
@ -66,6 +69,7 @@ func Init() error {
|
||||
func Start() error {
|
||||
deadline := sigctx.New()
|
||||
|
||||
log.Printf("Starting sam, version: %v, built on: %v", version.Version, version.BuildTime)
|
||||
log.Println("Starting http server on address " + flags.http.Addr)
|
||||
listener, err := net.Listen("tcp", flags.http.Addr)
|
||||
if err != nil {
|
||||
@ -90,6 +94,10 @@ func Start() error {
|
||||
printRoutes(r, flags.http)
|
||||
mountSystemRoutes(r, flags.http)
|
||||
|
||||
if flags.monitor.Interval > 0 {
|
||||
go metrics.NewMonitor(flags.monitor.Interval)
|
||||
}
|
||||
|
||||
go http.Serve(listener, r)
|
||||
<-deadline.Done()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user