forked from didip/shawty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (61 loc) · 1.46 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
67
68
69
70
71
package main
import (
"flag"
"math/rand"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
"github.com/NarrativeTeam/shawty/handlers"
"github.com/NarrativeTeam/shawty/storages"
"github.com/getsentry/raven-go"
"github.com/mitchellh/go-homedir"
)
var migrate bool
func init() {
raven.SetDSN(os.Getenv("SENTRY_DSN"))
flag.BoolVar(&migrate, "migrate", false, "Specify to migrate database")
}
func main() {
flag.Parse()
// Seed the randomizer for the token-generation
rand.Seed(time.Now().UnixNano())
runtime.GOMAXPROCS(runtime.NumCPU())
dir, _ := homedir.Dir()
postgresHost := os.Getenv("POSTGRES_HOST")
var storage storages.IStorage
if postgresHost != "" {
user := os.Getenv("POSTGRES_USER")
password := os.Getenv("POSTGRES_PASSWORD")
dbName := os.Getenv("POSTGRES_DB")
ssl := os.Getenv("POSTGRES_SSL") != "false"
pg, err := storages.NewPostgres(postgresHost, dbName, user, password, ssl)
if err != nil {
//Could not correct to postgres
raven.CaptureError(err, nil, nil)
panic(err)
}
storage = pg
if migrate {
pg.Setup()
return
}
} else {
str := &storages.Filesystem{}
err := str.Init(filepath.Join(dir, "shawty"))
if err != nil {
raven.CaptureError(err, nil, nil)
}
storage = str
}
http.HandleFunc("/", raven.RecoveryHandler(handlers.MainHandler(storage)))
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
err := http.ListenAndServe(":"+port, nil)
if err != nil {
raven.CaptureError(err, nil, nil)
}
}