2024-01-03 19:56:42 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-01-03 20:25:15 +00:00
|
|
|
"resource-manager/domain/cache"
|
|
|
|
"resource-manager/domain/resource"
|
2024-01-03 19:56:42 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetupServer() *gin.Engine {
|
|
|
|
server := createServer()
|
|
|
|
RegisterRoutes(server)
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
|
|
|
|
func createServer() *gin.Engine {
|
|
|
|
server := gin.New()
|
|
|
|
server.NoRoute(NoRoute())
|
|
|
|
server.NoMethod(NoMethod())
|
2024-01-04 20:48:05 +00:00
|
|
|
server.Use(gin.Recovery(), Auth())
|
2024-01-03 19:56:42 +00:00
|
|
|
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)
|
|
|
|
|
2024-01-04 20:48:05 +00:00
|
|
|
v1Group := server.Group("/api/v1/resources", Secure("basic", "api"))
|
|
|
|
|
|
|
|
v1Group.POST("", HandleUpload(resourceManager))
|
|
|
|
v1Group.GET("", HandleDownload(resourceManager))
|
|
|
|
v1Group.GET("presign", HandlePresign(resourceManager))
|
|
|
|
v1Group.PUT("copy", HandleCopy(resourceManager))
|
|
|
|
v1Group.DELETE("", HandleDelete(resourceManager))
|
2024-01-03 19:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|