2019-05-21 11:31:40 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2019-10-07 14:38:40 +08:00
|
|
|
"strings"
|
|
|
|
|
2019-05-27 18:01:53 +08:00
|
|
|
"github.com/go-shiori/shiori/internal/webserver"
|
|
|
|
"github.com/sirupsen/logrus"
|
2019-05-21 11:31:40 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serveCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "serve",
|
|
|
|
Short: "Serve web interface for managing bookmarks",
|
2019-09-23 15:43:29 +08:00
|
|
|
Long: "Run a simple and performant web server which " +
|
2019-05-21 11:31:40 +08:00
|
|
|
"serves the site for managing bookmarks. If --port " +
|
|
|
|
"flag is not used, it will use port 8080 by default.",
|
2019-05-27 18:01:53 +08:00
|
|
|
Run: serveHandler,
|
2019-05-21 11:31:40 +08:00
|
|
|
}
|
|
|
|
|
2019-09-23 15:43:29 +08:00
|
|
|
cmd.Flags().IntP("port", "p", 8080, "Port used by the server")
|
2019-08-27 13:36:24 +08:00
|
|
|
cmd.Flags().StringP("address", "a", "", "Address the server listens to")
|
2019-10-07 14:38:40 +08:00
|
|
|
cmd.Flags().StringP("webroot", "r", "/", "Root path that used by server")
|
2019-05-21 11:31:40 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2019-05-27 18:01:53 +08:00
|
|
|
|
|
|
|
func serveHandler(cmd *cobra.Command, args []string) {
|
2019-10-07 14:38:40 +08:00
|
|
|
// Get flags value
|
2019-05-27 18:01:53 +08:00
|
|
|
port, _ := cmd.Flags().GetInt("port")
|
2019-08-27 13:36:24 +08:00
|
|
|
address, _ := cmd.Flags().GetString("address")
|
2019-10-07 14:38:40 +08:00
|
|
|
rootPath, _ := cmd.Flags().GetString("webroot")
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
2019-05-27 18:01:53 +08:00
|
|
|
|
2019-10-07 14:38:40 +08:00
|
|
|
err := webserver.ServeApp(serverConfig)
|
2019-05-27 18:01:53 +08:00
|
|
|
if err != nil {
|
|
|
|
logrus.Fatalf("Server error: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|