cmd-api/config.go
Kaustubh Maske Patil 85c71d2372 initial commit
2023-08-31 15:15:38 +05:30

53 lines
1.1 KiB
Go

package main
import (
"log"
"os"
"strconv"
"strings"
"time"
)
// Config is a minimal config struct to hold the
// server port and a list of whitelisted commands
type Config struct {
Port int
WhitelistedCommmands []string
Timeout time.Duration
}
func (config *Config) load() {
// port
portStr, ok := os.LookupEnv("PORT")
if !ok {
portStr = "8080"
}
port, err := strconv.Atoi(portStr)
if err != nil {
// simply print error and exit
log.Print("Error parsing port: ", err)
os.Exit(1)
}
config.Port = port
// whitelisted commands
whitelistedCommandsStr, ok := os.LookupEnv("WHITELISTED_COMMANDS")
if !ok {
whitelistedCommandsStr = "ls,pwd,echo,cat,touch,rm,mkdir"
}
whitelistedCommandsStr = strings.TrimSpace(whitelistedCommandsStr)
config.WhitelistedCommmands = strings.Split(whitelistedCommandsStr, ",")
// timeout
timeoutStr, ok := os.LookupEnv("TIMEOUT")
if !ok {
timeoutStr = "5s"
}
timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
log.Print("Error parsing timeout: ", err)
os.Exit(1)
}
config.Timeout = timeout
}