diff --git a/README.md b/README.md index 18626cc..72e67c8 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ Reverso sometimes blocks IPs of servers hosting mozhi, and since it doesn't have ## Configuration Features of Mozhi can be customized and toggled on/off using Environment Variables. -- `MOZHI_PORT`: Port the webserver listens on (if hosting API) +- `MOZHI_HOST`: Host address the webserver listens on (if hosting API). Defaults to listening on all interfaces +- `MOZHI_PORT`: Port the webserver listens on (if hosting API). Defaults to `3000` - `MOZHI_LIBRETRANSLATE_URL`: URL of Libretranslate instance (Example: `MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt`) - `MOZHI_DEFAULT_SOURCE_LANG`: Language to default to if no source language is set by user. Defaults to Auto-Detect (or first available language in engines which dont support it) - `MOZHI_DEFAULT_PREFER_AUTODETECT`: Prefer autodetect if available instead of specified/default source language. Defaults to false diff --git a/cmd/serve.go b/cmd/serve.go index b0f28f6..099d676 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -6,6 +6,7 @@ import ( "codeberg.org/aryak/mozhi/serve" ) +var host string = "" var port string = "3000" var serveCmd = &cobra.Command{ @@ -13,15 +14,17 @@ var serveCmd = &cobra.Command{ Short: "Start the web server.", Long: `Start the web server.`, Run: func(cmd *cobra.Command, args []string) { - serve.Serve(port) + serve.Serve(host, port) }, } func init() { rootCmd.AddCommand(serveCmd) - serveCmd.Flags().StringVarP(&port, "port", "p", "", "The port Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_PORT environment variable.") + serveCmd.Flags().StringVarP(&host, "host", "H", "", "The host Mozhi will listen on. Defaults to listening on all interfaces, and overrides the MOZHI_HOST environment variable.") + serveCmd.Flags().StringVarP(&port, "port", "p", "", "The port Mozhi will listen on. Defaults to 3000, and overrides the MOZHI_PORT environment variable.") - // set port variable to the value of the port flag + // set variables to the value of the flags + host = serveCmd.Flag("host").Value.String() port = serveCmd.Flag("port").Value.String() } diff --git a/serve/serve.go b/serve/serve.go index d76f4b9..28b0d69 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -34,7 +34,7 @@ import ( // @license.name AGPL 3.0 // @license.url https://www.gnu.org/licenses/agpl-3.0.txt // @BasePath /api -func Serve(port string) { +func Serve(host, port string) { views := http.FS(views.GetFiles()) engine := html.NewFileSystem(views, ".html") @@ -137,12 +137,21 @@ func Serve(port string) { Root: http.FS(public.GetFiles()), })) - val, ok := os.LookupEnv("MOZHI_PORT") + hostVal, ok := os.LookupEnv("MOZHI_HOST") if !ok { - val = "3000" + hostVal = "" + } + if host != "" { + hostVal = host + } + + portVal, ok := os.LookupEnv("MOZHI_PORT") + if !ok { + portVal = "3000" } if port != "" { - val = port + portVal = port } - log.Fatal(app.Listen(":" + val)) + + log.Fatal(app.Listen(hostVal + ":" + portVal)) }