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

Added API for new CLI program

This commit is contained in:
Zack Scholl 2017-06-27 21:32:38 -06:00
parent 294d4611d2
commit b7fc420f7e

View File

@ -26,6 +26,7 @@ func serve(host, port, crt_path, key_path string, TLS bool) {
}) })
router.GET("/:page/*command", handlePageRequest) router.GET("/:page/*command", handlePageRequest)
router.POST("/update", handlePageUpdate) router.POST("/update", handlePageUpdate)
router.POST("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
router.POST("/prime", handlePrime) router.POST("/prime", handlePrime)
router.POST("/lock", handleLock) router.POST("/lock", handleLock)
router.POST("/encrypt", handleEncrypt) router.POST("/encrypt", handleEncrypt)
@ -59,6 +60,31 @@ func loadTemplates(list ...string) multitemplate.Render {
return r return r
} }
func handlePageRelinquish(c *gin.Context) {
type QueryJSON struct {
Page string `json:"page"`
}
var json QueryJSON
err := c.BindJSON(&json)
if err != nil {
log.Trace(err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"})
return
}
if len(json.Page) == 0 {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Must specify `page`"})
return
}
message := "Relinquished"
p := Open(json.Page)
text := p.Text.GetCurrent()
if !p.IsLocked && p.IsPrimedForSelfDestruct {
p.Erase()
message = "Relinquished and erased"
}
c.JSON(http.StatusOK, gin.H{"success": true, "message": message, "text": text})
}
func handlePageRequest(c *gin.Context) { func handlePageRequest(c *gin.Context) {
page := c.Param("page") page := c.Param("page")
command := c.Param("command") command := c.Param("command")
@ -182,27 +208,41 @@ func handlePageRequest(c *gin.Context) {
func handlePageUpdate(c *gin.Context) { func handlePageUpdate(c *gin.Context) {
type QueryJSON struct { type QueryJSON struct {
Page string `json:"page"` Page string `json:"page"`
NewText string `json:"new_text"` NewText string `json:"new_text"`
IsEncrypted bool `json:"is_encrypted"`
IsPrimed bool `json:"is_primed"`
} }
var json QueryJSON var json QueryJSON
if c.BindJSON(&json) != nil { err := c.BindJSON(&json)
if err != nil {
log.Trace(err.Error())
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"}) c.JSON(http.StatusOK, gin.H{"success": false, "message": "Wrong JSON"})
return return
} }
if len(json.NewText) > 100000 { if len(json.NewText) > 10000000 {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Too much"}) c.JSON(http.StatusOK, gin.H{"success": false, "message": "Too much"})
return return
} }
if len(json.Page) == 0 {
c.JSON(http.StatusOK, gin.H{"success": false, "message": "Must specify `page`"})
return
}
log.Trace("Update: %v", json) log.Trace("Update: %v", json)
p := Open(json.Page) p := Open(json.Page)
var message string var message string
if p.IsLocked { if p.IsLocked {
message = "Locked" message = "Locked, must unlock first"
} else if p.IsEncrypted { } else if p.IsEncrypted {
message = "Encrypted" message = "Encrypted, must decrypt first"
} else { } else {
p.Update(json.NewText) p.Update(json.NewText)
if json.IsEncrypted {
p.IsEncrypted = true
}
if json.IsPrimed {
p.IsPrimedForSelfDestruct = true
}
p.Save() p.Save()
message = "Saved" message = "Saved"
} }