From 83453281bc1211ca432753f47ef541c89c722623 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Mon, 7 Jun 2021 23:52:49 +0200 Subject: [PATCH 1/3] 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)), "" } From 8654f0ca1bffa1ce188d5e1725d807e4ccffdefd Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 9 Jul 2021 22:15:35 +0200 Subject: [PATCH 2/3] Rename to StyleSheet and precache CSS bytes If there is a custom CSS file specified and usable (it exists and is readable), we precache it on server startup (for every host). This increase the request performance and reduce IO-load slightly. --- CONFIGURATION.md | 6 +++--- config.go | 19 ++++++++++++++++++- serve_https.go | 23 +++++++---------------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 483a31e..dfec97e 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -81,9 +81,9 @@ Set this option to `true` to disable this feature. Pages are converted automatically by [gmitohtml](https://code.rocketnine.space/tslocum/gmitohtml). -## Styles +## StyleSheet -Specify a custom CSS styles file if you want to override the default. This only +Specify a custom CSS stylesheet file if you want to override the default. This only affects the `HTTPS` protocol (obviously). ### DisableSize @@ -228,7 +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 + stylesheet: /srv/gemini.rocks/styles.css # Custom CSS stylesheet paths: - path: ^/.*\.php$ diff --git a/config.go b/config.go index 0b757a1..7f20b18 100644 --- a/config.go +++ b/config.go @@ -67,7 +67,7 @@ type hostConfig struct { Paths []*pathConfig // Custom CSS styles. If specified, it will be used for all paths in that host/domain. - Styles string + StyleSheet string cert *tls.Certificate } @@ -87,6 +87,8 @@ type serverConfig struct { const cacheUnset = -1965 +var customCSS = make(map[string][]byte) + var config *serverConfig func readconfig(configPath string) error { @@ -213,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 6258910..9504328 100644 --- a/serve_https.go +++ b/serve_https.go @@ -31,22 +31,6 @@ 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) @@ -55,6 +39,13 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) { 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)), "" } From daf4b2b1a2c817aafdd9401bcafeabbea8590140 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 9 Jul 2021 22:19:46 +0200 Subject: [PATCH 3/3] gofmt --- serve_https.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/serve_https.go b/serve_https.go index 9504328..b4c09cc 100644 --- a/serve_https.go +++ b/serve_https.go @@ -41,8 +41,8 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) { // 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)), "" + w.Write(css) + return status, int64(len(css)), "" } // Default CSS