1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Add -css option for custom CSS on the /read page

This commit is contained in:
Zack Scholl 2017-11-04 04:18:05 -06:00
parent 5ae5c91945
commit 2040dbaaa5
3 changed files with 36 additions and 7 deletions

View File

@ -17,7 +17,9 @@ import (
"github.com/schollz/cowyo/encrypt" "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) gin.SetMode(gin.ReleaseMode)
router := gin.Default() router := gin.Default()
store := sessions.NewCookieStore([]byte("secret")) 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 // start long-processes as threads
go thread_SiteMap() 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 { if TLS {
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router) http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
} else { } else {
@ -166,11 +179,21 @@ func handlePageRequest(c *gin.Context) {
data, _ := Asset("/static/img/cowyo/favicon.ico") data, _ := Asset("/static/img/cowyo/favicon.ico")
c.Data(http.StatusOK, contentType("/static/img/cowyo/favicon.ico"), data) c.Data(http.StatusOK, contentType("/static/img/cowyo/favicon.ico"), data)
return return
} else if page == "/static/css/custom.css" {
c.Data(http.StatusOK, contentType("custom.css"), customCSS)
return
} else if page == "static" { } else if page == "static" {
filename := page + command filename := page + command
data, err := Asset(filename) var data []byte
if err != nil { fmt.Println(filename)
c.String(http.StatusInternalServerError, "Could not find data") 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) c.Data(http.StatusOK, contentType(filename), data)
return return
@ -291,7 +314,7 @@ func handlePageRequest(c *gin.Context) {
"HasDotInName": strings.Contains(page, "."), "HasDotInName": strings.Contains(page, "."),
"RecentlyEdited": getRecentlyEdited(page, c), "RecentlyEdited": getRecentlyEdited(page, c),
"IsPublished": p.IsPublished, "IsPublished": p.IsPublished,
"CustomCSS": "", "CustomCSS": len(customCSS) > 0,
}) })
} }

View File

@ -38,7 +38,7 @@ func main() {
} else { } else {
fmt.Printf("\nRunning cowyo server (version %s) at http://%s:%s\n\n", version, host, c.GlobalString("port")) 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 return nil
} }
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
@ -72,6 +72,11 @@ func main() {
Value: "", Value: "",
Usage: "absolute path to SSL private key", Usage: "absolute path to SSL private key",
}, },
cli.StringFlag{
Name: "css",
Value: "",
Usage: "use a custom CSS file",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug, d", Name: "debug, d",
Usage: "turn on debugging", Usage: "turn on debugging",

View File

@ -22,7 +22,8 @@
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png"> <meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#fff"> <meta name="theme-color" content="#fff">
{{ if and .CustomCSS .IsPublished }} {{ if and .CustomCSS .ReadPage }}
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
{{ else }} {{ else }}
<script type="text/javascript" src="/static/js/jquery-1.8.3.js"></script> <script type="text/javascript" src="/static/js/jquery-1.8.3.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/github-markdown.css"> <link rel="stylesheet" type="text/css" href="/static/css/github-markdown.css">