2023-04-17 05:00:34 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2023-04-17 08:48:09 +03:00
|
|
|
"gitstore.ru/tolikproh/sirius/internal/controller/handler"
|
2023-04-17 05:00:34 +03:00
|
|
|
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
httpServer *http.Server
|
|
|
|
}
|
|
|
|
|
2023-04-17 08:48:09 +03:00
|
|
|
func NewApp(port string) *App {
|
2023-04-17 05:00:34 +03:00
|
|
|
// Initiate an S3 compatible client
|
|
|
|
|
|
|
|
return &App{
|
2023-04-17 08:48:09 +03:00
|
|
|
httpServer: &http.Server{
|
|
|
|
Addr: ":" + port,
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}}
|
2023-04-17 05:00:34 +03:00
|
|
|
}
|
|
|
|
|
2023-04-17 08:48:09 +03:00
|
|
|
func (a *App) Run() error {
|
2023-04-17 05:00:34 +03:00
|
|
|
// Init gin handler
|
|
|
|
router := gin.Default()
|
|
|
|
|
|
|
|
router.Use(
|
|
|
|
gin.Recovery(),
|
|
|
|
gin.Logger(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// API endpoints
|
|
|
|
api := router.Group("/api")
|
2023-04-17 08:48:09 +03:00
|
|
|
api.POST("/upload", handler.Upload)
|
2023-04-17 05:00:34 +03:00
|
|
|
|
|
|
|
// HTTP Server
|
2023-04-17 08:48:09 +03:00
|
|
|
a.httpServer.Handler = router
|
2023-04-17 05:00:34 +03:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := a.httpServer.ListenAndServe(); err != nil {
|
|
|
|
log.Fatalf("Failed to listen and serve: %+v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt, os.Interrupt)
|
|
|
|
|
|
|
|
<-quit
|
|
|
|
|
|
|
|
ctx, shutdown := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer shutdown()
|
|
|
|
|
|
|
|
return a.httpServer.Shutdown(ctx)
|
|
|
|
}
|