-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (56 loc) · 1.56 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
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/willfantom/lu-covid-api/db"
"github.com/willfantom/lu-covid-api/graphs"
"github.com/willfantom/lu-covid-api/telegram"
"github.com/gorilla/mux"
"github.com/willfantom/lu-covid-api/api"
"github.com/willfantom/lu-covid-api/rates"
log "github.com/sirupsen/logrus"
)
const (
apiBase string = "/api/v1/"
)
func main() {
log.Infoln("Lancaster Univerity covid cases API 🦠 (unofficial)")
if _, exist := os.LookupEnv("DEBUG"); exist {
log.SetLevel(log.DebugLevel)
}
if err := db.Init(); err != nil {
log.Fatalln("🆘 no database connection could be made")
}
justUpdate := flag.Bool("update-db", false, "just update the database and then exit")
flag.Parse()
log.Debugln("💬 initial scrape...")
if err := rates.Scrape(true, true); err != nil {
log.Fatalln("🆘 initial scrape failed!")
}
log.Debugln("✅ scrape success")
if *justUpdate {
log.Infoln("📁 database updated")
os.Exit(0)
}
go fetch()
if token, exists := os.LookupEnv("TG_TOKEN"); exists {
go telegram.Init(token)
}
router := mux.NewRouter()
router.HandleFunc("/", redirect)
api.API(router.PathPrefix("/api").Subrouter())
graphs.API(router.PathPrefix("/graphs").Subrouter())
log.Debugln("💬 running api...")
log.Fatal(http.ListenAndServe(":8080", router))
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://portal.lancaster.ac.uk/intranet/cms/coronavirus/covid-19-statistics", 301)
}
func fetch() {
for {
rates.Scrape(true, false)
time.Sleep(time.Minute * 30)
}
}