From b7fc420f7ea83d0fc7c5d508c91adf8c4a4f331b Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Tue, 27 Jun 2017 21:32:38 -0600 Subject: [PATCH] Added API for new CLI program --- handlers.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/handlers.go b/handlers.go index 2d599a3..a556a66 100755 --- a/handlers.go +++ b/handlers.go @@ -26,6 +26,7 @@ func serve(host, port, crt_path, key_path string, TLS bool) { }) router.GET("/:page/*command", handlePageRequest) 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("/lock", handleLock) router.POST("/encrypt", handleEncrypt) @@ -59,6 +60,31 @@ func loadTemplates(list ...string) multitemplate.Render { 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) { page := c.Param("page") command := c.Param("command") @@ -182,27 +208,41 @@ func handlePageRequest(c *gin.Context) { func handlePageUpdate(c *gin.Context) { type QueryJSON struct { - Page string `json:"page"` - NewText string `json:"new_text"` + Page string `json:"page"` + NewText string `json:"new_text"` + IsEncrypted bool `json:"is_encrypted"` + IsPrimed bool `json:"is_primed"` } 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"}) return } - if len(json.NewText) > 100000 { + if len(json.NewText) > 10000000 { c.JSON(http.StatusOK, gin.H{"success": false, "message": "Too much"}) return } + if len(json.Page) == 0 { + c.JSON(http.StatusOK, gin.H{"success": false, "message": "Must specify `page`"}) + return + } log.Trace("Update: %v", json) p := Open(json.Page) var message string if p.IsLocked { - message = "Locked" + message = "Locked, must unlock first" } else if p.IsEncrypted { - message = "Encrypted" + message = "Encrypted, must decrypt first" } else { p.Update(json.NewText) + if json.IsEncrypted { + p.IsEncrypted = true + } + if json.IsPrimed { + p.IsPrimedForSelfDestruct = true + } p.Save() message = "Saved" }