This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
120 lines (98 loc) · 2.64 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"github.com/elmasy-com/columbus-server/config"
"github.com/elmasy-com/columbus-server/db"
"github.com/elmasy-com/columbus-server/server"
"github.com/elmasy-com/elnet/dns"
)
var (
Version string
Commit string
)
// Returns "up-to-date" and code 0, if no update required.
// Returns the latest release version (eg.: "v0.9.1") and code 1, if update available.
// Returns the error string and code 2, if error happened.
func checkUpdate() {
resp, err := http.Get("https://api.github.com/repos/elmasy-com/columbus-server/releases/latest")
if err != nil {
fmt.Fprintf(os.Stderr, "HTTP failed: %s\n", err)
os.Exit(2)
}
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read body: %s\n", err)
os.Exit(2)
}
var v struct {
TagName string `json:"tag_name"`
}
err = json.Unmarshal(out, &v)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to unmarshal: %s\n", err)
os.Exit(2)
}
if v.TagName == "" {
fmt.Fprint(os.Stderr, "Failed to unmarshal: TagName is empty\n")
os.Exit(2)
}
if Version < v.TagName {
fmt.Printf("%s", v.TagName)
os.Exit(1)
}
fmt.Printf("up-to-date")
os.Exit(0)
}
func main() {
path := flag.String("config", "", "Path to the config file.")
version := flag.Bool("version", false, "Print version informations.")
check := flag.Bool("check", false, "Check for updates.")
flag.Parse()
if *version {
fmt.Printf("Version: %s\n", Version)
fmt.Printf("Git Commit: %s\n", Commit)
os.Exit(0)
}
if *check {
// checkUpdate will exit inside the function
checkUpdate()
}
if *path == "" {
fmt.Fprintf(os.Stderr, "Path to the config file is missing!\n")
fmt.Printf("Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
fmt.Printf("Parsing config file...\n")
if err := config.Parse(*path); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse config file: %s\n", err)
os.Exit(1)
}
// Use common DNS servers
dns.UpdateConfCommon()
fmt.Printf("Connecting to MongoDB...\n")
if err := db.Connect(config.MongoURI); err != nil {
fmt.Fprintf(os.Stderr, "Failed to connect to MongoDB: %s\n", err)
os.Exit(1)
}
defer db.Disconnect()
fmt.Printf("Starting db.StatisticsInsertWorker...\n")
go db.StatisticsInsertWorker()
fmt.Printf("Starting db.StatisticsCleanWorker...\n")
go db.StatisticsCleanWorker()
fmt.Printf("Starting RecordUpdater...\n")
go db.RecordsUpdater()
fmt.Printf("Starting HTTP server...\n")
if err := server.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Server failed: %s\n", err)
os.Exit(1)
} else {
fmt.Printf("HTTP server stopped!\n")
}
}