mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 18:51:03 +01:00
42b123975c
Former-commit-id: 90c05e743052bc3722e7fefaa0cbb0ed5153a1fb
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris/middleware/basicauth"
|
|
)
|
|
|
|
func newApp() *iris.Application {
|
|
app := iris.New()
|
|
|
|
authConfig := basicauth.Config{
|
|
Users: map[string]string{"myusername": "mypassword", "mySecondusername": "mySecondpassword"},
|
|
Realm: "Authorization Required", // defaults to "Authorization Required"
|
|
Expires: time.Duration(30) * time.Minute,
|
|
}
|
|
|
|
authentication := basicauth.New(authConfig)
|
|
|
|
// to global app.Use(authentication) (or app.UseGlobal before the .Run)
|
|
// to routes
|
|
/*
|
|
app.Get("/mysecret", authentication, h)
|
|
*/
|
|
|
|
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 main() {
|
|
app := newApp()
|
|
// open http://localhost:8080/admin
|
|
app.Run(iris.Addr(":8080"))
|
|
}
|
|
|
|
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)
|
|
}
|