/
main.go
87 lines (68 loc) · 1.74 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
package main
import (
"github.com/gin-gonic/gin"
"github.com/thelegy/koma-bot/sse"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
)
type SoundFS struct {
fs http.FileSystem
}
func (s SoundFS) Open(name string) (http.File, error) {
if strings.HasSuffix(name, ".wav") {
return s.fs.Open(name)
}
return nil, os.ErrNotExist
}
func newSoundFS(c *Config) SoundFS {
s := SoundFS{http.Dir(c.GetConfigString("sounds.dir"))}
return s
}
func indexPage(v Version, timetableInfo TimetableInfo) func(*gin.Context) {
return func(c *gin.Context) {
c.HTML(http.StatusOK, "home.html", gin.H{
"Version": v,
"TimetableInfo": timetableInfo,
})
}
}
func processSignal(out chan<- interface{}) chan<- os.Signal {
in := make(chan os.Signal)
go func(out chan<- interface{}, in <-chan os.Signal) {
for {
sig := <-in
out <- sig
}
}(out, in)
return in
}
func main() {
config := loadConfig()
version := getVersion()
timetableInfo := config.GetTimetableInfo()
sse := sse.NewProvider()
sigStream := processSignal(sse.EventStream)
signal.Notify(sigStream, syscall.SIGUSR1)
twitterApi := twitterConnect(config)
config.ResolveUserIds(twitterApi)
go twitterListen(twitterApi, config, sse.EventStream)
go processTweetSounds(config, sse)
loadRecentTweets(twitterApi, config)
if !config.IsDebugging() {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
router.StaticFS("/static", http.Dir("static"))
router.StaticFS("/sounds", newSoundFS(config))
router.LoadHTMLGlob("templates/*")
initAPI(config, sse.NewClients, router)
router.GET("/", indexPage(version, timetableInfo))
if len(os.Args) > 1 && os.Args[1] == "--docker" {
panic(router.Run("0.0.0.0:8000"))
} else {
panic(router.Run(config.GetConfigString("address")))
}
}