-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartbound.go
170 lines (147 loc) · 4.11 KB
/
artbound.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"embed"
"encoding/json"
"html/template"
"log"
"net/http"
"net/url"
"os"
"time"
"github.com/birabittoh/artbound-go/cache"
"github.com/joho/godotenv"
)
const templatesDirectory = "templates/"
var (
//go:embed templates/index.html templates/help.html
templates embed.FS
//go:embed all:static
static embed.FS
indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html"))
helpTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"help.html"))
)
type TemplateData struct {
Emoji EmojiDict
LastUpdated string
CurrentMonth string
}
func indexHandler(db *cache.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Please use GET.", http.StatusMethodNotAllowed)
return
}
lastUpdated := db.LastUpdated.Format("02/01/2006 15:04")
currentMonth := time.Now().Format("2006-01")
templateData := &TemplateData{defaultEmojis, lastUpdated, currentMonth}
buf := &bytes.Buffer{}
err := indexTemplate.Execute(buf, templateData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
buf.WriteTo(w)
}
}
func helpHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Please use GET.", http.StatusMethodNotAllowed)
return
}
buf := &bytes.Buffer{}
err := helpTemplate.Execute(buf, defaultEmojis)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
buf.WriteTo(w)
}
func clearHandler(db *cache.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Please use POST.", http.StatusMethodNotAllowed)
return
}
err := db.Clear()
if err != nil {
log.Println("Error:", err)
http.Error(w, "Could not delete cache.", http.StatusInternalServerError)
}
http.Error(w, "Done.", http.StatusOK)
}
}
func updateHandler(db *cache.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Please use POST.", http.StatusMethodNotAllowed)
return
}
p := db.UpdateCall()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(p)
}
}
func getHandler(db *cache.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(r.URL.String())
if err != nil {
log.Println("Could not parse URL.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
month := u.Query().Get("month")
if month == "" {
http.Error(w, "\"month\" parameter is required.", http.StatusBadRequest)
return
}
entries, err := db.GetEntries(month)
if err != nil {
http.Error(w, "Could not get entries.", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
if len(entries) == 0 {
w.Write([]byte("[]"))
return
}
json.NewEncoder(w).Encode(entries)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Println("No .env file provided.")
}
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
spreadsheetId := os.Getenv("SPREADSHEET_ID")
if spreadsheetId == "" {
log.Fatal("Please fill out SPREADSHEET_ID in .env")
}
spreadsheetRange := os.Getenv("SPREADSHEET_RANGE")
if spreadsheetRange == "" {
log.Fatal("Please fill out SPREADSHEET_RANGE in .env")
}
cacheRemotePath := "/" + cache.CachePath + "/"
cacheFS := http.FileServer(http.Dir(cache.CachePath))
fs := http.FileServer(http.FS(static))
db := cache.InitDB(spreadsheetId, spreadsheetRange)
r := http.NewServeMux()
r.HandleFunc("/", indexHandler(db))
r.HandleFunc("/clear", clearHandler(db))
r.HandleFunc("/update", updateHandler(db))
r.HandleFunc("/get", getHandler(db))
r.HandleFunc("/help", helpHandler)
r.Handle("/static/", fs)
r.Handle(cacheRemotePath, http.StripPrefix(cacheRemotePath, cacheFS))
log.Println("Serving on port", port)
err = http.ListenAndServe(":"+port, r)
if err != nil {
log.Fatal(err)
}
}