Adds api
This commit is contained in:
96
cmd/tpapi/init.go
Normal file
96
cmd/tpapi/init.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/hashicorp/logutils"
|
||||
)
|
||||
|
||||
// getEnvString returns string from environment variable
|
||||
func getEnvString(env, def string) (val string) { //nolint:deadcode
|
||||
val = os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// getEnvInt returns int from environment variable
|
||||
func getEnvInt(env string, def int) (ret int) {
|
||||
val := os.Getenv(env)
|
||||
|
||||
if val == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
ret, err := strconv.Atoi(val)
|
||||
if err != nil {
|
||||
log.Fatalf("[ERROR] Environment variable is not numeric: %v\n", env)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func setLogLevel(l int) {
|
||||
switch {
|
||||
case l <= 20:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("ERROR"))
|
||||
case l > 20 && l <= 40:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("WARNING"))
|
||||
case l > 40 && l <= 60:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("INFO"))
|
||||
case l > 60 && l <= 80:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("DEBUG"))
|
||||
case l > 80:
|
||||
config.Log.SetMinLevel(logutils.LogLevel("TRACE"))
|
||||
}
|
||||
}
|
||||
|
||||
func initialize() {
|
||||
var tz string
|
||||
|
||||
// log configuration
|
||||
flag.IntVar(&config.LogLevel,
|
||||
"log",
|
||||
getEnvInt("LOG_LEVEL", 0),
|
||||
"(LOG_LEVEL) Set the log verbosity.",
|
||||
)
|
||||
// locality configuration
|
||||
flag.StringVar(&tz,
|
||||
"timezone",
|
||||
getEnvString("TIMEZONE", "America/Chicago"),
|
||||
"(TIMEZONE) Timezone for the sunrise/sunset calculation.",
|
||||
)
|
||||
// local webserver configuration
|
||||
flag.IntVar(&config.WebSrvPort,
|
||||
"http-port",
|
||||
getEnvInt("HTTP_PORT", 8080),
|
||||
"(HTTP_PORT) Listen port for internal webserver")
|
||||
flag.StringVar(&config.WebSrvIP,
|
||||
"http-ip",
|
||||
getEnvString("HTTP_IP", "0.0.0.0"),
|
||||
"(HTTP_IP) Listen ip for internal webserver")
|
||||
flag.IntVar(&config.WebSrvIdleTimeout,
|
||||
"http-idle-timeout",
|
||||
getEnvInt("HTTP_IDLE_TIMEOUT", 2),
|
||||
"(HTTP_IDLE_TIMEOUT) Idle timeout for internal webserver")
|
||||
flag.IntVar(&config.WebSrvReadTimeout,
|
||||
"http-read-timeout",
|
||||
getEnvInt("HTTP_READ_TIMEOUT", 5),
|
||||
"(HTTP_READ_TIMEOUT) Read timeout for internal webserver")
|
||||
flag.IntVar(&config.WebSrvWriteTimeout,
|
||||
"http-write-timeout",
|
||||
getEnvInt("HTTP_WRITE_TIMEOUT", 2),
|
||||
"(HTTP_WRITE_TIMEOUT) Write timeout for internal webserver")
|
||||
flag.Parse()
|
||||
|
||||
setLogLevel(config.LogLevel)
|
||||
log.SetOutput(config.Log)
|
||||
|
||||
log.Printf("[DEBUG] Initialization complete.")
|
||||
}
|
Reference in New Issue
Block a user