mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Drop viper dependency (#550)
* Removed viper dependency. * removed from glide files
This commit is contained in:
@@ -2,34 +2,25 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
viper.AutomaticEnv() // picks up env vars automatically
|
||||
cwd, err := os.Getwd()
|
||||
var err error
|
||||
currDir, err = os.Getwd()
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatalln("")
|
||||
}
|
||||
// Replace forward slashes in case this is windows, URL parser errors
|
||||
cwd = strings.Replace(cwd, "\\", "/", -1)
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.SetDefault(EnvLogLevel, "info")
|
||||
viper.SetDefault(EnvMQURL, fmt.Sprintf("bolt://%s/data/worker_mq.db", cwd))
|
||||
viper.SetDefault(EnvDBURL, fmt.Sprintf("sqlite3://%s/data/fn.db", cwd))
|
||||
viper.SetDefault(EnvLOGDBURL, "") // default to just using DB url
|
||||
viper.SetDefault(EnvPort, 8080)
|
||||
viper.SetDefault(EnvZipkinURL, "") // off default
|
||||
viper.SetDefault(EnvAPIURL, fmt.Sprintf("http://127.0.0.1:%d", viper.GetInt(EnvPort)))
|
||||
viper.AutomaticEnv() // picks up env vars automatically
|
||||
logLevel, err := logrus.ParseLevel(viper.GetString(EnvLogLevel))
|
||||
currDir = strings.Replace(currDir, "\\", "/", -1)
|
||||
|
||||
logLevel, err := logrus.ParseLevel(getEnv(EnvLogLevel, DefaultLogLevel))
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatalln("Invalid log level.")
|
||||
}
|
||||
@@ -41,6 +32,26 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -243,7 +243,7 @@ func TestRouteRunnerTimeout(t *testing.T) {
|
||||
},
|
||||
[]*models.Route{
|
||||
{Path: "/pull", AppName: "myapp", Image: "fnproject/sleeper", Type: "sync", Memory: 128, Timeout: 30, IdleTimeout: 30},
|
||||
{Path: "/sleeper", AppName: "myapp", Image: "fnproject/sleeper", Type: "sync", Memory: 128, Timeout: 1, IdleTimeout: 30},
|
||||
{Path: "/sleeper", AppName: "myapp", Image: "fnproject/sleeper", Type: "sync", Memory: 128, Timeout: 2, IdleTimeout: 30},
|
||||
{Path: "/waitmemory", AppName: "myapp", Image: "fnproject/sleeper", Type: "sync", Memory: hugeMem, Timeout: 1, IdleTimeout: 30},
|
||||
}, nil,
|
||||
)
|
||||
|
||||
@@ -30,18 +30,24 @@ import (
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
"github.com/openzipkin/zipkin-go-opentracing"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
currDir string
|
||||
)
|
||||
|
||||
const (
|
||||
EnvLogLevel = "log_level"
|
||||
EnvMQURL = "mq_url"
|
||||
EnvDBURL = "db_url"
|
||||
EnvLOGDBURL = "logstore_url"
|
||||
EnvPort = "port" // be careful, Gin expects this variable to be "port"
|
||||
EnvAPIURL = "api_url"
|
||||
EnvAPICORS = "api_cors"
|
||||
EnvZipkinURL = "zipkin_url"
|
||||
EnvLogLevel = "LOG_LEVEL"
|
||||
EnvMQURL = "MQ_URL"
|
||||
EnvDBURL = "DB_URL"
|
||||
EnvLOGDBURL = "LOGSTORE_URL"
|
||||
EnvPort = "PORT" // be careful, Gin expects this variable to be "port"
|
||||
EnvAPICORS = "API_CORS"
|
||||
EnvZipkinURL = "ZIPKIN_URL"
|
||||
|
||||
// Defaults
|
||||
DefaultLogLevel = "info"
|
||||
DefaultPort = 8080
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -57,19 +63,30 @@ type Server struct {
|
||||
|
||||
// NewFromEnv creates a new Functions server based on env vars.
|
||||
func NewFromEnv(ctx context.Context, opts ...ServerOption) *Server {
|
||||
ds, err := datastore.New(viper.GetString(EnvDBURL))
|
||||
return NewFromURLs(ctx,
|
||||
getEnv(EnvDBURL, fmt.Sprintf("sqlite3://%s/data/fn.db", currDir)),
|
||||
getEnv(EnvMQURL, fmt.Sprintf("bolt://%s/data/fn.mq", currDir)),
|
||||
getEnv(EnvLOGDBURL, ""),
|
||||
opts...,
|
||||
)
|
||||
}
|
||||
|
||||
// Create a new server based on the string URLs for each service.
|
||||
// Sits in the middle of NewFromEnv and New
|
||||
func NewFromURLs(ctx context.Context, dbURL, mqURL, logstoreURL string, opts ...ServerOption) *Server {
|
||||
ds, err := datastore.New(dbURL)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatalln("Error initializing datastore.")
|
||||
}
|
||||
|
||||
mq, err := mqs.New(viper.GetString(EnvMQURL))
|
||||
mq, err := mqs.New(mqURL)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("Error initializing message queue.")
|
||||
}
|
||||
|
||||
var logDB models.LogStore = ds
|
||||
if ldb := viper.GetString(EnvLOGDBURL); ldb != "" && ldb != viper.GetString(EnvDBURL) {
|
||||
logDB, err = logs.New(viper.GetString(EnvLOGDBURL))
|
||||
if ldb := logstoreURL; ldb != "" && ldb != dbURL {
|
||||
logDB, err = logs.New(logstoreURL)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("Error initializing logs store.")
|
||||
}
|
||||
@@ -82,8 +99,9 @@ func optionalCorsWrap(r *gin.Engine) {
|
||||
// By default no CORS are allowed unless one
|
||||
// or more Origins are defined by the API_CORS
|
||||
// environment variable.
|
||||
if len(viper.GetString(EnvAPICORS)) > 0 {
|
||||
origins := strings.Split(strings.Replace(viper.GetString(EnvAPICORS), " ", "", -1), ",")
|
||||
corsStr := getEnv(EnvAPICORS, "")
|
||||
if len(corsStr) > 0 {
|
||||
origins := strings.Split(strings.Replace(corsStr, " ", "", -1), ",")
|
||||
|
||||
corsConfig := cors.DefaultConfig()
|
||||
corsConfig.AllowOrigins = origins
|
||||
@@ -146,7 +164,7 @@ func setTracer() {
|
||||
debugMode = false
|
||||
serviceName = "fnserver"
|
||||
serviceHostPort = "localhost:8080" // meh
|
||||
zipkinHTTPEndpoint = viper.GetString(EnvZipkinURL)
|
||||
zipkinHTTPEndpoint = getEnv(EnvZipkinURL, "")
|
||||
// ex: "http://zipkin:9411/api/v1/spans"
|
||||
)
|
||||
|
||||
@@ -188,7 +206,7 @@ func setTracer() {
|
||||
}
|
||||
|
||||
func setMachineID() {
|
||||
port := uint16(viper.GetInt(EnvPort))
|
||||
port := uint16(getEnvInt(EnvPort, DefaultPort))
|
||||
addr := whoAmI().To4()
|
||||
if addr == nil {
|
||||
addr = net.ParseIP("127.0.0.1").To4()
|
||||
@@ -281,7 +299,7 @@ func (s *Server) Start(ctx context.Context) {
|
||||
func (s *Server) startGears(ctx context.Context, cancel context.CancelFunc) {
|
||||
// By default it serves on :8080 unless a
|
||||
// PORT environment variable was defined.
|
||||
listen := fmt.Sprintf(":%d", viper.GetInt(EnvPort))
|
||||
listen := fmt.Sprintf(":%d", getEnvInt(EnvPort, DefaultPort))
|
||||
|
||||
const runHeader = `
|
||||
______
|
||||
|
||||
Reference in New Issue
Block a user