Files
upstream-centrifugo/internal/cli/serve.go
upstream-mirror b352a9a30d
Some checks failed
test / Lint (push) Failing after 3s
test / Test with Go 1.26.8 (push) Failing after 2s
snapshot of https://github.com/centrifugal/centrifugo.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:17:21 +05:00

39 lines
1.1 KiB
Go

package cli
import (
"fmt"
"net"
"net/http"
"os"
"strconv"
"github.com/spf13/cobra"
)
func Serve() *cobra.Command {
var serveDir string
var servePort int
var serveAddr string
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Run static file server (for development only)",
Long: `Run static file server (for development only)`,
Run: func(cmd *cobra.Command, args []string) {
serve(serveAddr, servePort, serveDir)
},
}
serveCmd.Flags().StringVarP(&serveDir, "dir", "d", "./", "path to directory")
serveCmd.Flags().IntVarP(&servePort, "port", "p", 3000, "port to serve on")
serveCmd.Flags().StringVarP(&serveAddr, "address", "a", "", "interface to serve on (default: all interfaces)")
return serveCmd
}
func serve(serveAddr string, servePort int, serveDir string) {
address := net.JoinHostPort(serveAddr, strconv.Itoa(servePort))
fmt.Printf("start serving %s on %s\n", serveDir, address)
if err := http.ListenAndServe(address, http.FileServer(http.Dir(serveDir))); err != nil { //nolint:gosec // Only for development use.
fmt.Println(err)
os.Exit(1)
}
}