Merge pull request 'Add MOZHI_HOST environment variable and --host/-H CLI flag' (#114) from stasadev/mozhi:mozhi_host into master
Some checks failed
mozhi pipeline / Push Docker image to Codeberg docker registry (push) Failing after 3m30s
mozhi pipeline / Build and publish artifacts (push) Successful in 1h37m41s

Reviewed-on: https://codeberg.org/aryak/mozhi/pulls/114
This commit is contained in:
2025-11-20 07:29:34 +01:00
3 changed files with 22 additions and 9 deletions

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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))
}