-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·69 lines (53 loc) · 1.4 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
package main
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/bloqhead/demigods.go/handler"
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-gonic/gin"
)
var (
app *gin.Engine
)
func RegisterRouter(r *gin.RouterGroup) {
// prepare our store for route caching
store := persistence.NewInMemoryStore(time.Second)
// fetch all items
r.GET("/api/all", cache.CachePage(store, time.Minute, handler.FetchAll))
// fetch by ID
r.GET("/api/id/:id", cache.CachePage(store, time.Minute, handler.FetchById))
// fetch by category
r.GET("/api/category/:category", cache.CachePage(store, time.Minute, handler.FetchByCategory))
// fetch category list
r.GET("/api/categories", cache.CachePage(store, time.Minute, handler.FetchCategories))
}
func ErrRouter(c *gin.Context) {
c.String(http.StatusBadRequest, "url err")
}
// init gin app
func init() {
app = gin.New()
// Use the CORS middleware
app.Use(handler.Cors)
// Handling routing errors
app.NoRoute(func(c *gin.Context) {
sb := &strings.Builder{}
sb.WriteString("Available routes:\n")
for _, v := range app.Routes() {
sb.WriteString(fmt.Sprintf("- %s %s\n", v.Method, v.Path))
}
c.String(http.StatusBadRequest, sb.String())
})
r := app.Group("/")
// register route
RegisterRouter(r)
}
func main() {
// make sure our data is prepped
handler.FetchData()
// run the app
app.Run(":8080")
}