add(auth): separate webservice for auth

This commit is contained in:
Tit Petric
2018-09-04 15:37:43 +02:00
parent d7cac7dbc1
commit bdb0844a48
35 changed files with 1333 additions and 24 deletions
+9
View File
@@ -0,0 +1,9 @@
package main
// this file exists to keep go-spew in vendor for development needs
import (
"github.com/davecgh/go-spew/spew"
)
var _ = spew.Dump
+23
View File
@@ -0,0 +1,23 @@
package main
import (
_ "github.com/joho/godotenv/autoload"
"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)")
for _, mount := range mountFlags {
mount(prefix)
}
flag.Parse()
return config
}
+25
View File
@@ -0,0 +1,25 @@
package main
import (
"log"
"os"
"github.com/crusttech/crust/auth"
"github.com/crusttech/crust/rbac"
)
func main() {
config := flags("auth", rbac.Flags, auth.Flags)
// log to stdout not stderr
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
go NewMonitor(config.monitorInterval)
if err := auth.Init(); err != nil {
log.Fatalf("Error initializing auth: %+v", err)
}
if err := auth.Start(); err != nil {
log.Fatalf("Error starting/running auth: %+v", err)
}
}
+59
View File
@@ -0,0 +1,59 @@
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))
}
}