-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure.go
144 lines (118 loc) · 2.81 KB
/
azure.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"time"
"github.com/gin-gonic/gin"
"cal"
)
func main() {
if len(os.Args) > 1 {
if os.Args[1] == "--get" {
callSchedule()
return
}
}
log.Println("Start Introquiz Portal Square Azure")
cfg := getConfig()
go loopGet(cfg.CheckDistance)
// routerの初期設定
router := gin.Default()
router.SetFuncMap(template.FuncMap{
"safe": noescape,
})
// js,css,faviconなどを読み込むためのasstes設定
router.LoadHTMLGlob("view/*.tmpl")
router.Static("/resource", "./resource")
router.StaticFile("/favicon.ico", "./resource/favicon.ico")
router.GET("/", func(ctx *gin.Context) {
sche := cal.GetScheduleJson()
cfg := getConfig()
ctx.HTML(http.StatusOK, "main.tmpl", gin.H{
"title": "Top",
"update": sche.Update,
"sche": getTemplateSche(sche.Schedules),
"circles": getCircles(),
"message": cfg.Msg,
})
})
router.GET("/about", func(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "about.tmpl", gin.H{
"title": "About",
})
})
router.Run(":52417")
log.Println("End Introquiz Portal Square Azure")
}
func noescape(tmpl string) template.HTML {
return template.HTML(tmpl)
}
func loopGet(distance int) {
for {
callSchedule()
time.Sleep(time.Minute * time.Duration(distance))
}
}
func callSchedule() {
err := cal.MakeScheduleJson()
if err == nil {
log.Println("Complete to get events from Google Calendar.")
} else {
log.Println("Fail to get events from Google Calendar.")
}
}
type TmpSchedule struct {
Schedule cal.IntroSchedule
Simple string
CircleName string
Closed bool
}
func getCircles() map[string]cal.Circle {
js, err := ioutil.ReadFile("circles.json")
if err != nil {
log.Fatalf("Can't read circles.json: %v\n", err)
}
var circles map[string]cal.Circle
err = json.Unmarshal(js, &circles)
if err != nil {
log.Fatalf("Unmarshal error circles.json: %v\n", err)
}
return circles
}
func getTemplateSche(sche map[string]cal.IntroSchedule) (sc []TmpSchedule) {
cir := getCircles()
for _, s := range sche {
sc = append(sc, TmpSchedule{
Schedule: s,
Simple: cir[s.CircleId].SimpleName,
CircleName: cir[s.CircleId].Name,
Closed: len(cir[s.CircleId].Overview) == 0,
})
}
sort.Slice(sc, func(i, j int) bool { return sc[i].Schedule.No < sc[j].Schedule.No })
return sc
}
type Config struct {
CheckDistance int `json:"check_distance"`
Msg Message `json:"message"`
}
type Message struct {
Title string `json:"title"`
Content string `json:"content"`
}
func getConfig() (cfg Config) {
js, err := ioutil.ReadFile("config.json")
if err != nil {
log.Fatalf("Can't read config.json: %v\n", err)
}
err = json.Unmarshal(js, &cfg)
if err != nil {
log.Fatalf("Unmarshal error config.json: %v\n", err)
}
return cfg
}