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

Add static files, encryption

Former-commit-id: 80a53c01301cf96c28a22e1018efac2ceca7a381 [formerly af1c7ae599f506fcceebc85f87cc345e76329da3] [formerly 1bf3deab458c0e1f376da2296b03ac4fe8fcf8e9 [formerly 5a923ae11a]]
Former-commit-id: e826a5ffd431d22f8c37ac51f1be2193de1c270f [formerly 1deadc5e9c4fc7ff98b48bba18be31b773b8bde7]
Former-commit-id: 7fc964bb4c9d007010b1bb52665818212c0a2a8c
This commit is contained in:
Zack Scholl
2017-03-21 21:06:35 -06:00
parent 0be3c91823
commit 79422d1fb3
6 changed files with 75 additions and 11 deletions

51
main.go
View File

@ -5,12 +5,14 @@ import (
"net/http"
"strconv"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.Use(static.Serve("/static/", static.LocalFile("./static", true)))
router.GET("/", func(c *gin.Context) {
c.Redirect(302, "/"+randomAlliterateCombo())
@ -23,6 +25,7 @@ func main() {
router.POST("/update", handlePageUpdate)
router.POST("/prime", handlePrime)
router.POST("/lock", handleLock)
router.POST("/encrypt", handleEncrypt)
router.Run(":8050")
}
@ -78,13 +81,17 @@ func handlePageUpdate(c *gin.Context) {
}
log.Trace("Update: %v", json)
p := Open(json.Page)
if !p.IsLocked {
var message string
if p.IsLocked {
message = "Locked"
} else if p.IsEncrypted {
message = "Encrypted"
} else {
p.Update(json.NewText)
p.Save()
c.JSON(http.StatusOK, gin.H{"success": true, "message": "Saved"})
} else {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Locked"})
message = "Saved"
}
c.JSON(http.StatusOK, gin.H{"success": false, "message": message})
}
func handlePrime(c *gin.Context) {
@ -132,3 +139,39 @@ func handleLock(c *gin.Context) {
p.Save()
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
}
func handleEncrypt(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`
Passphrase string `json:"passphrase"`
}
var json QueryJSON
if c.BindJSON(&json) != nil {
c.String(http.StatusBadRequest, "Problem binding keys")
return
}
p := Open(json.Page)
var message string
if p.IsEncrypted {
decrypted, err2 := DecryptString(p.Text.GetCurrent(), json.Passphrase)
if err2 != nil {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong password"})
return
}
p.IsEncrypted = false
p.Erase()
p = Open(json.Page)
p.Update(decrypted)
message = "Decrypted"
} else {
p.IsEncrypted = true
p.Erase()
p = Open(json.Page)
encrypted, _ := EncryptString(p.Text.GetCurrent(), json.Passphrase)
p.Update(encrypted)
message = "Encrypted"
}
p.Save()
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
}