Fixed a lot of stuff

This commit is contained in:
Nabil Ould Hamou 2024-02-28 09:13:08 +01:00
parent 9d947e1e37
commit 46a52dd1d7
4 changed files with 32 additions and 20 deletions

View file

@ -12,7 +12,7 @@ services:
JWT_SECRET: "${JWT_SECRET}" JWT_SECRET: "${JWT_SECRET}"
GIN_MODE: "${GIN_MODE}" GIN_MODE: "${GIN_MODE}"
volumes: volumes:
- assets:/app/assets - ./assets:/data/assets
volumes: volumes:
assets: assets:

View file

@ -5,22 +5,19 @@ import (
"os" "os"
) )
func CreateAssetsFolder() { var DebugBasePath string = "./assets/"
var ReleaseBasePath string = "/data/assets/"
if _, err := os.Stat("assets/images"); os.IsNotExist(err) { func CreateAssetsFolder(basePath string) {
if err := os.MkdirAll("assets/images", os.ModePerm); err != nil {
if _, err := os.Stat(basePath + "images"); os.IsNotExist(err) {
if err := os.MkdirAll(basePath+"images", os.ModePerm); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
if _, err := os.Stat("assets/audios"); os.IsNotExist(err) { if _, err := os.Stat(basePath + "videos"); os.IsNotExist(err) {
if err := os.MkdirAll("assets/audios", os.ModePerm); err != nil { if err := os.MkdirAll(basePath+"videos", os.ModePerm); err != nil {
log.Fatal(err)
}
}
if _, err := os.Stat("assets/videos"); os.IsNotExist(err) {
if err := os.MkdirAll("assets/videos", os.ModePerm); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View file

@ -1,14 +1,18 @@
package initializers package initializers
import ( import (
"github.com/joho/godotenv"
"log" "log"
"os"
"github.com/joho/godotenv"
) )
func LoadEnv() { func LoadEnv() {
err := godotenv.Load() if os.Getenv("GIN_MODE") != "release" {
err := godotenv.Load()
if err != nil { if err != nil {
log.Fatal("Error loading .env file!") log.Fatal("Error loading .env file!")
}
} }
} }

17
main.go
View file

@ -10,9 +10,16 @@ import (
"os" "os"
) )
var currentMode string
func init() { func init() {
initializers.LoadEnv() initializers.LoadEnv()
initializers.CreateAssetsFolder() currentMode = os.Getenv("GIN_MODE")
if currentMode == "debug" {
initializers.CreateAssetsFolder(initializers.DebugBasePath)
} else {
initializers.CreateAssetsFolder(initializers.ReleaseBasePath)
}
initializers.ConnectToDB() initializers.ConnectToDB()
} }
@ -30,9 +37,13 @@ func main() {
api.Api.GET("/users/:id", controllers.GetUserById) api.Api.GET("/users/:id", controllers.GetUserById)
// Files // Files
api.Router.Static("assets", "./assets/images") if currentMode == "debug" {
api.Router.Static("assets", initializers.DebugBasePath+"/images")
} else {
api.Router.Static("assets", initializers.ReleaseBasePath+"/images")
}
// Posts (middleware to add) // Posts
api.Api.POST("/posts", middleware.RequireAuth, controllers.CreatePost) api.Api.POST("/posts", middleware.RequireAuth, controllers.CreatePost)
// Starting // Starting