-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (65 loc) · 2.12 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
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"github.com/barskern/paragliding/igcserver"
"github.com/globalsign/mgo"
log "github.com/sirupsen/logrus"
"net/http"
"os"
)
func main() {
for _, v := range os.Args {
switch v {
case "-v":
log.SetLevel(log.DebugLevel)
case "-q":
log.SetLevel(log.WarnLevel)
case "-h":
fmt.Println("Usage: paragliding [-q][-v][-h]\n\n-q Quiet mode (only warn and error)\n-v Verbose mode (all logs)")
os.Exit(0)
}
}
// Get port from env
port, ok := os.LookupEnv("PORT")
if !ok {
port = "8080"
}
// Get mongodb url from env
mongoURL, ok := os.LookupEnv("MONGODB_URI")
if !ok {
log.Fatal("unable to get required envvar 'MONGODB_URI'")
}
log.WithFields(log.Fields{
"port": port,
"logLevel": log.GetLevel(),
}).Info("initializing server")
mongoSession, err := mgo.Dial(mongoURL)
if err != nil {
log.WithFields(log.Fields{
"uri": mongoURL,
"error": err,
}).Fatal("unable to connect to mongo db")
}
// Make a http client which the server will use for external requests
httpClient := http.Client{}
// Create a track metas abstraction which will connect to mongodb to store
// all igctracks
trackMetas := igcserver.NewTrackMetasDB(mongoSession.Copy())
// Create a webhooks abstraction which will connect to a mongodb to store
// all webhooks
webhooks := igcserver.NewWebhooksDB(mongoSession.Copy(), &httpClient)
// Make simple ticker for database
ticker := igcserver.NewTickerDB(mongoSession.Copy(), 10)
// Create a new server which encompasses all routing and server state
server := igcserver.NewServer(&httpClient, &trackMetas, &ticker, &webhooks)
// Route all requests to `paragliding/api/` to the server and remove prefix
http.Handle("/paragliding/api/", http.StripPrefix("/paragliding/api", &server))
http.Handle("/paragliding", http.RedirectHandler("/paragliding/api/", http.StatusMovedPermanently))
// This function will block the current thread
err = http.ListenAndServe(":"+port, nil)
mongoSession.Close()
// We will only get to this statement if the server unexpectedly crashes
log.WithFields(log.Fields{
"error": err,
}).Fatal("server error occurred")
}