Files
fn-serverless/api/server/init.go
Reed Allman 24aa911609 add FN_LOG_DEST for logs, fixup init (#663)
* add FN_LOG_DEST for logs, fixup init

* FN_LOG_DEST can point to a remote logging place (papertrail, whatever)
* FN_LOG_PREFIX can add a prefix onto each log line sent to FN_LOG_DEST

default remains stderr with no prefix. users need this to send to various
logging backends, though it could be done operationally, this is somewhat
simpler.

we were doing some configuration stuff inside of init() for some of the global
things. even though they're global, it's nice to keep them all in the normal
server init path.

we have had strange issues with the tracing setup, I tested the last repro of
this repeatedly and didn't have any luck reproducing it, though maybe it comes
back.

* add docs
2018-01-09 14:27:50 -08:00

58 lines
1.1 KiB
Go

package server
import (
"context"
"os"
"os/signal"
"strconv"
"github.com/fnproject/fn/api/common"
"github.com/gin-gonic/gin"
)
func init() {
// gin is not nice by default, this can get set in logging initialization
gin.SetMode(gin.ReleaseMode)
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func getEnvInt(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
// linter liked this better than if/else
var err error
var i int
if i, err = strconv.Atoi(value); err != nil {
panic(err) // not sure how to handle this
}
return i
}
return fallback
}
func contextWithSignal(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) {
newCTX, halt := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, signals...)
go func() {
for {
select {
case <-c:
common.Logger(ctx).Info("Halting...")
halt()
return
case <-ctx.Done():
common.Logger(ctx).Info("Halting... Original server context canceled.")
halt()
return
}
}
}()
return newCTX, halt
}