-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
59 lines (45 loc) · 1.1 KB
/
server.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
package main
import (
"net/http"
"os"
"github.com/AEPi-AK/character-server/models"
log "github.com/Sirupsen/logrus"
"github.com/rs/cors"
"gopkg.in/mgo.v2"
)
var (
DB *mgo.Database
)
func main() {
session, err := mgo.Dial("mongodb://isaac:[email protected]:23480/character-server")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
DB = session.DB("character-server")
// If characters collection empty or non-existent, drop the database
// and reset the auto-incrementing sequence collection.
count, err := DB.C("characters").Count()
if err != nil || count == 0 {
log.Warn("character collection empty or nonexistent, resetting database")
err = DB.DropDatabase()
if err != nil {
panic(err)
}
err = DB.C("counter").Insert(&models.Counter{ID: "isaacsucks", Seq: 0})
if err != nil {
panic(err)
}
}
if err != nil {
panic(err)
}
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
handler := cors.Default().Handler(NewRouter())
log.Info("Listening on port ", port)
log.Fatal(http.ListenAndServe(":"+port, handler))
}