3ed5370439
Thnigs can go wrong and abort in server.Start() before the web server is started. This commit will move the log message to where the webserver is started to avoid confusing the user.
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/photoprism/photoprism/internal/config"
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
)
|
|
|
|
var log = event.Log
|
|
|
|
// 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 {
|
|
log.Info("web server shutdown complete")
|
|
} else {
|
|
log.Errorf("web server closed unexpect: %s", err)
|
|
}
|
|
}
|
|
}()
|
|
|
|
<-ctx.Done()
|
|
log.Info("shutting down web server")
|
|
err := server.Close()
|
|
if err != nil {
|
|
log.Errorf("web server shutdown failed: %v", err)
|
|
}
|
|
}
|