-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
123 lines (112 loc) · 3.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"flag"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
_ "github.com/jmoussa/crypto-dashboard/docs"
// gRPC Microservices
cdm_client "github.com/jmoussa/crypto-dashboard/coindeskmicro/client" // TODO: move with startAPI() to separate package
cdm_server "github.com/jmoussa/crypto-dashboard/coindeskmicro/server"
twitter_client "github.com/jmoussa/crypto-dashboard/twittermicro/client"
twitter_server "github.com/jmoussa/crypto-dashboard/twittermicro/server"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// TODO: move and reference crypto-dashboard/api/api.go
func startAPI() {
router := gin.Default()
// Routes
router.GET("/", HealthCheck)
url := ginSwagger.URL("http://localhost:3000/swagger/doc.json") // The url pointing to API definition
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
router.GET("/coindesk", func(c *gin.Context) {
// microservice client handles request via gRPC
content, err := cdm_client.FetchCoinDeskData()
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
c.JSON(200, content)
})
router.POST("/twitter", func(c *gin.Context) {
// microservice client handles request via gRPC
max_entries := c.PostForm("max_entries")
max_entries_int, err := strconv.ParseInt(max_entries, 0, 64)
if err != nil {
log.Printf("Error when parsing max_entries: %s\nUsing default value of 100", err)
max_entries_int = 100
}
content, err := twitter_client.FetchTwitterData(max_entries_int)
if err != nil {
c.JSON(500, gin.H{
"message": "Could not fetch data from Twitter API",
"error": err.Error(),
})
return
}
c.JSON(200, content)
})
router.Run(":3000") // listen and serve on 0.0.0.0:3000 (for windows "localhost:3000")
}
// @title Gin Swagger Example API
// @version 1.0
// @description This is a sample server server.
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:3000
// @BasePath /
// @schemes http
func main() {
var server bool
var api bool
var coindesk bool
var twitter bool
flag.BoolVar(&server, "server", false, "Run the server")
flag.BoolVar(&coindesk, "coindesk", false, "Run the server")
flag.BoolVar(&twitter, "twitterscraper", false, "Run the server")
flag.BoolVar(&api, "api", false, "Run the api")
flag.Parse()
log.Printf("Server flag: %v", server)
log.Printf("api flag: %v", api)
if server {
if twitter {
log.Println("Starting Twitter Microservice server...")
twitter_server.StartServer()
} else if coindesk {
log.Println("Starting Coindesk Microservice server...")
cdm_server.StartServer()
} else {
log.Println("Please specify a microservice to run ('coindesk', 'twitter') as a flag")
}
} else if api {
log.Println("Starting Top Level API...")
startAPI()
}
}
// HealthCheck godoc
// @Summary Show the status of server.
// @Description get the status of server.
// @Tags root
// @Accept */*
// @Produce json
// @Success 200 {object} map[string]interface{}
// @Router / [get]
func HealthCheck(c *gin.Context) {
res := map[string]interface{}{
"data": "Server is up and running",
}
c.JSON(http.StatusOK, res)
}