package main import ( "git.bbr-dev.info/brajkovic/resource_manager/cache" "git.bbr-dev.info/brajkovic/resource_manager/resource" "git.bbr-dev.info/brajkovic/resource_manager/security" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "log" "net/http" "os" "strings" "time" ) func init() { log.SetPrefix("[ReM] ") godotenv.Load() } func main() { cacheManager := cache.NewManager() expiration := loadExpiration() log.Println("Presign | expiration set to " + expiration.String()) resourceManager := resource.NewManager(cacheManager, expiration) securityHandlers := setupSecurity() server := gin.Default() if strings.Contains(os.Getenv("PROFILE"), "legacy") { setupLegacyEndpoints(server, securityHandlers, resourceManager) } setupV1Endpoints(server, securityHandlers, resourceManager) log.Fatalln(http.ListenAndServe(":5201", server)) } func setupSecurity() gin.HandlersChain { switch os.Getenv("SECURITY") { case "basic_auth": log.Println("Security | using basic auth") return []gin.HandlerFunc{security.HandleBasicAuth(), security.MockUser("mock", "mock", "upload", "download", "copy")} case "keycloak": log.Println("Security | using keycloak auth") return []gin.HandlerFunc{security.HandleKeycloak()} default: log.Println("Security | unsecured") return []gin.HandlerFunc{security.MockUser("mock", "mock", "upload", "download", "copy")} } } func loadExpiration() time.Duration { if value := os.Getenv("PRESIGN_DURATION"); value != "" { duration, err := time.ParseDuration(value) if err != nil { return duration } } // default duration return 1 * time.Hour } func setupLegacyEndpoints(server *gin.Engine, handlers gin.HandlersChain, resourceManager resource.Manager) { //secured endpoints group := server.Group("", handlers...) group.POST("/save", security.RequireRole("upload"), HandleLegacySave(resourceManager)) group.PUT("/copy", security.RequireRole("copy"), HandleCopy(resourceManager)) //unsecured endpoints server.GET("/get", HandleGet(resourceManager)) server.GET("/presign", HandlePresign(resourceManager)) } func setupV1Endpoints(server *gin.Engine, handlers gin.HandlersChain, resourceManager resource.Manager) { group := server.Group("", handlers...) group.POST("/api/v1/save", security.RequireRole("upload"), HandleLegacySave(resourceManager)) group.GET("/api/v1/get", security.RequireRole("download"), HandleGet(resourceManager)) group.GET("/api/v1/presign", security.RequireRole("download"), HandlePresign(resourceManager)) group.PUT("/api/v1/copy", security.RequireRole("copy"), HandleCopy(resourceManager)) group.DELETE("/api/v1/delete", security.RequireRole("upload"), HandleDelete(resourceManager)) }