-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (92 loc) · 3.07 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
package main
// Copyright (C) 2018 Joas Schilling <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import (
"database/sql"
"log"
"net/http"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
)
var (
config *viper.Viper
db *sql.DB
)
func main() {
arguments := os.Args[1:]
if len(arguments) != 1 ||
(arguments[0] != "test" &&
arguments[0] != "fetch" &&
arguments[0] != "server" &&
arguments[0] != "streaks") {
log.Println("./my-cfhn-presence-stats [argument]")
log.Println(" * \"test\" the config, database connection and connection to the presence API")
log.Println(" * \"fetch\" query the presence API and store the data in the database")
log.Println(" * \"server\" start the server which servers the stats")
log.Println(" * \"streaks\" recalculate the streaks based on the presence data")
return
}
config = viper.New()
config.SetConfigName("config")
config.AddConfigPath(".")
if err := config.ReadInConfig(); err != nil {
log.Fatalf("[✘ ] Fatal error config file: %s \n", err)
return
}
log.Println("[✓ ] Config file loaded")
// Open database connection
var err error
db, err = sql.Open("mysql",
config.GetString("database.user")+":"+config.GetString("database.password")+
"@tcp("+config.GetString("database.host")+")/"+config.GetString("database.name"))
if err != nil {
log.Fatalf("[✘ ] Fatal error database connection: %s \n", err)
return
}
log.Println("[✓ ] Database connection established")
defer db.Close()
if arguments[0] == "test" {
loc, _ := time.LoadLocation("Europe/Berlin")
now := time.Now().In(loc)
log.Printf("[?] %s", now)
client := &http.Client{}
_, err = client.Get(config.GetString("presence_api.server"))
if err != nil {
log.Fatalf("[✘ ] Could not connect to resence API: %s", err)
return
}
log.Println("[✓ ] Presence API is reachable")
} else if arguments[0] == "fetch" {
fetchPresencesFromAPI()
} else if arguments[0] == "streaks" {
recalculateStreaksFromPresences()
} else if arguments[0] == "server" {
// Fetch the presences via Cron
fetchPresencesViaCron()
// Create a mux for routing incoming requests
m := http.NewServeMux()
// All URLs will be handled by this function
m.HandleFunc("/api", apiGetStats)
m.HandleFunc("/web/", serveWebsite)
s := &http.Server{
Addr: ":" + config.GetString("server.port"),
Handler: m,
}
log.Printf("[✓ ] Listening on port %d", config.GetInt("server.port"))
log.Fatal(s.ListenAndServe())
}
}