Add reverse proxy for local debugging
This commit is contained in:
25
init/init.go
25
init/init.go
@@ -19,18 +19,32 @@ type PixelWebConfig struct {
|
|||||||
SessionCookieDomain string `toml:"session_cookie_domain"`
|
SessionCookieDomain string `toml:"session_cookie_domain"`
|
||||||
ResourceDir string `toml:"resource_dir"`
|
ResourceDir string `toml:"resource_dir"`
|
||||||
DebugMode bool `toml:"debug_mode"`
|
DebugMode bool `toml:"debug_mode"`
|
||||||
|
ProxyAPIRequests bool `toml:"proxy_api_requests"`
|
||||||
MaintenanceMode bool `toml:"maintenance_mode"`
|
MaintenanceMode bool `toml:"maintenance_mode"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultConfig is the default configuration for Pixeldrain Web
|
// DefaultConfig is the default configuration for Pixeldrain Web
|
||||||
const DefaultConfig = `# Pixeldrain Web UI server configuration
|
const DefaultConfig = `## Pixeldrain Web UI server configuration
|
||||||
|
|
||||||
|
# Address used in the browser for making requests directly to the API. Can be
|
||||||
|
# relative to the current domain name
|
||||||
|
api_url_external = "/api"
|
||||||
|
|
||||||
|
# Address used to make internal API requests to the backend
|
||||||
|
api_url_internal = "https://pixeldrain.com/api"
|
||||||
|
|
||||||
api_url_external = "/api" # Used in the web browser
|
|
||||||
api_url_internal = "http://127.0.0.1:8080" # Used for internal API requests to the pixeldrain server, not visible to users
|
|
||||||
website_address = "https://pixeldrain.com"
|
website_address = "https://pixeldrain.com"
|
||||||
session_cookie_domain = ".pixeldrain.com"
|
session_cookie_domain = ""
|
||||||
resource_dir = "res"
|
resource_dir = "res"
|
||||||
debug_mode = false
|
|
||||||
|
# Parse all the templates every time a request comes in
|
||||||
|
debug_mode = true
|
||||||
|
|
||||||
|
# Create proxy listeners to forward all requests made to /api to
|
||||||
|
# api_url_internal
|
||||||
|
proxy_api_requests = true
|
||||||
|
|
||||||
|
# When this is true every request will return a maintainance HTML page
|
||||||
maintenance_mode = false
|
maintenance_mode = false
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -68,5 +82,6 @@ func Init(r *httprouter.Router, prefix string, setLogLevel bool) {
|
|||||||
webconf.SessionCookieDomain,
|
webconf.SessionCookieDomain,
|
||||||
webconf.MaintenanceMode,
|
webconf.MaintenanceMode,
|
||||||
webconf.DebugMode,
|
webconf.DebugMode,
|
||||||
|
webconf.ProxyAPIRequests,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@@ -59,7 +59,7 @@ Toolbar.prototype.setStats = function() {
|
|||||||
this.spanDownloads.innerText = formatThousands(this.downloads)
|
this.spanDownloads.innerText = formatThousands(this.downloads)
|
||||||
}
|
}
|
||||||
this.statsWebsocket.onerror = (err) => {
|
this.statsWebsocket.onerror = (err) => {
|
||||||
log.error("WS error", err)
|
console.error("WS error", err)
|
||||||
this.statsWebsocket.close()
|
this.statsWebsocket.close()
|
||||||
this.statsWebsocket = null
|
this.statsWebsocket = null
|
||||||
|
|
||||||
|
@@ -19,7 +19,7 @@ import (
|
|||||||
|
|
||||||
func (wc *WebController) viewTokenOrBust() (t string) {
|
func (wc *WebController) viewTokenOrBust() (t string) {
|
||||||
var err error
|
var err error
|
||||||
if t, err = wc.api.GetMiscViewToken(); err != nil {
|
if t, err = wc.api.GetMiscViewToken(); err != nil && !wc.proxyAPIRequests {
|
||||||
log.Error("Could not get viewtoken: %s", err)
|
log.Error("Could not get viewtoken: %s", err)
|
||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
|
@@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -29,8 +31,8 @@ type WebController struct {
|
|||||||
apiURLInternal string
|
apiURLInternal string
|
||||||
apiURLExternal string
|
apiURLExternal string
|
||||||
websiteAddress string
|
websiteAddress string
|
||||||
|
|
||||||
sessionCookieDomain string
|
sessionCookieDomain string
|
||||||
|
proxyAPIRequests bool
|
||||||
|
|
||||||
// page-specific variables
|
// page-specific variables
|
||||||
captchaSiteKey string
|
captchaSiteKey string
|
||||||
@@ -55,6 +57,7 @@ func New(
|
|||||||
sessionCookieDomain string,
|
sessionCookieDomain string,
|
||||||
maintenanceMode bool,
|
maintenanceMode bool,
|
||||||
debugMode bool,
|
debugMode bool,
|
||||||
|
proxyAPIRequests bool,
|
||||||
) (wc *WebController) {
|
) (wc *WebController) {
|
||||||
var err error
|
var err error
|
||||||
wc = &WebController{
|
wc = &WebController{
|
||||||
@@ -63,6 +66,7 @@ func New(
|
|||||||
apiURLExternal: apiURLExternal,
|
apiURLExternal: apiURLExternal,
|
||||||
websiteAddress: websiteAddress,
|
websiteAddress: websiteAddress,
|
||||||
sessionCookieDomain: sessionCookieDomain,
|
sessionCookieDomain: sessionCookieDomain,
|
||||||
|
proxyAPIRequests: proxyAPIRequests,
|
||||||
httpClient: &http.Client{Timeout: time.Minute * 10},
|
httpClient: &http.Client{Timeout: time.Minute * 10},
|
||||||
api: apiclient.New(apiURLInternal),
|
api: apiclient.New(apiURLInternal),
|
||||||
}
|
}
|
||||||
@@ -96,6 +100,23 @@ func New(
|
|||||||
return wc
|
return wc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if proxyAPIRequests {
|
||||||
|
proxPath := strings.TrimSuffix(apiURLInternal, "/api")
|
||||||
|
proxURL, err := url.Parse(proxPath)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to parse reverse proxy URL '%s': %w", proxPath, err))
|
||||||
|
}
|
||||||
|
|
||||||
|
var prox = httputil.NewSingleHostReverseProxy(proxURL)
|
||||||
|
prox.Transport = wc.httpClient.Transport
|
||||||
|
r.Handler("OPTIONS", "/api/*p", prox)
|
||||||
|
r.Handler("POST", "/api/*p", prox)
|
||||||
|
r.Handler("GET", "/api/*p", prox)
|
||||||
|
r.Handler("PUT", "/api/*p", prox)
|
||||||
|
r.Handler("PATCH", "/api/*p", prox)
|
||||||
|
r.Handler("DELETE", "/api/*p", prox)
|
||||||
|
}
|
||||||
|
|
||||||
r.NotFound = http.HandlerFunc(wc.serveNotFound)
|
r.NotFound = http.HandlerFunc(wc.serveNotFound)
|
||||||
|
|
||||||
// Request method shorthands. These help keep the array of handlers aligned
|
// Request method shorthands. These help keep the array of handlers aligned
|
||||||
|
Reference in New Issue
Block a user