mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
3945fa68d1
We have to do the same on iris-contrib/examples, iris-contrib/middleware and e.t.c. Former-commit-id: 0860688158f374bc137bc934b81b26dcd0e10964
47 lines
990 B
Go
47 lines
990 B
Go
package main
|
|
|
|
import (
|
|
"github.com/kataras/iris/v12"
|
|
"github.com/kataras/iris/v12/middleware/basicauth"
|
|
)
|
|
|
|
func newApp() *iris.Application {
|
|
app := iris.New()
|
|
|
|
authConfig := basicauth.Config{
|
|
Users: map[string]string{"myusername": "mypassword"},
|
|
}
|
|
|
|
authentication := basicauth.New(authConfig)
|
|
|
|
app.Get("/", func(ctx iris.Context) { ctx.Redirect("/admin") })
|
|
|
|
// to party
|
|
|
|
needAuth := app.Party("/admin", authentication)
|
|
{
|
|
//http://localhost:8080/admin
|
|
needAuth.Get("/", h)
|
|
// http://localhost:8080/admin/profile
|
|
needAuth.Get("/profile", h)
|
|
|
|
// http://localhost:8080/admin/settings
|
|
needAuth.Get("/settings", h)
|
|
}
|
|
|
|
return app
|
|
}
|
|
|
|
func h(ctx iris.Context) {
|
|
username, password, _ := ctx.Request().BasicAuth()
|
|
// third parameter it will be always true because the middleware
|
|
// makes sure for that, otherwise this handler will not be executed.
|
|
|
|
ctx.Writef("%s %s:%s", ctx.Path(), username, password)
|
|
}
|
|
|
|
func main() {
|
|
app := newApp()
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|