shiori/internal/cmd/server.go
Felipe Martin cc7c75116d
refactor: migrate bookmark static pages to new http server (#775)
* migrate bookmark content route to new http server

* new archive page

* remove unused go generate comment

* database mock

* utils cleanup

* unused var

* domains refactor and tests

* fixed secret key type

* redirect to login on ui errors

* fixed archive folder with storage domain

* webroot documentation

* some bookmark route tests

* fixed error in bookmark domain for non existant bookmarks

* centralice errors

* add coverage data to unittests

* added tests, refactor storage to use afero

* removed mock to avoid increasing complexity

* using deps to copy files around

* remove config usage (to deps)

* remove handler-ui file
2023-12-28 18:18:32 +01:00

81 lines
2.3 KiB
Go

package cmd
import (
"context"
"strings"
"github.com/go-shiori/shiori/internal/http"
"github.com/go-shiori/shiori/internal/model"
"github.com/spf13/cobra"
)
func newServerCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "server",
Short: "Starts the Shiori webserver",
Long: "Serves the Shiori web interface and API.",
Run: newServerCommandHandler(),
}
cmd.Flags().IntP("port", "p", 8080, "Port used by the server")
cmd.Flags().StringP("address", "a", "", "Address the server listens to")
cmd.Flags().StringP("webroot", "r", "/", "Root path that used by server")
cmd.Flags().Bool("access-log", false, "Print out a non-standard access log")
cmd.Flags().Bool("serve-web-ui", true, "Serve static files from the webroot path")
cmd.Flags().String("secret-key", "", "Secret key used for encrypting session data")
return cmd
}
func newServerCommandHandler() func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
ctx := context.Background()
// Get flags values
port, _ := cmd.Flags().GetInt("port")
address, _ := cmd.Flags().GetString("address")
rootPath, _ := cmd.Flags().GetString("webroot")
accessLog, _ := cmd.Flags().GetBool("access-log")
serveWebUI, _ := cmd.Flags().GetBool("serve-web-ui")
secretKey, _ := cmd.Flags().GetBytesHex("secret-key")
cfg, dependencies := initShiori(ctx, cmd)
cfg.Http.SetDefaults(dependencies.Log)
// Validate root path
if rootPath == "" {
rootPath = "/"
}
if !strings.HasPrefix(rootPath, "/") {
rootPath = "/" + rootPath
}
if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}
// Override configuration from flags
cfg.Http.Port = port
cfg.Http.Address = address + ":"
cfg.Http.RootPath = rootPath
cfg.Http.AccessLog = accessLog
cfg.Http.ServeWebUI = serveWebUI
cfg.Http.SecretKey = secretKey
dependencies.Log.Infof("Starting Shiori v%s", model.BuildVersion)
server, err := http.NewHttpServer(dependencies.Log).Setup(cfg, dependencies)
if err != nil {
dependencies.Log.WithError(err).Fatal("error setting up server")
}
if err := server.Start(ctx); err != nil {
dependencies.Log.WithError(err).Fatal("error starting server")
}
dependencies.Log.WithField("addr", address).Debug("started http server")
server.WaitStop(ctx)
}
}