package server
import (
"fmt"
"net/http"
"github.com/bestruirui/octopus/internal/conf"
"github.com/bestruirui/octopus/internal/relay/bodycache"
_ "github.com/bestruirui/octopus/internal/server/handlers"
"github.com/bestruirui/octopus/internal/server/middleware"
"github.com/bestruirui/octopus/internal/server/resp"
"github.com/bestruirui/octopus/internal/server/router"
"github.com/bestruirui/octopus/internal/utils/log"
"github.com/bestruirui/octopus/static"
"github.com/gin-gonic/gin"
)
var httpSrv http.Server
func Start() error {
if conf.IsDebug() {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
tmpDir := bodycache.TmpDirFromEnv()
olderThan := bodycache.TmpCleanupOlderThanFromEnv()
if err := bodycache.CleanupOldTmpFiles(tmpDir, bodycache.TmpFilePrefix, olderThan); err != nil {
log.Warnf("cleanup images tmp files failed: dir=%s prefix=%s olderThan=%s err=%v", tmpDir, bodycache.TmpFilePrefix, olderThan, err)
}
r := gin.New()
r.Use(gin.CustomRecovery(func(c *gin.Context, recovered interface{}) {
resp.Error(c, http.StatusInternalServerError, resp.ErrInternalServer)
c.Abort()
}))
if conf.IsDebug() {
r.Use(middleware.Logger())
}
r.Use(middleware.Cors())
r.Use(middleware.StaticEmbed("/", static.StaticFS))
router.RegisterAll(r)
httpSrv.Addr = fmt.Sprintf("%s:%d", conf.AppConfig.Server.Host, conf.AppConfig.Server.Port)
httpSrv.Handler = r
go func() {
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Errorf("http server listen and serve error: %v", err)
}
}()
return nil
}
func Close() error {
return httpSrv.Close()
}