Skip to content

Instantly share code, notes, and snippets.

@omarkurt
Created November 4, 2017 09:37
Show Gist options
  • Save omarkurt/39bd94a09b75100ca7a2acb78f5938c8 to your computer and use it in GitHub Desktop.
Save omarkurt/39bd94a09b75100ca7a2acb78f5938c8 to your computer and use it in GitHub Desktop.
acl test code
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/s4l1h/acl"
)
const (
MODULE_NAME = "Members"
MODULE_DESC = "Members Module"
PERM_NAME = "add"
PERM_DESC = "Add Perms"
)
func main() {
router := gin.Default()
// This handler will match /user/john but will not match neither /user/ or /user
router.GET("/user/:name", func(c *gin.Context) {
name := c.Param("name")
// CREATE MODULE
module := acl.NewModule(MODULE_NAME, MODULE_DESC)
if module.GetName() != MODULE_NAME {
fmt.Errorf("Module Name Error : %v != %v", module.GetName(), MODULE_NAME)
}
if module.GetDesc() != MODULE_DESC {
fmt.Errorf("Module desc Error : %v != %v", module.GetDesc(), MODULE_DESC)
}
nname := MODULE_NAME + " New"
ndesc := MODULE_DESC + " New"
module.SetName(nname)
module.SetDesc(ndesc)
if module.GetName() != nname {
fmt.Errorf("Module SetName Error : %v != %v", module.GetName(), nname)
}
if module.GetDesc() != ndesc {
fmt.Errorf("Module SetDesc Error : %v != %v", module.GetDesc(), ndesc)
}
c.String(http.StatusOK, "username : %s \n", name)
c.String(http.StatusOK, "ADD Perm : %s \n", module)
c.String(http.StatusOK, "Module name : %s \n", nname)
c.String(http.StatusOK, "Module Des : %s \n", ndesc)
c.String(http.StatusOK, "Module getName : %s \n", module.GetName())
// CREATE PERM in module
perm := acl.NewPerm(PERM_NAME, PERM_DESC)
if perm.GetName() != PERM_NAME {
fmt.Errorf("Perm Name Error : %v != %v", perm.GetName(), PERM_NAME)
}
if perm.GetDesc() != PERM_DESC {
fmt.Errorf("Perm desc Error : %v != %v", perm.GetDesc(), PERM_DESC)
}
//Test Set Name,Desc
nname1 := PERM_NAME + " New"
ndesc1 := PERM_DESC + " New"
perm.SetName(nname1)
perm.SetDesc(ndesc1)
if perm.GetName() != nname {
fmt.Errorf("Perm SetName Error : %v != %v", perm.GetName(), nname)
}
if perm.GetDesc() != ndesc {
fmt.Errorf("Perm SetDesc Error : %v != %v", perm.GetDesc(), ndesc)
}
c.String(http.StatusOK, "-------------------------------- \n\n\n")
c.String(http.StatusOK, "Module name : %s \n", nname1)
c.String(http.StatusOK, "Module Des : %s \n", ndesc1)
c.String(http.StatusOK, "Cagir ne var ne yok : %s \n", module.GetName())
})
// However, this one will match /user/john/ and also /user/john/send
// If no other routers match /user/john, it will redirect to /user/john/
router.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
message := name + " is " + action
c.String(http.StatusOK, message)
})
router.Run(":2221") // listen and serve on 0.0.0.0:8080
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment