diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 5eca83a..e9ae1af 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -98,6 +98,11 @@ Set this option to `true` to disable this feature. Pages are converted automatically by [gmitohtml](https://code.rocketnine.space/tslocum/gmitohtml). +## StyleSheet + +Specify a custom CSS stylesheet file if you want to override the default. This only +affects the `HTTPS` protocol (obviously). + ### DisableSize The size of the response body is included in the media type header by default. @@ -240,6 +245,7 @@ hosts: gemini.rocks: cert: /srv/gemini.rocks/data/cert.crt key: /srv/gemini.rocks/data/cert.key + stylesheet: /srv/gemini.rocks/styles.css # Custom CSS stylesheet paths: - path: ^/.*\.php$ diff --git a/config.go b/config.go index 4a75b4d..7f20b18 100644 --- a/config.go +++ b/config.go @@ -66,6 +66,9 @@ type hostConfig struct { Key string Paths []*pathConfig + // Custom CSS styles. If specified, it will be used for all paths in that host/domain. + StyleSheet string + cert *tls.Certificate } @@ -84,6 +87,8 @@ type serverConfig struct { const cacheUnset = -1965 +var customCSS = make(map[string][]byte) + var config *serverConfig func readconfig(configPath string) error { @@ -210,6 +215,21 @@ func readconfig(configPath string) error { } host.cert = &cert + // Custom CSS stylesheets are precached in customCSS and used on HTTPS requests. + if host.StyleSheet != "" { + _, err := os.Stat(host.StyleSheet) + if os.IsNotExist(err) { + log.Printf("custom stylesheet '%v' not found, ignore it.", host.StyleSheet) + } else { + cssBytes, err := ioutil.ReadFile(host.StyleSheet) + if err != nil { + log.Printf("problems with stylesheet '%v': %v", host.StyleSheet, err.Error()) + } else { + customCSS[hostname] = cssBytes + } + } + } + for _, serve := range host.Paths { if serve.Path == "" { log.Fatal("a path must be specified in each serve entry") diff --git a/serve_https.go b/serve_https.go index e227064..b4c09cc 100644 --- a/serve_https.go +++ b/serve_https.go @@ -38,6 +38,14 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) { if r.Method == "HEAD" { return status, 0, "" } + + // Do we have a custom CSS file? If so, we use this instead. + if css, found := customCSS[r.URL.Hostname()]; found { + w.Write(css) + return status, int64(len(css)), "" + } + + // Default CSS w.Write(cssBytes) return status, int64(len(cssBytes)), "" }