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.
This commit is contained in:
Aaron Fischer 2021-06-07 23:52:49 +02:00
parent cd85204680
commit 83453281bc
3 changed files with 26 additions and 0 deletions

View file

@ -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). 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 ### DisableSize
The size of the response body is included in the media type header by default. The size of the response body is included in the media type header by default.
@ -223,6 +228,7 @@ hosts:
gemini.rocks: gemini.rocks:
cert: /srv/gemini.rocks/data/cert.crt cert: /srv/gemini.rocks/data/cert.crt
key: /srv/gemini.rocks/data/cert.key key: /srv/gemini.rocks/data/cert.key
styles: /srv/gemini.rocks/styles.css # Custom CSS styles
paths: paths:
- -
path: ^/.*\.php$ path: ^/.*\.php$

View file

@ -66,6 +66,9 @@ type hostConfig struct {
Key string Key string
Paths []*pathConfig Paths []*pathConfig
// Custom CSS styles. If specified, it will be used for all paths in that host/domain.
Styles string
cert *tls.Certificate cert *tls.Certificate
} }

View file

@ -31,6 +31,22 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) {
http.Redirect(w, r, u.String(), status) http.Redirect(w, r, u.String(), status)
return status, -1, "" return status, -1, ""
} else if r.URL.Path == "/assets/style.css" { } 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 status := http.StatusOK
w.Header().Set("Content-Type", cssType) w.Header().Set("Content-Type", cssType)
w.WriteHeader(status) w.WriteHeader(status)
@ -38,6 +54,7 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) {
if r.Method == "HEAD" { if r.Method == "HEAD" {
return status, 0, "" return status, 0, ""
} }
w.Write(cssBytes) w.Write(cssBytes)
return status, int64(len(cssBytes)), "" return status, int64(len(cssBytes)), ""
} }