-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
83 lines (64 loc) · 2 KB
/
server.go
File metadata and controls
83 lines (64 loc) · 2 KB
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
80
81
82
83
package main
import (
"context"
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
database "rb3server/database"
"rb3server/servers"
)
func main() {
uri := os.Getenv("MONGOCONNECTIONSTRING")
if uri == "" {
log.Fatalln("GoCentral relies on MongoDB. You must set a MongoDB connection string to use GoCentral")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
log.Fatalln("Could not connect to MongoDB: ", err)
}
defer func() {
if err = client.Disconnect(ctx); err != nil {
log.Fatalln("Could not connect to MongoDB: ", err)
}
}()
// Ping the primary
if err := client.Ping(ctx, readpref.Primary()); err != nil {
log.Fatalln("Could not ping MongoDB: ", err)
}
log.Println("Successfully established connection to MongoDB")
database.GocentralDatabase = client.Database("gocentral")
configCollection := database.GocentralDatabase.Collection("config")
// get config from DB
err = configCollection.FindOne(nil, bson.M{}).Decode(&servers.Config)
if err != nil {
log.Println("Could not get config from MongoDB database, creating default config: ", err)
_, err = configCollection.InsertOne(nil, bson.D{
{Key: "last_pid", Value: 500},
{Key: "last_band_id", Value: 0},
{Key: "last_character_id", Value: 0},
})
servers.Config.LastPID = 500
servers.Config.LastCharacterID = 0
servers.Config.LastBandID = 0
if err != nil {
log.Fatalln("Could not create default config! GoCentral cannot proceed: ", err)
}
}
// seed randomness with current time
rand.Seed(time.Now().UnixNano())
go servers.StartAuthServer()
go servers.StartSecureServer()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Printf("Signal (%s) received, stopping\n", s)
}