-
Notifications
You must be signed in to change notification settings - Fork 0
/
rex.go
224 lines (194 loc) · 5.54 KB
/
rex.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strconv"
"time"
"github.com/clbx/rex/db"
_ "github.com/clbx/rex/docs"
"github.com/clbx/rex/platform"
"github.com/clbx/rex/search"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
var platforms []platform.Platform
var ctx = context.Background()
var gamedb *db.DB
var apikey = "f8e8aae3d8fdcd3d4d29c1e2a65d899410001610d398afd6df7964fa1b527e1a"
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
// @title Rex
// @description Self Hostable Game Library
// @host localhost:8080
// @BasePath /
// @schemes http
func main() {
r := gin.Default()
r.Use(CORSMiddleware())
startup()
findGames()
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json")
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
r.GET("/v1/ping", ping)
r.GET("/v1/games", getGames)
r.GET("/v1/games/byId", getGamesById)
r.GET("/v1/platforms", getPlatforms)
r.GET("/cache/:filename", func(c *gin.Context) {
fileName := c.Param("filename")
targetPath := filepath.Join("/cache/", fileName)
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+fileName)
c.Header("Content-Type", "application/octet-stream")
c.File(targetPath)
})
r.POST("/v1/games/setGameById", setGameById)
r.Run()
}
func startup() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("/config/")
viper.AutomaticEnv()
err := viper.ReadInConfig()
if err != nil {
log.Fatal(err)
}
err = viper.UnmarshalKey("platforms", &platforms)
if err != nil {
log.Fatal(err)
}
//platforms := []platform.Platform(viper.Get("platforms"))
//fmt.Printf("%+v\n", platforms)
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
gamedb, err = db.InitMongoDB(ctx, "mongodb://mongodb:27017")
}
func findGames() {
// Iterate through every platform
for _, p := range platforms {
// Iterate through every directory that is a part of every platform
for _, dir := range p.Directories {
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
// Iterate through every file in directory
for _, file := range files {
log.Printf("[%s] Found file %s", p.Name, dir+"/"+file.Name())
// Attempt to Identify the Game by Platform Specific Information
game, err := platform.IdenfityGameByPlatform(p, dir, file.Name())
// Search the game in TGDB
//TODO: Filter for multiple IDs
ids := search.TGDBsearchGameByName(apikey, game)
fmt.Printf("ID: %d", ids[0])
if ids[0] != -1 {
game = search.TGDBsearchGameByID(apikey, ids[0], game)
}
//spew.Dump(game)
if err != nil {
log.Fatal(err)
}
err = db.AddGame(gamedb, ctx, game)
if err != nil {
log.Fatal(err)
}
}
}
}
}
// ping godoc
// @Summary Ping!
// @Description Pong!
// @Accept */*
// @Produce json
// @Success 200 {object} string
// @Router /v1/ping [get]
func ping(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
}
// getGames godoc
// @Summary Get All Games
// @Description Get a list of all of the games that Rex can find
// @Produce json
// @Success 200
// @Router /v1/games [get]
func getGames(c *gin.Context) {
allGames, err := db.GetAllGames(gamedb, ctx)
if err != nil {
fmt.Printf("HERE1.5\n")
log.Fatal(err)
}
fmt.Printf("HERE2\n")
c.JSON(http.StatusOK, allGames)
}
// getGames godoc
// @Summary Get game by UUID
// @Description Get a game by UUID
// @Produce json
// @Success 200
// @Router /v1/games/byId [get]
// @Param id path string true "ID of the game to search for"
func getGamesById(c *gin.Context) {
gameId := c.Query("id")
if gameId == "" {
c.JSON(http.StatusBadRequest, "No UUID provided with game")
return
}
game, err := db.GetGameByID(gamedb, ctx, gameId)
if err != nil {
log.Fatal(err)
}
//TODO: check if more than one, there never should be, need to figure out mongo better
c.JSON(http.StatusOK, game[0])
}
// getGames godoc
// @Summary Get game by UUID
// @Description Get a game by UUID
// @Produce json
// @Success 200
// @Router /v1/games/setGameById [post]
func setGameById(c *gin.Context) {
gameToSet := c.Query("id")
tgdbIDstr := c.Query("tgdbid")
if gameToSet == "" || tgdbIDstr == "" {
c.JSON(http.StatusBadRequest, "No UUID or IGDB ID Provided")
return
}
tgdbID, _ := strconv.Atoi(tgdbIDstr)
game, err := db.GetGameByID(gamedb, ctx, gameToSet)
if err != nil {
log.Fatal(err)
}
game[0] = search.TGDBsearchGameByID(apikey, tgdbID, game[0])
db.AddGame(gamedb, ctx, game[0])
c.JSON(200, gin.H{"status": "ID Assigned"})
}
// getPlatforms godoc
// @Summary Get platforms with games
// @Description Returns a list of platforms with games in the library
// @Produce json
// @Sucess 200
// @Router /v1/platforms [get]
func getPlatforms(c *gin.Context) {
c.JSON(http.StatusOK, platforms)
}