54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"log"
|
|
"os"
|
|
"resource-manager/domain/cache"
|
|
"resource-manager/domain/resource"
|
|
"resource-manager/security"
|
|
"time"
|
|
)
|
|
|
|
func SetupServer() *gin.Engine {
|
|
server := createServer()
|
|
RegisterRoutes(server)
|
|
return server
|
|
}
|
|
|
|
func createServer() *gin.Engine {
|
|
server := gin.New()
|
|
server.NoRoute(NoRoute())
|
|
server.NoMethod(NoMethod())
|
|
server.Use(gin.Recovery(), Auth())
|
|
return server
|
|
}
|
|
|
|
func RegisterRoutes(server *gin.Engine) {
|
|
cacheManager := cache.NewManager()
|
|
expiration := loadExpiration()
|
|
|
|
log.Println("Presign | expiration set to " + expiration.String())
|
|
|
|
resourceManager := resource.NewManager(cacheManager, expiration)
|
|
|
|
resourcesGroup := server.Group("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi))
|
|
resourcesGroup.POST("", HandleUpload(resourceManager))
|
|
resourcesGroup.GET("presign", HandlePresign(resourceManager))
|
|
resourcesGroup.PUT("copy", HandleCopy(resourceManager))
|
|
resourcesGroup.DELETE("", HandleDelete(resourceManager))
|
|
|
|
server.GET("/api/v1/resources", Secure(security.TypeBasic, security.TypeApi, security.TypeToken), HandleDownload(resourceManager))
|
|
}
|
|
|
|
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
|
|
}
|