shiori/internal/cmd/serve.go
Emmanuel Frecon e1e2c7bfd8
Web Server and CI Improvements (#374)
* Fix for infinite redirect loop

path.Join trims the trailing slash if the path isn't /, use configured
root instead.

* Add repo root independence

This makes the workflows agnostic of the repository root, making it
possible to build in forked repos.

* Add HTTP request logging

* Fix proper RootPath handler

* Add logging for all resources

This adds proper logging for all resources, including errors. Logging
is on by default, but can be turned off.

* Report effective length of written data
2022-02-19 08:22:50 +01:00

63 lines
1.5 KiB
Go

package cmd
import (
"strings"
"github.com/go-shiori/shiori/internal/webserver"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func serveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "serve",
Short: "Serve web interface for managing bookmarks",
Long: "Run a simple and performant web server which " +
"serves the site for managing bookmarks. If --port " +
"flag is not used, it will use port 8080 by default.",
Run: serveHandler,
}
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("log", true, "Print out a non-standard access log")
return cmd
}
func serveHandler(cmd *cobra.Command, args []string) {
// Get flags value
port, _ := cmd.Flags().GetInt("port")
address, _ := cmd.Flags().GetString("address")
rootPath, _ := cmd.Flags().GetString("webroot")
log, _ := cmd.Flags().GetBool("log")
// Validate root path
if rootPath == "" {
rootPath = "/"
}
if !strings.HasPrefix(rootPath, "/") {
rootPath = "/" + rootPath
}
if !strings.HasSuffix(rootPath, "/") {
rootPath += "/"
}
// Start server
serverConfig := webserver.Config{
DB: db,
DataDir: dataDir,
ServerAddress: address,
ServerPort: port,
RootPath: rootPath,
Log: log,
}
err := webserver.ServeApp(serverConfig)
if err != nil {
logrus.Fatalf("Server error: %v\n", err)
}
}