-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
53 lines (43 loc) · 1.14 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/ympons/flasher/db"
"github.com/ympons/flasher/server"
)
func main() {
port := getenv("PORT", "5900")
basePath := getenv("BASE_PATH", "web")
secretKey := getenv("SECRET_KEY_BASE", "8c18346e742aec88ddf68fc9f51e5e")
dbSource := getenv("DATABASE_URL", "./db/cards-jwasham.db")
credentials := getenv("CREDENTIALS", "admin:admin")
dbInstance, err := db.Open("sqlite3", dbSource)
if err != nil {
log.Fatalf("Unable to connect to the DB: %v", err)
}
if err := dbInstance.InitDbSchemas(); err != nil {
log.Fatalf("Initializing DB schemas failed: %v", err)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGTERM)
srv := server.New(basePath, secretKey, credentials, dbInstance)
// Block until a signal is received
go func() {
sig := <-c
if err := srv.Close(); err != nil {
log.Printf("Failed to close server: %v", err)
}
log.Printf("Exiting given signal: %v", sig)
os.Exit(0)
}()
srv.Run(":" + port)
}
func getenv(k string, v string) string {
if val := os.Getenv(k); val != "" {
return val
}
os.Setenv(k, v)
return v
}