mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
Add /exists to check if page exists
This commit is contained in:
parent
d69e37d1bc
commit
d3101da4ac
25
handlers.go
25
handlers.go
@ -28,6 +28,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("/relinquish", handlePageRelinquish) // relinquish returns the page no matter what (and destroys if nessecary)
|
||||||
|
router.POST("/exists", handlePageExists)
|
||||||
router.POST("/prime", handlePrime)
|
router.POST("/prime", handlePrime)
|
||||||
router.POST("/lock", handleLock)
|
router.POST("/lock", handleLock)
|
||||||
router.POST("/encrypt", handleEncrypt)
|
router.POST("/encrypt", handleEncrypt)
|
||||||
@ -210,6 +211,26 @@ func handlePageRequest(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handlePageExists(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", "exists": false})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p := Open(json.Page)
|
||||||
|
if len(p.Text.GetCurrent()) > 0 {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " found", "exists": true})
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusOK, gin.H{"success": true, "message": json.Page + " not found", "exists": false})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func handlePageUpdate(c *gin.Context) {
|
func handlePageUpdate(c *gin.Context) {
|
||||||
type QueryJSON struct {
|
type QueryJSON struct {
|
||||||
Page string `json:"page"`
|
Page string `json:"page"`
|
||||||
@ -235,6 +256,7 @@ func handlePageUpdate(c *gin.Context) {
|
|||||||
log.Trace("Update: %v", json)
|
log.Trace("Update: %v", json)
|
||||||
p := Open(json.Page)
|
p := Open(json.Page)
|
||||||
var message string
|
var message string
|
||||||
|
success := false
|
||||||
if p.IsLocked {
|
if p.IsLocked {
|
||||||
message = "Locked, must unlock first"
|
message = "Locked, must unlock first"
|
||||||
} else if p.IsEncrypted {
|
} else if p.IsEncrypted {
|
||||||
@ -249,8 +271,9 @@ func handlePageUpdate(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
p.Save()
|
p.Save()
|
||||||
message = "Saved"
|
message = "Saved"
|
||||||
|
success = true
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
|
c.JSON(http.StatusOK, gin.H{"success": success, "message": message})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlePrime(c *gin.Context) {
|
func handlePrime(c *gin.Context) {
|
||||||
|
Loading…
Reference in New Issue
Block a user