From 83453281bc1211ca432753f47ef541c89c722623 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 7 Jun 2021 23:52:49 +0200 Subject: [PATCH] Add support for custom CSS files Specify the ```styles``` config option for a host and point to a valid CSS file to use this instead of the default one. This fixes #9. --- CONFIGURATION.md | 6 ++++++ config.go | 3 +++ serve_https.go | 17 +++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 46c1987..483a31e 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -81,6 +81,11 @@ Set this option to `true` to disable this feature. Pages are converted automatically by [gmitohtml](https://code.rocketnine.space/tslocum/gmitohtml). +## Styles + +Specify a custom CSS styles 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. @@ -223,6 +228,7 @@ hosts: gemini.rocks: cert: /srv/gemini.rocks/data/cert.crt key: /srv/gemini.rocks/data/cert.key + styles: /srv/gemini.rocks/styles.css # Custom CSS styles paths: - path: ^/.*\.php$ diff --git a/config.go b/config.go index 4a75b4d..0b757a1 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. + Styles string + cert *tls.Certificate } diff --git a/serve_https.go b/serve_https.go index e227064..6258910 100644 --- a/serve_https.go +++ b/serve_https.go @@ -31,6 +31,22 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) { http.Redirect(w, r, u.String(), status) return status, -1, "" } else if r.URL.Path == "/assets/style.css" { + // Do we have a custom CSS file? If so, we use this instead. + styleFilepath := config.Hosts[r.URL.Hostname()].Styles + if styleFilepath != "" { + _, err := os.Stat(styleFilepath) + if os.IsNotExist(err) { + http.Error(w, "Custom styles not found", http.StatusNotFound) + return http.StatusNotFound, -1, "" + } + + cssBytes, err = ioutil.ReadFile(styleFilepath) + if err != nil { + http.Error(w, "Cannot process custom styles", http.StatusInternalServerError) + return http.StatusInternalServerError, -1, "" + } + } + status := http.StatusOK w.Header().Set("Content-Type", cssType) w.WriteHeader(status) @@ -38,6 +54,7 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) { if r.Method == "HEAD" { return status, 0, "" } + w.Write(cssBytes) return status, int64(len(cssBytes)), "" }