m: implement NewServiceCtx, use it in totp-proxy
This commit is contained in:
parent
15b84cb173
commit
6adfbdbcfb
@ -29,7 +29,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := mctx.New()
|
||||
ctx := m.NewServiceCtx()
|
||||
logger := mlog.From(ctx)
|
||||
cookieName := mcfg.String(ctx, "cookie-name", "_totp_proxy", "String to use as the name for cookies")
|
||||
cookieTimeout := mcfg.Duration(ctx, "cookie-timeout", mtime.Duration{1 * time.Hour}, "Timeout for cookies")
|
||||
|
32
m/m.go
32
m/m.go
@ -24,11 +24,33 @@ func CfgSource() mcfg.Source {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Create a function, `NewService() mctx.Context` which preloads the
|
||||
// context with log-level param. Will one day also add debug server. Problem
|
||||
// comes because mlog isn't quite designed right and setting log-level from
|
||||
// config won't propagate changes to child contexts which have already called
|
||||
// mlog.From.
|
||||
// NewServiceCtx returns a Context which should be used as the root Context when
|
||||
// creating long running services, such as an RPC service or database.
|
||||
//
|
||||
// The returned Context will automatically handle setting up global
|
||||
// configuration parameters like "log-level", as well as an http endpoint where
|
||||
// debug information about the running process can be accessed.
|
||||
//
|
||||
// TODO set up the debug endpoint.
|
||||
func NewServiceCtx() mctx.Context {
|
||||
ctx := mctx.New()
|
||||
|
||||
// set up log level handling
|
||||
logLevelStr := mcfg.String(ctx, "log-level", "info", "Maximum log level which will be printed.")
|
||||
mrun.OnStart(ctx, func(mctx.Context) error {
|
||||
logLevel := mlog.LevelFromString(*logLevelStr)
|
||||
if logLevel == nil {
|
||||
return merr.New("invalid log level", "log-level", *logLevelStr)
|
||||
}
|
||||
mlog.CtxSetAll(ctx, func(_ mctx.Context, logger *mlog.Logger) *mlog.Logger {
|
||||
logger.SetMaxLevel(logLevel)
|
||||
return logger
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
// Run performs the work of populating configuration parameters, triggering the
|
||||
// start event, waiting for an interrupt, and then triggering the stop event.
|
||||
|
48
m/m_test.go
Normal file
48
m/m_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
package m
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
. "testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
||||
)
|
||||
|
||||
func TestServiceCtx(t *T) {
|
||||
t.Run("log-level", func(t *T) {
|
||||
ctx := NewServiceCtx()
|
||||
|
||||
// pull the Logger out of the ctx and set the Handler on it, so we can check
|
||||
// the log level
|
||||
var msgs []mlog.Message
|
||||
logger := mlog.From(ctx)
|
||||
logger.SetHandler(func(msg mlog.Message) error {
|
||||
msgs = append(msgs, msg)
|
||||
return nil
|
||||
})
|
||||
|
||||
// create a child Context before running to ensure it the change propagates
|
||||
// correctly.
|
||||
ctxA := mctx.ChildOf(ctx, "A")
|
||||
|
||||
params := mcfg.ParamValues{{Name: "log-level", Value: json.RawMessage(`"DEBUG"`)}}
|
||||
if err := mcfg.Populate(ctx, params); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if err := mrun.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mlog.From(ctxA).Info("foo")
|
||||
mlog.From(ctxA).Debug("bar")
|
||||
massert.Fatal(t, massert.All(
|
||||
massert.Len(msgs, 2),
|
||||
massert.Equal(msgs[0].Level.String(), "INFO"),
|
||||
massert.Equal(msgs[0].Description.String(), "(/A) foo"),
|
||||
massert.Equal(msgs[1].Level.String(), "DEBUG"),
|
||||
massert.Equal(msgs[1].Description.String(), "(/A) bar"),
|
||||
))
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user