Serve Static Dir and clean unused routes
This commit is contained in:
parent
138a926999
commit
c452e29722
2 changed files with 2 additions and 47 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
static-hoster
|
||||
hostdir
|
||||
|
|
|
@ -8,8 +8,6 @@ import (
|
|||
"nikurasu.gay/static-hoster/middleware/auth"
|
||||
)
|
||||
|
||||
var db = make(map[string]string)
|
||||
|
||||
func Create() *gin.Engine {
|
||||
router := gin.Default()
|
||||
|
||||
|
@ -22,51 +20,7 @@ func Create() *gin.Engine {
|
|||
c.String(http.StatusOK, "pong")
|
||||
})
|
||||
|
||||
// Get user value
|
||||
router.GET("/user/:name", func(c *gin.Context) {
|
||||
user := c.Params.ByName("name")
|
||||
value, ok := db[user]
|
||||
if ok {
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "value": value})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
|
||||
}
|
||||
})
|
||||
|
||||
// Authorized group (uses gin.BasicAuth() middleware)
|
||||
// Same than:
|
||||
// authorized := r.Group("/")
|
||||
// authorized.Use(gin.BasicAuth(gin.Credentials{
|
||||
// "foo": "bar",
|
||||
// "manu": "123",
|
||||
//}))
|
||||
authorized := router.Group("/", gin.BasicAuth(gin.Accounts{
|
||||
"foo": "bar", // user:foo password:bar
|
||||
"manu": "123", // user:manu password:123
|
||||
}))
|
||||
|
||||
/* example curl for /admin with basicauth header
|
||||
Zm9vOmJhcg== is base64("foo:bar")
|
||||
|
||||
curl -X POST \
|
||||
http://localhost:8080/admin \
|
||||
-H 'authorization: Basic Zm9vOmJhcg==' \
|
||||
-H 'content-type: application/json' \
|
||||
-d '{"value":"bar"}'
|
||||
*/
|
||||
authorized.POST("admin", func(c *gin.Context) {
|
||||
user := c.MustGet(gin.AuthUserKey).(string)
|
||||
|
||||
// Parse JSON
|
||||
var json struct {
|
||||
Value string `json:"value" binding:"required"`
|
||||
}
|
||||
|
||||
if c.Bind(&json) == nil {
|
||||
db[user] = json.Value
|
||||
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
})
|
||||
router.Static("/home", "./hostdir")
|
||||
|
||||
return router
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue