-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (66 loc) · 1.86 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
package main
import (
"github.com/bitmon-world/bitmon-api/controllers"
"github.com/bitmon-world/bitmon-api/types"
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/joho/godotenv/autoload"
"net/http"
"os"
"time"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
App := GetApp()
err := App.Run(":" + port)
if err != nil {
panic(err)
}
}
// GetApp is used to wrap all the additions to the GIN API.
func GetApp() *gin.Engine {
App := gin.Default()
App.Use(cors.Default())
ApplyRoutes(App)
return App
}
func ApplyRoutes(r *gin.Engine) {
api := r.Group("/", gin.BasicAuth(gin.Accounts{
os.Getenv("AUTH_USERNAME"): os.Getenv("AUTH_PASSWORD"),
}))
{
store := persistence.NewInMemoryStore(time.Hour)
ctrl := controllers.NewBitmonController(os.Getenv("MONGODB_URI"), os.Getenv("MONGODB_NAME"))
// General Information
api.GET("/mon/single/:id", cache.CachePage(store, time.Minute*10, func(c *gin.Context) { callWrapper(c, ctrl.GetMonInfo) }))
api.GET("/mon/list", cache.CachePage(store, time.Minute*10, func(c *gin.Context) { callWrapper(c, ctrl.GetMonList) }))
api.POST("/mon/add", cache.CachePage(store, time.Minute*10, func(c *gin.Context) { callWrapper(c, ctrl.GetMonList) }))
// Adventure algorithm
api.POST("/adventure", func(c *gin.Context) { callWrapper(c, ctrl.CalcAdventure) })
}
r.NoRoute(func(c *gin.Context) {
c.String(http.StatusNotFound, "Not Found")
})
}
func callWrapper(c *gin.Context, method func(params types.ReqParams) (interface{}, error)) {
id := c.Param("id")
params := types.ReqParams{
ID: id,
AdventureType: "",
TicketID: "",
TicketProof: "",
}
res, err := method(params)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
} else {
c.JSON(200, res)
return
}
}