websrv

package
v0.0.0-...-642df0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Req: go 1.16 or later (embed.FS is N/A on Go 1.15 or lower)

Index

Constants

View Source
const (
	VERSION   string = "r.20240114.2007"
	SIGNATURE string = "(c) 2020-2024 unix-world.org"

	SERVER_ADDR string = "127.0.0.1"
	SERVER_PORT uint16 = 17788

	WEB_PUBLIC_RELATIVE_ROOT_PATH string = "./web-public/"
	DEFAULT_DIRECTORY_INDEX_HTML  string = "index.html"

	DAV_PUBLIC_SAFETY_FILE         string = "./webdav-allow-public-no-auth"
	DAV_STORAGE_RELATIVE_ROOT_PATH string = "./webdav" // do not add trailing slash
	DAV_URL_PATH                   string = "webdav"

	CACHED_EXP_TIME_SECONDS uint32 = assets.CACHED_EXP_TIME_SECONDS * 3 // 24h

	CERTIFICATES_DEFAULT_PATH string = "./ssl/"
	CERTIFICATE_PEM_CRT       string = "cert.crt"
	CERTIFICATE_PEM_KEY       string = "cert.key"

	HTTP_AUTH_REALM string = "Smart.Web Server: Auth Area"

	REAL_IP_HEADER_KEY = "" // if used behind a proxy, can be set as: X-REAL-IP, X-FORWARDED-FOR, HTTP-X-CLIENT-IP, ... or any other trusted proxy header ; if no proxy is used, set as an empty string

	DEBUG bool = false
)
View Source
const (
	REGEX_SAFE_WEB_ROUTE string = `^[_a-zA-Z0-9\-\.@,;\!\/]+$` // SAFETY: SUPPORT ONLY THESE CHARACTERS IN WEB ROUTES ... ; w3s alike ; must support everything in REGEX_SMART_SAFE_FILE_NAME except: #, includding A-Z which on register route may be disallowed
)
View Source
const (
	REGEX_SESS_UUID_COOKIE_VALID_VALUE string = `^[A-Za-z0-9]+` // B62
)
View Source
const TheStrSignature string = "SmartGO Web Server " + VERSION

Variables

View Source
var AllowedMethods []string = []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE"}

Functions

func GetBasePath

func GetBasePath() string

func GetCurrentYear

func GetCurrentYear() string

func GetUuidCookieName

func GetUuidCookieName() string

func GetUuidCookieValue

func GetUuidCookieValue(r *http.Request) string

func JsonValidateWithSchema

func JsonValidateWithSchema(schema string, json string) error

func RequestHaveQueryString

func RequestHaveQueryString(r *http.Request) bool

func UrlHandlerRegisterRoute

func UrlHandlerRegisterRoute(route string, skipAuth bool, methods []string, maxTailSegments int, fxHandler HttpHandlerFunc) bool

func UrlHandlerUnRegisterRoute

func UrlHandlerUnRegisterRoute(route string) bool

func WebServerRun

func WebServerRun(servePublicPath bool, webdavOptions *WebdavRunOptions, serveSecure bool, certifPath string, httpAddr string, httpPort uint16, timeoutSeconds uint32, allowedIPs string, authUser string, authPass string, customAuthCheck smarthttputils.HttpAuthCheckFunc, rateLimit int, rateBurst int) int16

IMPORTANT: If using Proxy with different PROXY_HTTP_BASE_PATH than "/" (ex: "/api/") the Proxy MUST strip back PROXY_HTTP_BASE_PATH to "/" for this backend

Types

type HttpHandlerFunc

type HttpHandlerFunc func(r *http.Request, headPath string, tailPaths []string, authData smart.AuthDataStruct) (code uint16, content string, contentFileName string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string)
var RouteHandlerInfoPage HttpHandlerFunc = func(r *http.Request, headPath string, tailPaths []string, authData smart.AuthDataStruct) (code uint16, content string, contentFileName string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string) {

	defer smart.PanicHandler()
	remoteAddr, remotePort := smart.GetHttpRemoteAddrIpAndPortFromRequest(r)
	_, realClientIp, _, _ := smart.GetHttpRealClientIpFromRequestHeaders(r)
	dom, port, _ := smart.GetHttpDomainAndPortFromRequest(r)
	baseDom, _ := smart.GetBaseDomainFromDomain(dom)
	var proxyRealIpKey string = smart.GetHttpProxyRealClientIpHeaderKey()
	if proxyRealIpKey == "" {
		proxyRealIpKey = "No:Proxy"
	} else {
		proxyRealIpKey = "Proxy:" + proxyRealIpKey
	}
	code = 200
	var headHtml string = ""
	var bodyHtml string = "<h1>" + "Server Info" + "</h1>" + "<h4>" + smart.StrNl2Br(smart.EscapeHtml(TheStrSignature)) + "</h4>"
	bodyHtml += "Visitor Real-IP [" + smart.EscapeHtml(proxyRealIpKey) + "] is: <b>`" + smart.EscapeHtml(realClientIp) + "`</b> ; Remote-IP (Host:Port) is: " + smart.EscapeHtml("`"+remoteAddr+"`:`"+remotePort+"`") + "<br>"
	bodyHtml += "Visitor UserAgent: <i>`" + smart.EscapeHtml(smart.GetHttpUserAgentFromRequest(r)) + "`</i>" + "<br>"
	bodyHtml += "Server Protocol: <b>`" + smart.EscapeHtml(smart.GetHttpProtocolFromRequest(r)) + "`</b>" + "<br>"
	bodyHtml += "Server BaseDomain: `" + smart.EscapeHtml(baseDom) + "`" + "<br>"
	bodyHtml += "Server Domain: <b>`" + smart.EscapeHtml(dom) + "`</b>" + "<br>"
	bodyHtml += "Server Port: `" + smart.EscapeHtml(port) + "`" + "<br>"
	bodyHtml += "Server Path: <b>`" + smart.EscapeHtml(smart.GetHttpBrowserPathFromRequest(r)) + "`</b>" + " ; Served GO Path: `" + smart.EscapeHtml(smart.GetHttpPathFromRequest(r)) + "`" + "<br>"
	bodyHtml += "Server QueryString: `" + smart.EscapeHtml(smart.GetHttpQueryStringFromRequest(r)) + "`" + "<br>"

	bodyHtml += `<hr>`
	bodyHtml += `<i class="sfi sfi-info sfi-3x"></i>`
	bodyHtml += `<img src="lib/framework/img/golang-logo.svg" height="64" style="margin-top:-24px; margin-left:12px;">`
	bodyHtml += "<hr>"
	bodyHtml += `<div style="font-size:0.75rem; color:#CCCCDD; text-align:right;">&copy; 2023-` + smart.EscapeHtml(GetCurrentYear()) + ` unix-world.org</div>`
	content = srvassets.HtmlServerTemplate(TheStrSignature, headHtml, bodyHtml)
	contentFileName = "index.html"

	contentDisposition = ""
	cacheExpiration = -1
	cacheLastModified = ""
	cacheControl = smarthttputils.CACHE_CONTROL_NOCACHE
	headers = map[string]string{}
	headers["Z-Srv-Info"] = TheStrSignature

	return
}

-- info page (html)

var RouteHandlerStatusPage HttpHandlerFunc = func(r *http.Request, headPath string, tailPaths []string, authData smart.AuthDataStruct) (code uint16, content string, contentFileName string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string) {

	defer smart.PanicHandler()
	code = 202
	var headHtml string = "<style>" + "\n" + "div.status { text-align:center; margin:10px; cursor:help; }" + "\n" + "div.signature { background:#778899; color:#FFFFFF; font-size:2rem; font-weight:bold; text-align:center; border-radius:3px; padding:10px; margin:20px; }" + "\n" + "</style>"
	var bodyHtml string = `<div class="status"><img alt="status:svg" title="Service Status: Up and Running ..." width="48" height="48" src="data:image/svg+xml,` + smart.EscapeHtml(smart.EscapeUrl(assets.ReadWebAsset("lib/framework/img/loading-spin.svg"))) + `"></div>` + "\n" + `<div class="signature">` + "\n" + "<pre>" + "\n" + smart.EscapeHtml(TheStrSignature) + "</pre>" + "\n" + "</div>"
	content = assets.HtmlStandaloneTemplate(TheStrSignature, headHtml, bodyHtml)
	contentFileName = "status.html"

	return
}

-- status page (html)

var RouteHandlerVersionPage HttpHandlerFunc = func(r *http.Request, headPath string, tailPaths []string, authData smart.AuthDataStruct) (code uint16, content string, contentFileName string, contentDisposition string, cacheExpiration int, cacheLastModified string, cacheControl string, headers map[string]string) {

	defer smart.PanicHandler()
	code = 203
	ver := versionStruct{Version: TheStrSignature, Copyright: SIGNATURE}
	content = smart.JsonNoErrChkEncode(ver, false, false)
	contentFileName = "version.json"

	return
}

-- version page (json)

type WebdavRunOptions

type WebdavRunOptions struct {
	Enabled        bool
	SharedMode     bool
	SmartSafePaths bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL