-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapi.go
61 lines (49 loc) · 1.09 KB
/
api.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
package main
import (
"flag"
"net/http"
"os"
"time"
"github.com/src-d/landing/api/config"
"github.com/src-d/landing/api/handlers"
"github.com/src-d/landing/api/services"
"github.com/erizocosmico/gin-cache"
"github.com/erizocosmico/gin-cache/persistence"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gopkg.in/inconshreveable/log15.v2"
)
var (
ttl = flag.Duration("ttl", 1*time.Hour, "ttl of the cache")
)
func main() {
flag.Parse()
conf, err := config.Load()
checkErr(err)
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowAllOrigins: true,
AllowHeaders: []string{"Content-Type"},
AllowMethods: []string{"GET", "POST"},
}))
store := persistence.NewInMemoryStore(*ttl)
r.GET("/", handlers.Head(200))
api := r.Group("/api")
{
api.GET("/positions", cache.CachePage(
store,
*ttl,
handlers.NewPositions(services.NewPositionsProvider(conf)).Get,
))
}
r.NoRoute(func(c *gin.Context) {
c.AbortWithStatus(http.StatusNotFound)
})
checkErr(r.Run(conf.Addr))
}
func checkErr(err error) {
if err != nil {
log15.Crit(err.Error())
os.Exit(1)
}
}