photoprism/internal/server/server.go

61 lines
1.2 KiB
Go
Raw Normal View History

package server
import (
"context"
"fmt"
"net/http"
2018-09-06 14:47:32 +02:00
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/event"
)
var log = event.Log
2018-11-06 19:02:03 +01:00
// Start the REST API server using the configuration provided
func Start(ctx context.Context, conf *config.Config) {
defer func() {
if err := recover(); err != nil {
log.Error(err)
}
}()
if conf.HttpServerMode() != "" {
gin.SetMode(conf.HttpServerMode())
} else if conf.Debug() == false {
gin.SetMode(gin.ReleaseMode)
}
router := gin.New()
router.Use(Logger(), Recovery())
// Set template directory
router.LoadHTMLGlob(conf.HttpTemplatesPath() + "/*")
registerRoutes(router, conf)
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", conf.HttpServerHost(), conf.HttpServerPort()),
Handler: router,
}
go func() {
log.Infof("starting web server at %s", server.Addr)
if err := server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
2019-06-04 00:22:25 +02:00
log.Info("web server shutdown complete")
} else {
log.Errorf("web server closed unexpect: %s", err)
}
}
}()
<-ctx.Done()
2019-06-04 00:22:25 +02:00
log.Info("shutting down web server")
err := server.Close()
if err != nil {
2019-06-04 00:22:25 +02:00
log.Errorf("web server shutdown failed: %v", err)
}
}