51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
|
package api
|
||
|
|
||
|
import (
|
||
|
"git.bbr-dev.info/brajkovic/resource_manager/domain/cache"
|
||
|
"git.bbr-dev.info/brajkovic/resource_manager/domain/resource"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"log"
|
||
|
"os"
|
||
|
"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())
|
||
|
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)
|
||
|
|
||
|
server.POST("/api/v1/upload", HandleUpload(resourceManager))
|
||
|
server.GET("/api/v1/download", HandleDownload(resourceManager))
|
||
|
server.GET("/api/v1/presign", HandlePresign(resourceManager))
|
||
|
server.PUT("/api/v1/copy", HandleCopy(resourceManager))
|
||
|
server.DELETE("/api/v1/delete", HandleDelete(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
|
||
|
}
|