diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-26 08:53:24 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-08-26 08:53:24 +0300 |
| commit | f1b7589b2936335957a6a1da1eea3d66233ad0ce (patch) | |
| tree | 1975af217c190f5dbdb23b96015cef45206302d4 | |
| parent | docs: profile improvement swagger (#51) (diff) | |
| download | lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.tar.gz lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.tar.bz2 lphub-f1b7589b2936335957a6a1da1eea3d66233ad0ce.zip | |
refactor: reorganizing packages
Former-commit-id: 99410223654c2a5ffc15fdab6ec3e921b5410cba
| -rw-r--r-- | backend/api/auth.go (renamed from backend/middleware/auth.go) | 4 | ||||
| -rw-r--r-- | backend/api/routes.go | 40 | ||||
| -rw-r--r-- | backend/handlers/home.go (renamed from backend/controllers/homeController.go) | 2 | ||||
| -rw-r--r-- | backend/handlers/login.go (renamed from backend/controllers/loginController.go) | 12 | ||||
| -rw-r--r-- | backend/handlers/map.go (renamed from backend/controllers/mapController.go) | 2 | ||||
| -rw-r--r-- | backend/handlers/mod.go (renamed from backend/controllers/modController.go) | 2 | ||||
| -rw-r--r-- | backend/handlers/record.go (renamed from backend/controllers/recordController.go) | 6 | ||||
| -rw-r--r-- | backend/handlers/user.go (renamed from backend/controllers/userController.go) | 2 | ||||
| -rw-r--r-- | backend/routes/routes.go | 41 | ||||
| -rw-r--r-- | main.go | 8 |
10 files changed, 59 insertions, 60 deletions
diff --git a/backend/middleware/auth.go b/backend/api/auth.go index e2c84fa..91ef80c 100644 --- a/backend/middleware/auth.go +++ b/backend/api/auth.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package middleware | 1 | package api |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| @@ -16,7 +16,7 @@ func CheckAuth(c *gin.Context) { | |||
| 16 | // Validate token | 16 | // Validate token |
| 17 | token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { | 17 | token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
| 18 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { | 18 | if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { |
| 19 | return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) | 19 | return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) |
| 20 | } | 20 | } |
| 21 | return []byte(os.Getenv("SECRET_KEY")), nil | 21 | return []byte(os.Getenv("SECRET_KEY")), nil |
| 22 | }) | 22 | }) |
diff --git a/backend/api/routes.go b/backend/api/routes.go new file mode 100644 index 0000000..4dd8660 --- /dev/null +++ b/backend/api/routes.go | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | package api | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/gin-gonic/gin" | ||
| 5 | "github.com/pektezol/leastportalshub/backend/handlers" | ||
| 6 | swaggerfiles "github.com/swaggo/files" | ||
| 7 | ginSwagger "github.com/swaggo/gin-swagger" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func InitRoutes(router *gin.Engine) { | ||
| 11 | api := router.Group("/api") | ||
| 12 | { | ||
| 13 | v1 := api.Group("/v1") | ||
| 14 | v1.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) | ||
| 15 | v1.GET("/", func(c *gin.Context) { | ||
| 16 | c.File("docs/index.html") | ||
| 17 | }) | ||
| 18 | v1.GET("/token", handlers.GetCookie) | ||
| 19 | v1.DELETE("/token", handlers.DeleteCookie) | ||
| 20 | v1.GET("/home", CheckAuth, handlers.Home) | ||
| 21 | v1.GET("/login", handlers.Login) | ||
| 22 | v1.GET("/profile", CheckAuth, handlers.Profile) | ||
| 23 | v1.PUT("/profile", CheckAuth, handlers.UpdateCountryCode) | ||
| 24 | v1.POST("/profile", CheckAuth, handlers.UpdateUser) | ||
| 25 | v1.GET("/users/:id", CheckAuth, handlers.FetchUser) | ||
| 26 | v1.GET("/demos", handlers.DownloadDemoWithID) | ||
| 27 | v1.GET("/maps/:id/summary", handlers.FetchMapSummary) | ||
| 28 | v1.POST("/maps/:id/summary", CheckAuth, handlers.CreateMapSummary) | ||
| 29 | v1.PUT("/maps/:id/summary", CheckAuth, handlers.EditMapSummary) | ||
| 30 | v1.DELETE("/maps/:id/summary", CheckAuth, handlers.DeleteMapSummary) | ||
| 31 | v1.PUT("/maps/:id/image", CheckAuth, handlers.EditMapImage) | ||
| 32 | v1.GET("/maps/:id/leaderboards", handlers.FetchMapLeaderboards) | ||
| 33 | v1.POST("/maps/:id/record", CheckAuth, handlers.CreateRecordWithDemo) | ||
| 34 | v1.GET("/rankings", handlers.Rankings) | ||
| 35 | v1.GET("/search", handlers.SearchWithQuery) | ||
| 36 | v1.GET("/games", handlers.FetchGames) | ||
| 37 | v1.GET("/games/:id", handlers.FetchChapters) | ||
| 38 | v1.GET("/chapters/:id", handlers.FetchChapterMaps) | ||
| 39 | } | ||
| 40 | } | ||
diff --git a/backend/controllers/homeController.go b/backend/handlers/home.go index d1b99cb..6e9a0df 100644 --- a/backend/controllers/homeController.go +++ b/backend/handlers/home.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "log" | 4 | "log" |
diff --git a/backend/controllers/loginController.go b/backend/handlers/login.go index 9d772a5..4b151c2 100644 --- a/backend/controllers/loginController.go +++ b/backend/handlers/login.go | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "io/ioutil" | 6 | "io" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "os" | 8 | "os" |
| 9 | "time" | 9 | "time" |
| @@ -43,11 +43,7 @@ func Login(c *gin.Context) { | |||
| 43 | } | 43 | } |
| 44 | // Create user if new | 44 | // Create user if new |
| 45 | var checkSteamID int64 | 45 | var checkSteamID int64 |
| 46 | err = database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) | 46 | database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) |
| 47 | // if err != nil { | ||
| 48 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 49 | // return | ||
| 50 | // } | ||
| 51 | // User does not exist | 47 | // User does not exist |
| 52 | if checkSteamID == 0 { | 48 | if checkSteamID == 0 { |
| 53 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | 49 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) |
| @@ -152,7 +148,7 @@ func GetPlayerSummaries(steamId, apiKey string) (*models.PlayerSummaries, error) | |||
| 152 | if err != nil { | 148 | if err != nil { |
| 153 | return nil, err | 149 | return nil, err |
| 154 | } | 150 | } |
| 155 | body, err := ioutil.ReadAll(resp.Body) | 151 | body, err := io.ReadAll(resp.Body) |
| 156 | if err != nil { | 152 | if err != nil { |
| 157 | return nil, err | 153 | return nil, err |
| 158 | } | 154 | } |
diff --git a/backend/controllers/mapController.go b/backend/handlers/map.go index 0a324d6..b47e793 100644 --- a/backend/controllers/mapController.go +++ b/backend/handlers/map.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
diff --git a/backend/controllers/modController.go b/backend/handlers/mod.go index 7acdb5d..e47cb3f 100644 --- a/backend/controllers/modController.go +++ b/backend/handlers/mod.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
diff --git a/backend/controllers/recordController.go b/backend/handlers/record.go index d141fc3..00c9b7d 100644 --- a/backend/controllers/recordController.go +++ b/backend/handlers/record.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "context" | 4 | "context" |
| @@ -228,6 +228,10 @@ func DownloadDemoWithID(c *gin.Context) { | |||
| 228 | url := "https://drive.google.com/uc?export=download&id=" + locationID | 228 | url := "https://drive.google.com/uc?export=download&id=" + locationID |
| 229 | fileName := uuid + ".dem" | 229 | fileName := uuid + ".dem" |
| 230 | output, err := os.Create(fileName) | 230 | output, err := os.Create(fileName) |
| 231 | if err != nil { | ||
| 232 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 233 | return | ||
| 234 | } | ||
| 231 | defer os.Remove(fileName) | 235 | defer os.Remove(fileName) |
| 232 | defer output.Close() | 236 | defer output.Close() |
| 233 | response, err := http.Get(url) | 237 | response, err := http.Get(url) |
diff --git a/backend/controllers/userController.go b/backend/handlers/user.go index 84d589a..51eadb4 100644 --- a/backend/controllers/userController.go +++ b/backend/handlers/user.go | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
diff --git a/backend/routes/routes.go b/backend/routes/routes.go deleted file mode 100644 index 0b80678..0000000 --- a/backend/routes/routes.go +++ /dev/null | |||
| @@ -1,41 +0,0 @@ | |||
| 1 | package routes | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "github.com/gin-gonic/gin" | ||
| 5 | "github.com/pektezol/leastportalshub/backend/controllers" | ||
| 6 | "github.com/pektezol/leastportalshub/backend/middleware" | ||
| 7 | swaggerfiles "github.com/swaggo/files" | ||
| 8 | ginSwagger "github.com/swaggo/gin-swagger" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func InitRoutes(router *gin.Engine) { | ||
| 12 | api := router.Group("/api") | ||
| 13 | { | ||
| 14 | v1 := api.Group("/v1") | ||
| 15 | v1.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) | ||
| 16 | v1.GET("/", func(c *gin.Context) { | ||
| 17 | c.File("docs/index.html") | ||
| 18 | }) | ||
| 19 | v1.GET("/token", controllers.GetCookie) | ||
| 20 | v1.DELETE("/token", controllers.DeleteCookie) | ||
| 21 | v1.GET("/home", middleware.CheckAuth, controllers.Home) | ||
| 22 | v1.GET("/login", controllers.Login) | ||
| 23 | v1.GET("/profile", middleware.CheckAuth, controllers.Profile) | ||
| 24 | v1.PUT("/profile", middleware.CheckAuth, controllers.UpdateCountryCode) | ||
| 25 | v1.POST("/profile", middleware.CheckAuth, controllers.UpdateUser) | ||
| 26 | v1.GET("/users/:id", middleware.CheckAuth, controllers.FetchUser) | ||
| 27 | v1.GET("/demos", controllers.DownloadDemoWithID) | ||
| 28 | v1.GET("/maps/:id/summary", controllers.FetchMapSummary) | ||
| 29 | v1.POST("/maps/:id/summary", middleware.CheckAuth, controllers.CreateMapSummary) | ||
| 30 | v1.PUT("/maps/:id/summary", middleware.CheckAuth, controllers.EditMapSummary) | ||
| 31 | v1.DELETE("/maps/:id/summary", middleware.CheckAuth, controllers.DeleteMapSummary) | ||
| 32 | v1.PUT("/maps/:id/image", middleware.CheckAuth, controllers.EditMapImage) | ||
| 33 | v1.GET("/maps/:id/leaderboards", controllers.FetchMapLeaderboards) | ||
| 34 | v1.POST("/maps/:id/record", middleware.CheckAuth, controllers.CreateRecordWithDemo) | ||
| 35 | v1.GET("/rankings", controllers.Rankings) | ||
| 36 | v1.GET("/search", controllers.SearchWithQuery) | ||
| 37 | v1.GET("/games", controllers.FetchGames) | ||
| 38 | v1.GET("/games/:id", controllers.FetchChapters) | ||
| 39 | v1.GET("/chapters/:id", controllers.FetchChapterMaps) | ||
| 40 | } | ||
| 41 | } | ||
| @@ -7,8 +7,8 @@ import ( | |||
| 7 | 7 | ||
| 8 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 9 | "github.com/joho/godotenv" | 9 | "github.com/joho/godotenv" |
| 10 | "github.com/pektezol/leastportalshub/backend/api" | ||
| 10 | "github.com/pektezol/leastportalshub/backend/database" | 11 | "github.com/pektezol/leastportalshub/backend/database" |
| 11 | "github.com/pektezol/leastportalshub/backend/routes" | ||
| 12 | _ "github.com/pektezol/leastportalshub/docs" | 12 | _ "github.com/pektezol/leastportalshub/docs" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| @@ -19,8 +19,8 @@ import ( | |||
| 19 | // @license.name GNU General Public License, Version 2 | 19 | // @license.name GNU General Public License, Version 2 |
| 20 | // @license.url https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | 20 | // @license.url https://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 21 | 21 | ||
| 22 | // @host lp.ardapektezol.com/api | 22 | // @host lp.ardapektezol.com/api |
| 23 | // @BasePath /v1 | 23 | // @BasePath /v1 |
| 24 | func main() { | 24 | func main() { |
| 25 | if os.Getenv("ENV") == "PROD" { | 25 | if os.Getenv("ENV") == "PROD" { |
| 26 | gin.SetMode(gin.ReleaseMode) | 26 | gin.SetMode(gin.ReleaseMode) |
| @@ -31,6 +31,6 @@ func main() { | |||
| 31 | } | 31 | } |
| 32 | router := gin.Default() | 32 | router := gin.Default() |
| 33 | database.ConnectDB() | 33 | database.ConnectDB() |
| 34 | routes.InitRoutes(router) | 34 | api.InitRoutes(router) |
| 35 | router.Run(fmt.Sprintf(":%s", os.Getenv("PORT"))) | 35 | router.Run(fmt.Sprintf(":%s", os.Getenv("PORT"))) |
| 36 | } | 36 | } |