-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
190 lines (166 loc) · 5.53 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"fmt"
"funholidaysapi/models"
"funholidaysapi/mongoUtil"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
"github.com/gomarkdown/markdown"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
var mongoClient *mongo.Client
var holidayCollection *mongo.Collection
func today(c *gin.Context) {
currentTime := time.Now()
_, month, dayNum := currentTime.Date()
fmt.Printf("today is %d/%d", month, dayNum)
ctx, cancelFunc := mongoUtil.Timeout()
defer cancelFunc()
resultSet, err := holidayCollection.Find(ctx, bson.M{"month": month, "day": dayNum})
if err != nil {
// return 500
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. maybe there's no holidays today", ErrorDetails: err.Error()})
return
}
responseBody, err := models.DayListFromResults(resultSet)
c.JSON(http.StatusOK, responseBody)
return
}
func monthQuery(c *gin.Context) {
monthNum, err := strconv.Atoi(c.Param("monthNum"))
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{ErrorDescription: "oops. gotta be a number, dude", ErrorDetails: err.Error()})
return
}
fmt.Printf("finding all holidays in month #%d", monthNum)
ctx, cancelFunc := mongoUtil.Timeout()
defer cancelFunc()
resultSet, err := holidayCollection.Find(ctx, bson.M{"month": monthNum})
if err != nil {
// return 500
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. maybe there's no holidays that month", ErrorDetails: err.Error()})
return
}
responseBody, err := models.MonthListFromResults(resultSet)
c.JSON(http.StatusOK, responseBody)
return
}
func dateQuery(c *gin.Context) {
monthNum, err := strconv.Atoi(c.Param("monthNum"))
dayNum, err := strconv.Atoi(c.Param("dayNum"))
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse{ErrorDescription: "oops. gotta be a number, dude", ErrorDetails: err.Error()})
return
}
fmt.Printf("finding all holidays on %d/%d", monthNum, dayNum)
ctx, cancelFunc := mongoUtil.Timeout()
defer cancelFunc()
resultSet, err := holidayCollection.Find(ctx, bson.M{"month": monthNum, "day": dayNum})
if err != nil {
// return 500
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. maybe there's no holidays on that date", ErrorDetails: err.Error()})
return
}
responseBody, err := models.DayListFromResults(resultSet)
c.JSON(http.StatusOK, responseBody)
return
}
func searchQuery(c *gin.Context) {
keyword := c.Param("keyword")
fmt.Printf("finding all %s holidays", keyword)
ctx, cancelFunc := mongoUtil.Timeout()
defer cancelFunc()
resultSet, err := holidayCollection.Find(ctx, bson.M{"name": primitive.Regex{Pattern: fmt.Sprintf(".*%s.*", keyword), Options: "i"}})
if err != nil {
// return 500
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. maybe there's no holidays on that date", ErrorDetails: err.Error()})
return
}
if resultSet.RemainingBatchLength() == 0 {
log.Print("no search results")
}
responseBody, err := models.SearchResultsFromResults(resultSet)
c.JSON(http.StatusOK, responseBody)
return
}
func randomHoliday(c *gin.Context) {
ctx, cancelFunc := mongoUtil.Timeout()
defer cancelFunc()
daysPerMonth := []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
monthNum := rand.Int() % 12
dayNum := rand.Int() % daysPerMonth[monthNum]
filter := bson.M{"month": monthNum + 1, "day": dayNum}
maxRand, err := holidayCollection.CountDocuments(ctx, filter)
if err != nil {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. not good at counting", ErrorDetails: err.Error()})
return
}
choice := rand.Int() % int(maxRand)
cur, err := holidayCollection.Find(ctx, filter)
if err != nil {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. couldn't pick a card, any card", ErrorDetails: err.Error()})
return
}
for i := 0; i < choice; i++ {
cur.Next(ctx)
}
var selection models.Holiday
cur.Decode(&selection)
c.JSON(http.StatusOK, selection)
return
}
func renderIndex(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "National Api Day",
})
}
func renderDemo(c *gin.Context) {
keyword := c.Query("kw")
date := c.Query("dt")
c.HTML(http.StatusOK, "lookup", gin.H{
"title": "Fun Holiday Lookup Tool",
"date": func() string { return date },
"keyword": func() string { return keyword },
})
}
func renderDocs(c *gin.Context) {
//read file
md, err := os.ReadFile("doc.md")
if err != nil {
c.JSON(http.StatusInternalServerError, models.ErrorResponse{ErrorDescription: "oops. reading markdown is hard", ErrorDetails: err.Error()})
return
}
// parse markdown
mdAsHtml := markdown.ToHTML(md, nil, nil)
c.Data(http.StatusOK, "text/html; charset=utf-8", mdAsHtml)
}
func main() {
fmt.Println("running gin app holiday api")
mongoUtil.LoadDotEnv()
mongoClient = mongoUtil.Connect()
ctx, cancelFunc := mongoUtil.Timeout()
defer mongoClient.Disconnect(ctx)
defer cancelFunc()
holidayCollection = mongoUtil.GetCollection(mongoClient, os.Getenv("HOLIDAYCOLLECTIONNAME"))
r := gin.Default()
api := r.Group("/api")
{
api.GET("/today", today)
api.GET("/month/:monthNum", monthQuery)
api.GET("/date/:monthNum/:dayNum", dateQuery)
api.GET("/search/:keyword", searchQuery)
api.GET("/random", randomHoliday)
}
r.GET("/", renderIndex)
r.GET("/raw-docs", renderDocs)
r.LoadHTMLGlob("frontendV2/public/*.html")
r.Static("/assets", "./frontendV2/public")
r.Run()
}