diff --git a/docker-compose.yml b/docker-compose.yml index b95c4b5..ab27f3a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,7 +12,7 @@ services: JWT_SECRET: "${JWT_SECRET}" GIN_MODE: "${GIN_MODE}" volumes: - - assets:/app/assets + - ./assets:/data/assets volumes: - assets: \ No newline at end of file + assets: diff --git a/initializers/assets.go b/initializers/assets.go index d858687..d5ef060 100644 --- a/initializers/assets.go +++ b/initializers/assets.go @@ -5,22 +5,19 @@ import ( "os" ) -func CreateAssetsFolder() { +var DebugBasePath string = "./assets/" +var ReleaseBasePath string = "/data/assets/" - if _, err := os.Stat("assets/images"); os.IsNotExist(err) { - if err := os.MkdirAll("assets/images", os.ModePerm); err != nil { +func CreateAssetsFolder(basePath string) { + + if _, err := os.Stat(basePath + "images"); os.IsNotExist(err) { + if err := os.MkdirAll(basePath+"images", os.ModePerm); err != nil { log.Fatal(err) } } - if _, err := os.Stat("assets/audios"); os.IsNotExist(err) { - if err := os.MkdirAll("assets/audios", 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 { + if _, err := os.Stat(basePath + "videos"); os.IsNotExist(err) { + if err := os.MkdirAll(basePath+"videos", os.ModePerm); err != nil { log.Fatal(err) } } diff --git a/initializers/loadEnv.go b/initializers/loadEnv.go index 72df89b..ae44db6 100644 --- a/initializers/loadEnv.go +++ b/initializers/loadEnv.go @@ -1,14 +1,18 @@ package initializers import ( - "github.com/joho/godotenv" "log" + "os" + + "github.com/joho/godotenv" ) func LoadEnv() { - err := godotenv.Load() + if os.Getenv("GIN_MODE") != "release" { + err := godotenv.Load() - if err != nil { - log.Fatal("Error loading .env file!") + if err != nil { + log.Fatal("Error loading .env file!") + } } } diff --git a/main.go b/main.go index d04bfd3..7442b06 100644 --- a/main.go +++ b/main.go @@ -10,9 +10,16 @@ import ( "os" ) +var currentMode string + func init() { initializers.LoadEnv() - initializers.CreateAssetsFolder() + currentMode = os.Getenv("GIN_MODE") + if currentMode == "debug" { + initializers.CreateAssetsFolder(initializers.DebugBasePath) + } else { + initializers.CreateAssetsFolder(initializers.ReleaseBasePath) + } initializers.ConnectToDB() } @@ -30,9 +37,13 @@ func main() { api.Api.GET("/users/:id", controllers.GetUserById) // 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) // Starting