switch env vars to parameters
This commit is contained in:
parent
d9b39dbbb9
commit
1d4c32c360
|
@ -1 +1,2 @@
|
|||
test.db
|
||||
*.db
|
||||
emdb
|
27
service.go
27
service.go
|
@ -1,26 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"ewintr.nl/emdb/app"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.Int("port", 8080, "port to listen on")
|
||||
dbPath = flag.String("dbpath", "test.db", "path to sqlite db")
|
||||
apiKey = flag.String("apikey", "hoi", "api key to use")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
port, err := strconv.Atoi(getParam("API_PORT", "8080"))
|
||||
if err != nil {
|
||||
fmt.Printf("invalid port: %s", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
apiKey := getParam("API_KEY", "hoi")
|
||||
repo, err := app.NewSQLite(getParam("DB_PATH", "test.db"))
|
||||
repo, err := app.NewSQLite(*dbPath)
|
||||
if err != nil {
|
||||
fmt.Printf("could not create new sqlite repo: %s", err.Error())
|
||||
os.Exit(1)
|
||||
|
@ -30,16 +32,9 @@ func main() {
|
|||
"movie": app.NewMovieAPI(repo, logger),
|
||||
}
|
||||
|
||||
go http.ListenAndServe(fmt.Sprintf(":%d", port), app.NewServer(apiKey, apis, logger))
|
||||
go http.ListenAndServe(fmt.Sprintf(":%d", *port), app.NewServer(*apiKey, apis, logger))
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-c
|
||||
}
|
||||
|
||||
func getParam(param, def string) string {
|
||||
if val, ok := os.LookupEnv(param); ok {
|
||||
return val
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue