2018-07-20 14:13:52 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-06-03 22:58:15 +02:00
|
|
|
"context"
|
2018-07-20 14:13:52 +02:00
|
|
|
"fmt"
|
2019-05-04 13:29:32 +02:00
|
|
|
"net/http"
|
2022-01-05 18:15:39 +01:00
|
|
|
"time"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
2021-01-10 15:28:42 +01:00
|
|
|
"github.com/gin-contrib/gzip"
|
2018-09-06 14:47:32 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2019-12-02 00:30:58 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2018-07-20 14:13:52 +02:00
|
|
|
)
|
|
|
|
|
2019-12-02 00:30:58 +01:00
|
|
|
var log = event.Log
|
|
|
|
|
2018-11-06 19:02:03 +01:00
|
|
|
// Start the REST API server using the configuration provided
|
2019-06-03 22:58:15 +02:00
|
|
|
func Start(ctx context.Context, conf *config.Config) {
|
2020-01-21 15:12:26 +01:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Set HTTP server mode.
|
2020-12-18 09:11:42 +01:00
|
|
|
if conf.HttpMode() != "" {
|
|
|
|
gin.SetMode(conf.HttpMode())
|
2019-05-06 23:18:10 +02:00
|
|
|
} else if conf.Debug() == false {
|
2018-09-13 11:34:56 +02:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Create new HTTP router engine without standard middleware.
|
2019-05-04 13:29:32 +02:00
|
|
|
router := gin.New()
|
2021-10-17 16:48:53 +02:00
|
|
|
|
|
|
|
// Register logger middleware.
|
2020-01-21 15:12:26 +01:00
|
|
|
router.Use(Logger(), Recovery())
|
2018-07-20 14:13:52 +02:00
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Register security middleware.
|
|
|
|
router.Use(Security(SecurityOptions{
|
|
|
|
IsDevelopment: gin.Mode() != gin.ReleaseMode || conf.Test(),
|
|
|
|
AllowedHosts: []string{},
|
|
|
|
SSLRedirect: false,
|
|
|
|
SSLHost: "",
|
|
|
|
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
|
|
|
|
STSSeconds: 0,
|
|
|
|
STSIncludeSubdomains: false,
|
|
|
|
FrameDeny: true,
|
|
|
|
ContentTypeNosniff: false,
|
|
|
|
BrowserXssFilter: false,
|
|
|
|
ContentSecurityPolicy: "frame-ancestors 'none';",
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Enable HTTP compression?
|
2021-01-10 15:28:42 +01:00
|
|
|
switch conf.HttpCompression() {
|
|
|
|
case "gzip":
|
|
|
|
log.Infof("http: enabling gzip compression")
|
|
|
|
router.Use(gzip.Gzip(
|
|
|
|
gzip.DefaultCompression,
|
2021-02-07 19:04:17 +01:00
|
|
|
gzip.WithExcludedPaths([]string{
|
2021-07-05 16:41:43 +02:00
|
|
|
conf.BaseUri(config.ApiUri + "/t"),
|
|
|
|
conf.BaseUri(config.ApiUri + "/folders/t"),
|
|
|
|
conf.BaseUri(config.ApiUri + "/zip"),
|
|
|
|
conf.BaseUri(config.ApiUri + "/albums"),
|
|
|
|
conf.BaseUri(config.ApiUri + "/labels"),
|
2022-01-17 18:57:09 +01:00
|
|
|
conf.BaseUri(config.ApiUri + "/videos"),
|
2021-02-07 19:04:17 +01:00
|
|
|
})))
|
2021-01-10 15:28:42 +01:00
|
|
|
}
|
|
|
|
|
2018-09-24 11:27:46 +02:00
|
|
|
// Set template directory
|
2020-05-31 02:09:52 +02:00
|
|
|
router.LoadHTMLGlob(conf.TemplatesPath() + "/*")
|
2018-09-24 11:27:46 +02:00
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Register HTTP route handlers.
|
2019-05-06 23:18:10 +02:00
|
|
|
registerRoutes(router, conf)
|
2018-07-20 14:13:52 +02:00
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Create new HTTP server instance.
|
2019-05-04 13:29:32 +02:00
|
|
|
server := &http.Server{
|
2020-12-18 09:11:42 +01:00
|
|
|
Addr: fmt.Sprintf("%s:%d", conf.HttpHost(), conf.HttpPort()),
|
2019-05-04 13:29:32 +02:00
|
|
|
Handler: router,
|
2019-05-01 14:54:11 +02:00
|
|
|
}
|
2019-05-04 13:29:32 +02:00
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
log.Debugf("http: successfully initialized [%s]", time.Since(start))
|
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Start HTTP server.
|
2019-05-04 13:29:32 +02:00
|
|
|
go func() {
|
2021-01-10 15:28:42 +01:00
|
|
|
log.Infof("http: starting web server at %s", server.Addr)
|
2020-02-05 23:05:37 +01:00
|
|
|
|
2019-06-03 22:58:15 +02:00
|
|
|
if err := server.ListenAndServe(); err != nil {
|
|
|
|
if err == http.ErrServerClosed {
|
2021-01-10 15:28:42 +01:00
|
|
|
log.Info("http: web server shutdown complete")
|
2019-06-03 22:58:15 +02:00
|
|
|
} else {
|
2021-01-10 15:28:42 +01:00
|
|
|
log.Errorf("http: web server closed unexpect: %s", err)
|
2019-06-03 22:58:15 +02:00
|
|
|
}
|
2019-05-04 13:29:32 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-17 16:48:53 +02:00
|
|
|
// Graceful HTTP server shutdown.
|
2019-06-03 22:58:15 +02:00
|
|
|
<-ctx.Done()
|
2021-01-10 15:28:42 +01:00
|
|
|
log.Info("http: shutting down web server")
|
2019-06-03 22:58:15 +02:00
|
|
|
err := server.Close()
|
|
|
|
if err != nil {
|
2021-01-10 15:28:42 +01:00
|
|
|
log.Errorf("http: web server shutdown failed: %v", err)
|
2019-05-04 13:29:32 +02:00
|
|
|
}
|
2018-09-27 08:59:53 +02:00
|
|
|
}
|