2022-07-07 00:19:05 +03:00
package cmd
import (
2024-01-23 20:56:14 +02:00
"errors"
2022-07-07 00:19:05 +03:00
"net/http"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"
)
// NewServeCommand creates and returns new command responsible for
// starting the default PocketBase web server.
func NewServeCommand ( app core . App , showStartBanner bool ) * cobra . Command {
var allowedOrigins [ ] string
var httpAddr string
var httpsAddr string
2024-09-29 19:23:19 +03:00
var dashboardPath string
2022-07-07 00:19:05 +03:00
command := & cobra . Command {
2024-01-24 11:06:49 +02:00
Use : "serve [domain(s)]" ,
Args : cobra . ArbitraryArgs ,
Short : "Starts the web server (default to 127.0.0.1:8090 if no domain is specified)" ,
SilenceUsage : true ,
RunE : func ( command * cobra . Command , args [ ] string ) error {
2023-08-25 11:16:31 +03:00
// set default listener addresses if at least one domain is specified
if len ( args ) > 0 {
if httpAddr == "" {
httpAddr = "0.0.0.0:80"
}
if httpsAddr == "" {
httpsAddr = "0.0.0.0:443"
}
2023-08-25 12:06:24 +03:00
} else {
if httpAddr == "" {
httpAddr = "127.0.0.1:8090"
}
2023-08-25 11:16:31 +03:00
}
2024-09-29 19:23:19 +03:00
err := apis . Serve ( app , apis . ServeConfig {
2023-08-25 11:16:31 +03:00
HttpAddr : httpAddr ,
HttpsAddr : httpsAddr ,
2024-09-29 19:23:19 +03:00
DashboardPath : dashboardPath ,
2023-08-25 11:16:31 +03:00
ShowStartBanner : showStartBanner ,
AllowedOrigins : allowedOrigins ,
CertificateDomains : args ,
2023-04-20 05:05:41 +03:00
} )
2023-02-12 12:41:48 +02:00
2024-01-24 11:06:49 +02:00
if errors . Is ( err , http . ErrServerClosed ) {
return nil
2022-07-07 00:19:05 +03:00
}
2024-01-24 11:06:49 +02:00
return err
2022-07-07 00:19:05 +03:00
} ,
}
command . PersistentFlags ( ) . StringSliceVar (
& allowedOrigins ,
"origins" ,
[ ] string { "*" } ,
"CORS allowed domain origins list" ,
)
command . PersistentFlags ( ) . StringVar (
& httpAddr ,
"http" ,
2023-08-25 11:16:31 +03:00
"" ,
"TCP address to listen for the HTTP server\n(if domain args are specified - default to 0.0.0.0:80, otherwise - default to 127.0.0.1:8090)" ,
2022-07-07 00:19:05 +03:00
)
command . PersistentFlags ( ) . StringVar (
& httpsAddr ,
"https" ,
"" ,
2023-08-25 11:16:31 +03:00
"TCP address to listen for the HTTPS server\n(if domain args are specified - default to 0.0.0.0:443, otherwise - default to empty string, aka. no TLS)\nThe incoming HTTP traffic also will be auto redirected to the HTTPS version" ,
2022-07-07 00:19:05 +03:00
)
2024-09-29 19:23:19 +03:00
command . PersistentFlags ( ) . StringVar (
& dashboardPath ,
"dashboard" ,
"/_/{path...}" ,
"The route path to the superusers dashboard; must include the '{path...}' wildcard parameter" ,
)
2022-07-07 00:19:05 +03:00
return command
}