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

Merge pull request #93 from schollz/v3

V3
This commit is contained in:
Zack 2017-11-04 04:42:49 -06:00 committed by GitHub
commit 8b3e7b0605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 115 additions and 23 deletions

View File

@ -9,6 +9,11 @@ build:
go-bindata static/... templates/...
go build ${LDFLAGS}
.PHONY: quick
quick:
go-bindata static/... templates/...
go build
.PHONY: linuxarm
linuxarm:
env GOOS=linux GOARCH=arm go build ${LDFLAGS} -o dist/cowyo_linux_arm

View File

@ -17,7 +17,10 @@ import (
"github.com/schollz/cowyo/encrypt"
)
func serve(host, port, crt_path, key_path string, TLS bool) {
var customCSS []byte
var defaultLock string
func serve(host, port, crt_path, key_path string, TLS bool, cssFile string, defaultPage string, defaultPassword string) {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
@ -25,7 +28,11 @@ func serve(host, port, crt_path, key_path string, TLS bool) {
router.HTMLRender = loadTemplates("index.tmpl")
// router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
router.GET("/", func(c *gin.Context) {
c.Redirect(302, "/"+randomAlliterateCombo())
if defaultPage != "" {
c.Redirect(302, "/"+defaultPage+"/read")
} else {
c.Redirect(302, "/"+randomAlliterateCombo())
}
})
router.GET("/:page", func(c *gin.Context) {
page := c.Param("page")
@ -45,6 +52,22 @@ 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 defaultPassword != "" {
fmt.Println("running with locked pages")
defaultLock = HashPassword(defaultPassword)
}
if TLS {
http.ListenAndServeTLS(host+":"+port, crt_path, key_path, router)
} else {
@ -149,6 +172,7 @@ func generateSiteMap() (sitemap string) {
sitemap += "</urlset>"
return
}
func handlePageRequest(c *gin.Context) {
page := c.Param("page")
command := c.Param("command")
@ -165,26 +189,49 @@ 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
}
p := Open(page)
fmt.Println(command)
if len(command) < 2 {
c.Redirect(302, "/"+page+"/edit")
fmt.Println(p.IsPublished)
if p.IsPublished {
c.Redirect(302, "/"+page+"/read")
} else {
c.Redirect(302, "/"+page+"/edit")
}
return
}
version := c.DefaultQuery("version", "ajksldfjl")
p := Open(page)
// use the default lock
if defaultLock != "" && p.IsNew() {
p.IsLocked = true
p.PassphraseToUnlock = defaultLock
}
// Disallow anything but viewing locked/encrypted pages
if (p.IsEncrypted || p.IsLocked) &&
(command[0:2] != "/v" && command[0:2] != "/r") {
fmt.Println("IS LOCKED")
c.Redirect(302, "/"+page+"/view")
return
}
@ -232,7 +279,7 @@ func handlePageRequest(c *gin.Context) {
versionsChangeSums = reverseSliceInt(versionsChangeSums)
}
if command[0:2] == "/r" {
if command[0:3] == "/ra" {
c.Writer.Header().Set("Content-Type", contentType(p.Name))
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
@ -246,7 +293,8 @@ func handlePageRequest(c *gin.Context) {
log.Debug("%v", command[0:2] != "/e" &&
command[0:2] != "/v" &&
command[0:2] != "/l" &&
command[0:2] != "/h")
command[0:2] != "/h" &&
command[0:2] != "/r")
var FileNames, FileLastEdited []string
var FileSizes, FileNumChanges []int
@ -260,6 +308,7 @@ func handlePageRequest(c *gin.Context) {
"ViewPage": command[0:2] == "/v", // /view
"ListPage": command[0:2] == "/l", // /list
"HistoryPage": command[0:2] == "/h", // /history
"ReadPage": command[0:2] == "/r", // /history
"DontKnowPage": command[0:2] != "/e" &&
command[0:2] != "/v" &&
command[0:2] != "/l" &&
@ -282,6 +331,7 @@ func handlePageRequest(c *gin.Context) {
"HasDotInName": strings.Contains(page, "."),
"RecentlyEdited": getRecentlyEdited(page, c),
"IsPublished": p.IsPublished,
"CustomCSS": len(customCSS) > 0,
})
}
@ -419,6 +469,11 @@ func handleLock(c *gin.Context) {
return
}
p := Open(json.Page)
if defaultLock != "" && p.IsNew() {
p.IsLocked = true
p.PassphraseToUnlock = defaultLock
}
if p.IsEncrypted {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Encrypted"})
return
@ -437,6 +492,7 @@ func handleLock(c *gin.Context) {
p.PassphraseToUnlock = HashPassword(json.Passphrase)
message = "Locked"
}
fmt.Println(p)
p.Save()
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
}

17
main.go
View File

@ -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"), c.GlobalString("default-page"), c.GlobalString("lock"))
return nil
}
app.Flags = []cli.Flag{
@ -72,6 +72,21 @@ func main() {
Value: "",
Usage: "absolute path to SSL private key",
},
cli.StringFlag{
Name: "css",
Value: "",
Usage: "use a custom CSS file",
},
cli.StringFlag{
Name: "default-page",
Value: "",
Usage: "show default-page/read instead of editing (default: show random editing)",
},
cli.StringFlag{
Name: "lock",
Value: "",
Usage: "password to lock editing all files (default: all pages unlocked)",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "turn on debugging",

View File

@ -102,6 +102,10 @@ func (p *Page) Save() error {
return ioutil.WriteFile(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"), bJSON, 0644)
}
func (p *Page) IsNew() bool {
return !exists(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))
}
func (p *Page) Erase() error {
log.Trace("Erasing " + p.Name)
return os.Remove(path.Join(pathToData, encodeToBase32(strings.ToLower(p.Name))+".json"))

View File

@ -22,15 +22,18 @@
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#fff">
<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/menus-min.css">
<link rel="stylesheet" type="text/css" href="/static/css/base-min.css">
<link rel="stylesheet" href="/static/css/highlight.css">
<script src="/static/js/highlight.min.js"></script>
<script type="text/javascript" src="/static/js/highlight.pack.js"></script>
{{ if and .CustomCSS .ReadPage }}
<link rel="stylesheet" type="text/css" href="/static/css/custom.css">
{{ else }}
<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/menus-min.css">
<link rel="stylesheet" type="text/css" href="/static/css/base-min.css">
<link rel="stylesheet" href="/static/css/highlight.css">
<script src="/static/js/highlight.min.js"></script>
<script type="text/javascript" src="/static/js/highlight.pack.js"></script>
<style type="text/css">
<style type="text/css">
{{ if .ListPage }}
/* Required for lists */
span { cursor: pointer; }
@ -129,9 +132,8 @@ body#pad textarea {
padding-right: 20%;
}
}
</style>
</style>
{{ end }}
<title>{{ .Page }}</title>
@ -478,9 +480,18 @@ body#pad textarea {
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body id="pad">
<article class="markdown-body">
{{ if .ReadPage }}
<div id="wrap">
<div id="rendered">
{{ .RenderedPage }}
</div>
</div>
{{ else }}
<div class="pure-menu pure-menu-horizontal" id="menu">
<ul class="pure-menu-list">
<li></li>
@ -594,8 +605,9 @@ body#pad textarea {
{{end}}
</div>
</div>
</article>
{{ end }}
</article>