Merge pull request 'Add support for custom CSS files' (#11) from f/twins:custom-css into master

Reviewed-on: https://code.rocketnine.space/tslocum/twins/pulls/11
This commit is contained in:
tslocum 2021-07-09 19:37:56 -07:00
commit 9cd235c469
3 changed files with 34 additions and 0 deletions

View file

@ -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$

View file

@ -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")

View file

@ -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)), ""
}