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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strconv"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"ewintr.nl/emdb/app"
|
"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() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
port, err := strconv.Atoi(getParam("API_PORT", "8080"))
|
repo, err := app.NewSQLite(*dbPath)
|
||||||
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"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("could not create new sqlite repo: %s", err.Error())
|
fmt.Printf("could not create new sqlite repo: %s", err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -30,16 +32,9 @@ func main() {
|
||||||
"movie": app.NewMovieAPI(repo, logger),
|
"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)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||||
<-c
|
<-c
|
||||||
}
|
}
|
||||||
|
|
||||||
func getParam(param, def string) string {
|
|
||||||
if val, ok := os.LookupEnv(param); ok {
|
|
||||||
return val
|
|
||||||
}
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue