-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
136 lines (112 loc) · 2.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
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
package main
import (
"embed"
"html/template"
"log"
"net/http"
"os"
"time"
"github.com/go-co-op/gocron/v2"
"github.com/rockavoldy/pajak/kurs"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
)
var HTTP_PORT = ":8080"
var (
//go:embed templates
res embed.FS
pages = map[string]string{
"/": "templates/index.html",
"/dist/styles.css": "templates/dist/styles.css",
}
)
var t *template.Template
func main() {
t = template.Must(template.ParseFS(res, "templates/index.html"))
template.Must(t.ParseFS(res, "templates/dist/*"))
httpPortEnv := os.Getenv("HTTP_PORT")
if httpPortEnv != "" {
HTTP_PORT = httpPortEnv
}
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(httprate.Limit(
10,
10*time.Second,
httprate.WithKeyFuncs(httprate.KeyByIP, httprate.KeyByEndpoint),
))
// view and assets
// r.Get("/", func(w http.ResponseWriter, r *http.Request) {
// })
r.Get("/", indexHtml)
r.Get("/dist/styles.css", stylesCss)
// kurs API serve
r.Mount("/kurs", kurs.Router())
// Scheduler to run every Wednesday 7AM UTC+7 (new kurs updated every Wednesday)
s, err := gocron.NewScheduler()
if err != nil {
log.Println("err create new scheduler: ", err)
}
defer func() {
_ = s.Shutdown()
}()
s.NewJob(gocron.WeeklyJob(
1,
gocron.NewWeekdays(time.Wednesday),
gocron.NewAtTimes(gocron.NewAtTime(0, 0, 0)),
), gocron.NewTask(
func() {
log.Println("=== Update kurs.json file ===")
log.Println("Cron run at: ", time.Now())
if err := kurs.UpdateKurs(); err != nil {
log.Printf("err scheduler: %s", err)
}
},
))
s.Start()
log.Println("Listening on ", HTTP_PORT)
if err := http.ListenAndServe(HTTP_PORT, r); err != nil {
log.Panicln(err)
}
}
func indexHtml(w http.ResponseWriter, r *http.Request) {
page, ok := pages[r.URL.Path]
if !ok {
w.WriteHeader(http.StatusNotFound)
}
tmpl, err := template.ParseFS(res, page)
if err != nil {
log.Printf("page %s not found", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
data := map[string]interface{}{
"userAgent": r.UserAgent(),
}
if err := tmpl.Execute(w, data); err != nil {
return
}
}
func stylesCss(w http.ResponseWriter, r *http.Request) {
page, ok := pages[r.URL.Path]
if !ok {
w.WriteHeader(http.StatusNotFound)
}
tmpl, err := template.ParseFS(res, page)
if err != nil {
log.Printf("page %s not found", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/css")
w.WriteHeader(http.StatusOK)
data := map[string]interface{}{
"userAgent": r.UserAgent(),
}
if err := tmpl.Execute(w, data); err != nil {
return
}
}