-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (56 loc) · 1.42 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
54
55
56
57
58
59
60
61
62
63
64
65
66
// Jordy's Chordies - a web app for song chords
// https://github.com/barrettj12/chords
// Copyright 2022, Jordan Barrett (@barrettj12)
// https://github.com/barrettj12
// Licensed under the GNU AGPLv3.
// main.go
// Main entry point for the API/frontend server. Used locally and in
// production.
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/barrettj12/chords/src/dblayer"
"github.com/barrettj12/chords/src/server"
)
func main() {
// Try to read logging flags from LOG_FLAGS environment variable
// Invalid/unset values will just default to 0 (no flags)
flags, _ := strconv.Atoi(os.Getenv("LOG_FLAGS"))
// Initialise logger
logger := log.New(os.Stdout, "", flags)
// Set up DB
var db dblayer.ChordsDB
dbURL := os.Getenv("DATABASE_URL")
db, err := dblayer.GetDB(dbURL, logger)
if err != nil {
panic(err)
}
// defer db.Close()
// Read port from PORT environment variable
port := os.Getenv("PORT")
if _, err := strconv.Atoi(port); err != nil {
// Set default port value
fmt.Printf("Invalid port %q: listening on port 8080 instead\n", port)
port = "8080"
}
// Read authorisation key from env
authKey := os.Getenv("AUTH_KEY")
if authKey == "" {
// Try read from file
data, err := os.ReadFile("auth_key")
if err == nil {
authKey = string(data)
}
}
s, err := server.New(db, ":"+port, logger, authKey)
if err != nil {
panic(err)
}
err = s.Run()
if err != nil {
panic(err)
}
}