-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (53 loc) · 1.42 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
package main
import (
"net/http"
"os"
"time"
controller "github.com/CONCRETE-Project/concretepay-rates/controllers"
"github.com/CONCRETE-Project/concretepay-rates/services"
"github.com/gin-contrib/cache"
"github.com/gin-contrib/cache/persistence"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/ulule/limiter/v3"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
"github.com/ulule/limiter/v3/drivers/store/memory"
)
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
}
// ApplyRoutes is used to attach all the routes to the API service.
func ApplyRoutes(r *gin.Engine) {
api := r.Group("/")
{
rate := limiter.Rate{
Period: 1 * time.Hour,
Limit: 1000,
}
cacheStore := persistence.NewInMemoryStore(time.Second)
store := memory.NewStore()
limiterMiddleware := mgin.NewMiddleware(limiter.New(store, rate))
api.Use(limiterMiddleware)
coinGecko := services.NewCoinGeckoService()
rateCtrl := controller.RateController{CoinGeckoService: coinGecko}
api.GET(":coin", cache.CachePage(cacheStore, time.Minute*5, rateCtrl.GetCoin))
}
r.NoRoute(func(c *gin.Context) {
c.String(http.StatusNotFound, "Not Found")
})
}