1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

Update the _examples/mvc/login example - add a Path property at mvc.Response and add a context.Proceed helper function

Former-commit-id: b898901fe4a324e888a6e09c93530cf7a551cf2a
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-10-12 16:28:41 +03:00
parent b0f8329768
commit 32d14db46d
33 changed files with 1073 additions and 50 deletions

View File

@@ -1,43 +1,71 @@
// file: main.go
package main
import (
"time"
"github.com/kataras/iris/_examples/mvc/login/user"
"github.com/kataras/iris"
"github.com/kataras/iris/_examples/mvc/login/datasource"
"github.com/kataras/iris/_examples/mvc/login/repositories"
"github.com/kataras/iris/_examples/mvc/login/services"
"github.com/kataras/iris/_examples/mvc/login/web/controllers"
"github.com/kataras/iris/_examples/mvc/login/web/middleware"
"github.com/kataras/iris/sessions"
)
func main() {
app := iris.New()
// You got full debug messages, useful when using MVC and you want to make
// sure that your code is compatible with the Iris' MVC Architecture.
// sure that your code is aligned with the Iris' MVC Architecture.
app.Logger().SetLevel("debug")
app.RegisterView(iris.HTML("./views", ".html").Layout("shared/layout.html"))
// Load the template files.
tmpl := iris.HTML("./web/views", ".html").
Layout("shared/layout.html").
Reload(true)
app.RegisterView(tmpl)
app.StaticWeb("/public", "./public")
app.StaticWeb("/public", "./web/public")
manager := sessions.New(sessions.Config{
app.OnAnyErrorCode(func(ctx iris.Context) {
ctx.ViewData("Message", ctx.Values().
GetStringDefault("message", "The page you're looking for doesn't exist"))
ctx.View("shared/error.html")
})
// Create our repositories and services.
db, err := datasource.LoadUsers(datasource.Memory)
if err != nil {
app.Logger().Fatalf("error while loading the users: %v", err)
return
}
repo := repositories.NewUserRepository(db)
userService := services.NewUserService(repo)
// Register our controllers.
app.Controller("/users", new(controllers.UsersController),
// Add the basic authentication(admin:password) middleware
// for the /users based requests.
middleware.BasicAuth,
// Bind the "userService" to the UserController's Service (interface) field.
userService,
)
sessManager := sessions.New(sessions.Config{
Cookie: "sessioncookiename",
Expires: 24 * time.Hour,
})
users := user.NewDataSource()
app.Controller("/user", new(controllers.UserController), userService, sessManager)
app.Controller("/user", new(user.Controller), manager, users)
// http://localhost:8080/user/register
// http://localhost:8080/user/login
// http://localhost:8080/user/me
// http://localhost:8080/user/logout
// http://localhost:8080/user/1
app.Run(iris.Addr(":8080"), configure)
}
func configure(app *iris.Application) {
app.Configure(
// Start the web server at localhost:8080
// http://localhost:8080/hello
// http://localhost:8080/hello/iris
// http://localhost:8080/users/1
app.Run(
iris.Addr("localhost:8080"),
iris.WithoutVersionChecker,
iris.WithoutServerError(iris.ErrServerClosed),
iris.WithCharset("UTF-8"),
iris.WithOptimizations, // enables faster json serialization and more
)
}