@@ -33,16 +33,18 @@ const maxPageSize = 100
3333
3434const envAppToken = "APP_TOKEN"
3535const envDBPath = "DB_PATH"
36- const envPort = "PORT"
36+ const envPort = "API_PORT"
37+ const envAPIBaseURL = "API_URL"
3738const envWebUI = "WEB_UI"
39+ const envWebUIPath = "WEB_UI_URL"
3840const envWebUIPort = "WEB_UI_PORT"
3941
4042const envRybbitSiteID = "RYBBIT_SITE_ID"
4143const envRybbitSiteKey = "RYBBIT_SITE_KEY"
4244const envRybbitSiteURL = "RYBBIT_SITE_URL"
4345
4446const defaultAPIPort = "8080"
45- const defaultWebUIPort = "8080 "
47+ const defaultWebUIPort = "8090 "
4648const defaultDBPath = "short-it.db"
4749
4850const webUIPage = `<!doctype html>
@@ -118,6 +120,34 @@ func envOrDefault(key, fallback string) string {
118120 return v
119121}
120122
123+ func normalizeBaseURL (raw string ) string {
124+ return strings .TrimRight (strings .TrimSpace (raw ), "/" )
125+ }
126+
127+ func resolveAPIBaseURL (r * http.Request ) string {
128+ if base := normalizeBaseURL (os .Getenv (envAPIBaseURL )); base != "" {
129+ return base
130+ }
131+
132+ host := r .Host
133+ if h , _ , err := net .SplitHostPort (r .Host ); err == nil {
134+ host = h
135+ }
136+ if host == "" {
137+ host = "localhost"
138+ }
139+
140+ return fmt .Sprintf ("http://%s:%s" , host , envOrDefault (envPort , defaultAPIPort ))
141+ }
142+
143+ func resolveWebUIBaseURL (webUIPort string ) string {
144+ if base := normalizeBaseURL (os .Getenv (envWebUIPath )); base != "" {
145+ return base
146+ }
147+
148+ return fmt .Sprintf ("http://localhost:%s" , webUIPort )
149+ }
150+
121151func writeJSON (w http.ResponseWriter , status int , body any ) {
122152 w .Header ().Set ("Content-Type" , "application/json" )
123153 w .WriteHeader (status )
@@ -528,17 +558,7 @@ func handleWebUICreate(w http.ResponseWriter, r *http.Request) {
528558 return
529559 }
530560
531- host := r .Host
532- if h , _ , err := net .SplitHostPort (r .Host ); err == nil {
533- host = h
534- }
535- if host == "" {
536- host = "localhost"
537- }
538-
539- apiPort := envOrDefault (envPort , defaultAPIPort )
540-
541- shortURL := fmt .Sprintf ("http://%s:%s/%s" , host , apiPort , key )
561+ shortURL := fmt .Sprintf ("%s/%s" , resolveAPIBaseURL (r ), key )
542562
543563 writeJSON (w , http .StatusOK , map [string ]string {
544564 "key" : key ,
@@ -584,15 +604,19 @@ func main() {
584604 webUIMux .HandleFunc ("/api/create" , handleWebUICreate )
585605
586606 go func () {
587- log .Printf ("[web-ui] listening on :%s" , webUIPort )
607+ log .Printf ("[web-ui] listening on :%s (public %s) " , webUIPort , resolveWebUIBaseURL ( webUIPort ) )
588608 if err := http .ListenAndServe (":" + webUIPort , webUIMux ); err != nil {
589609 log .Printf ("[web-ui] server stopped: %v" , err )
590610 }
591611 }()
592612 }
593613 }
594614
595- log .Printf ("[api] listening on :%s" , port )
615+ apiBase := normalizeBaseURL (os .Getenv (envAPIBaseURL ))
616+ if apiBase == "" {
617+ apiBase = fmt .Sprintf ("http://localhost:%s" , port )
618+ }
619+ log .Printf ("[api] listening on :%s (public %s)" , port , apiBase )
596620 log .Fatal (http .ListenAndServe (":" + port , apiMux ))
597621}
598622
0 commit comments