-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
168 lines (137 loc) · 3.96 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/patrickmn/go-cache"
"github.com/shokujinjp/markov-menu/pkg/markov"
"github.com/shokujinjp/shokujinjp-sdk-go/shokujinjp"
)
const location = "Asia/Tokyo"
var (
c = cache.New(1*time.Hour, 2*time.Hour)
)
const (
cacheKeyALL = "all"
)
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"health": "ok"}`)
}
func menuAll(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
all, found := c.Get(cacheKeyALL)
if !found {
values, err := shokujinjp.GetMenuAllData()
if err != nil {
log.Printf("failed to GetMenuAllData: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
c.Set(cacheKeyALL, values, cache.DefaultExpiration)
all = values
}
jb, err := json.Marshal(all)
if err != nil {
log.Printf("failed to json.Marshal: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(jb))
}
func menuToday(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
todayTime := time.Now().Format(shokujinjp.DayFormat)
today, found := c.Get(todayTime)
if !found {
values, err := shokujinjp.GetMenuDateData(time.Now())
if err != nil {
log.Printf("failed to GetMenuDateData: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
c.Set(todayTime, values, cache.DefaultExpiration)
today = values
}
sortedToday := shokujinjp.SortByCategory((today).([]shokujinjp.Menu))
jb, err := json.Marshal(sortedToday)
if err != nil {
log.Printf("failed to json.Marshal: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(jb))
}
func jokeMarkov(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
all, found := c.Get(cacheKeyALL)
if !found {
values, err := shokujinjp.GetMenuAllData()
if err != nil {
log.Printf("failed to GetMenuAllData: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
c.Set(cacheKeyALL, values, cache.DefaultExpiration)
all = values
}
parsed, err := markov.Parse((all).([]shokujinjp.Menu))
if err != nil {
log.Printf("failed to markov.Parse: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
chain := markov.GenerateChain(parsed)
chain = markov.TrimSystemMessages(chain)
rand.Seed(time.Now().UnixNano())
price := rand.Intn(100000)
menu := shokujinjp.Menu{
Id: "joke",
Name: strings.Join(chain, ""),
Price: strconv.Itoa(price),
Category: shokujinjp.NoCategory.String(),
DayStart: "",
DayEnd: "",
CanWeekday: "",
Description: "joke menu generated by markov chain",
}
jb, err := json.Marshal(menu)
if err != nil {
log.Printf("failed to json.Marshal: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprint(w, string(jb))
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
loc, err := time.LoadLocation(location)
if err != nil {
loc = time.FixedZone(location, 9*60*60)
}
time.Local = loc
allowedHeaders := handlers.AllowedHeaders([]string{"X-Requested-With"})
allowedOrigins := handlers.AllowedOrigins([]string{"*"})
allowedMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"})
log.SetFlags(log.LstdFlags | log.Lshortfile)
router := mux.NewRouter()
router.Path("/").HandlerFunc(index)
rMenu := router.PathPrefix("/menu").Subrouter()
rMenu.Path("/all").HandlerFunc(menuAll)
rMenu.Path("/today").HandlerFunc(menuToday)
router.Path("/joke/markov").HandlerFunc(jokeMarkov)
if err := http.ListenAndServe(":"+port, handlers.CORS(allowedHeaders, allowedOrigins, allowedMethods)(router)); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}