From 2040dbaaa5f662e029eef77a2663160f65871030 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 4 Nov 2017 04:18:05 -0600 Subject: [PATCH] Add -css option for custom CSS on the /read page --- handlers.go | 33 ++++++++++++++++++++++++++++----- main.go | 7 ++++++- templates/index.tmpl | 3 ++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/handlers.go b/handlers.go index 70e38ed..445c7c6 100755 --- a/handlers.go +++ b/handlers.go @@ -17,7 +17,9 @@ import ( "github.com/schollz/cowyo/encrypt" ) -func serve(host, port, crt_path, key_path string, TLS bool) { +var customCSS []byte + +func serve(host, port, crt_path, key_path string, TLS bool, cssFile string) { gin.SetMode(gin.ReleaseMode) router := gin.Default() store := sessions.NewCookieStore([]byte("secret")) @@ -45,6 +47,17 @@ func serve(host, port, crt_path, key_path string, TLS bool) { // start long-processes as threads go thread_SiteMap() + // collect custom CSS + if len(cssFile) > 0 { + var errRead error + customCSS, errRead = ioutil.ReadFile(cssFile) + if errRead != nil { + fmt.Println(errRead.Error()) + return + } + fmt.Printf("Loaded CSS file, %d bytes\n", len(customCSS)) + } + if TLS { http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) } else { @@ -166,11 +179,21 @@ func handlePageRequest(c *gin.Context) { data, _ := Asset("/static/img/cowyo/favicon.ico") c.Data(http.StatusOK, contentType("/static/img/cowyo/favicon.ico"), data) return + } else if page == "/static/css/custom.css" { + c.Data(http.StatusOK, contentType("custom.css"), customCSS) + return } else if page == "static" { filename := page + command - data, err := Asset(filename) - if err != nil { - c.String(http.StatusInternalServerError, "Could not find data") + var data []byte + fmt.Println(filename) + if filename == "static/css/custom.css" { + data = customCSS + } else { + var errAssset error + data, errAssset = Asset(filename) + if errAssset != nil { + c.String(http.StatusInternalServerError, "Could not find data") + } } c.Data(http.StatusOK, contentType(filename), data) return @@ -291,7 +314,7 @@ func handlePageRequest(c *gin.Context) { "HasDotInName": strings.Contains(page, "."), "RecentlyEdited": getRecentlyEdited(page, c), "IsPublished": p.IsPublished, - "CustomCSS": "", + "CustomCSS": len(customCSS) > 0, }) } diff --git a/main.go b/main.go index 54fb1cd..1c46a93 100755 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { } else { fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) } - serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS) + serve(c.GlobalString("host"), c.GlobalString("port"), c.GlobalString("cert"), c.GlobalString("key"), TLS, c.GlobalString("css")) return nil } app.Flags = []cli.Flag{ @@ -72,6 +72,11 @@ func main() { Value: "", Usage: "absolute path to SSL private key", }, + cli.StringFlag{ + Name: "css", + Value: "", + Usage: "use a custom CSS file", + }, cli.BoolFlag{ Name: "debug, d", Usage: "turn on debugging", diff --git a/templates/index.tmpl b/templates/index.tmpl index 314a417..287f324 100755 --- a/templates/index.tmpl +++ b/templates/index.tmpl @@ -22,7 +22,8 @@ -{{ if and .CustomCSS .IsPublished }} +{{ if and .CustomCSS .ReadPage }} + {{ else }}