-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (90 loc) · 2.88 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
package main
import (
"net/http"
"time"
"github.com/bityield/protocol-api/backend"
"github.com/bityield/protocol-api/controllers"
v1Coins "github.com/bityield/protocol-api/controllers/v1/historicals/coins"
v1Funds "github.com/bityield/protocol-api/controllers/v1/historicals/funds"
"github.com/bityield/protocol-api/interfaces/scrapers/coinmarketcap"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"github.com/jinzhu/gorm"
ginlogrus "github.com/toorop/gin-logrus"
)
// DatabaseMiddleware for gin to pass DB context around
func DatabaseMiddleware(db *gorm.DB) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("database", db)
c.Next()
}
}
// RedisMiddleware for gin to pass DB context around
func RedisMiddleware(db *redis.Client) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("redis", db)
c.Next()
}
}
// CORSMiddleware ...
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func main() {
// Initalize a new client, the base entrpy point to the application code
b, e := backend.NewBackend(true, true)
if e != nil {
panic(e)
}
// Worker jobs
go func() {
for now := range time.Tick(time.Second * 43200) {
b.L.Infoln("Executing scrape job at:", now)
coinmarketcap.Execute(b)
}
}()
// Database connect, defer close
defer b.R.D.Close()
// Set the initial API instance
r := gin.Default()
// Enable and/or set cors
cf := cors.DefaultConfig()
cf.AllowAllOrigins = true
cf.AllowCredentials = true
cf.AddAllowHeaders("authorization")
r.Use(cors.New(cf))
// r.Use(cors.Default())
r.Use(CORSMiddleware())
// Use Middleware to pass around the db connection
r.Use(ginlogrus.Logger(b.L), gin.Recovery())
r.Use(DatabaseMiddleware(b.R.D))
r.Use(RedisMiddleware(b.R.R))
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"bityield": "Welcome to the Bityield API. Visit https://bityield.finance/developers/api for details about this API.",
})
})
r.GET("/health", func(c *gin.Context) {
c.Data(200, "text/plain", []byte("OK"))
})
// Static files
r.StaticFile("/v1/coins", "./coins.json")
// Funds endpoints
r.GET("/v1/funds", controllers.FindFunds)
r.GET("/v1/funds/:id", controllers.FindFund)
// API Methods and endpoints
r.GET("/v1/historicals/coin/:symbol", v1Coins.GetCoinHistoricals)
r.GET("/v1/historicals/fund/:symbol", v1Funds.GetFundHistoricals)
r.Run((":" + b.C.GetString("port")))
}