mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 14:57:05 +00:00
Add session.Destroy (before I've added: Increment & Decrement entry helpers as well) | Improve the debug messages for the controllers
Former-commit-id: f5f17b05252a5032ace1e2d0fdd2304fc4004635
This commit is contained in:
@@ -5,12 +5,14 @@ package main
|
||||
import (
|
||||
"time"
|
||||
|
||||
"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"
|
||||
"github.com/kataras/iris/mvc"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
@@ -34,7 +36,9 @@ func main() {
|
||||
ctx.View("shared/error.html")
|
||||
})
|
||||
|
||||
// Create our repositories and services.
|
||||
// ---- Register our controllers. ----
|
||||
|
||||
// Prepare our repositories and services.
|
||||
db, err := datasource.LoadUsers(datasource.Memory)
|
||||
if err != nil {
|
||||
app.Logger().Fatalf("error while loading the users: %v", err)
|
||||
@@ -43,29 +47,38 @@ func main() {
|
||||
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,
|
||||
)
|
||||
// "/users" based mvc application.
|
||||
users := mvc.New(app.Party("/users"))
|
||||
// Add the basic authentication(admin:password) middleware
|
||||
// for the /users based requests.
|
||||
users.Router.Use(middleware.BasicAuth)
|
||||
// Bind the "userService" to the UserController's Service (interface) field.
|
||||
users.AddDependencies(userService)
|
||||
users.Register(new(controllers.UsersController))
|
||||
|
||||
// "/user" based mvc application.
|
||||
sessManager := sessions.New(sessions.Config{
|
||||
Cookie: "sessioncookiename",
|
||||
Expires: 24 * time.Hour,
|
||||
})
|
||||
app.Controller("/user", new(controllers.UserController), userService, sessManager)
|
||||
user := mvc.New(app.Party("/user"))
|
||||
user.AddDependencies(
|
||||
userService,
|
||||
mvc.Session(sessManager),
|
||||
)
|
||||
user.Register(new(controllers.UserController))
|
||||
|
||||
// Start the web server at localhost:8080
|
||||
// http://localhost:8080/hello
|
||||
// http://localhost:8080/hello/iris
|
||||
// http://localhost:8080/noexist
|
||||
// and all controller's methods like
|
||||
// http://localhost:8080/users/1
|
||||
app.Run(
|
||||
// Starts the web server at localhost:8080
|
||||
iris.Addr("localhost:8080"),
|
||||
// Disables the updater.
|
||||
iris.WithoutVersionChecker,
|
||||
// Ignores err server closed log when CTRL/CMD+C pressed.
|
||||
iris.WithoutServerError(iris.ErrServerClosed),
|
||||
iris.WithOptimizations, // enables faster json serialization and more
|
||||
// Enables faster json serialization and more.
|
||||
iris.WithOptimizations,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/kataras/iris/_examples/mvc/login/datamodels"
|
||||
"github.com/kataras/iris/_examples/mvc/login/services"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
@@ -20,39 +20,21 @@ import (
|
||||
// GET /user/me
|
||||
// All HTTP Methods /user/logout
|
||||
type UserController struct {
|
||||
// mvc.C is just a lightweight lightweight alternative
|
||||
// to the "mvc.Controller" controller type,
|
||||
// use it when you don't need mvc.Controller's fields
|
||||
// (you don't need those fields when you return values from the method functions).
|
||||
mvc.C
|
||||
// context is auto-binded by Iris on each request,
|
||||
// remember that on each incoming request iris creates a new UserController each time,
|
||||
// so all fields are request-scoped by-default, only dependency injection is able to set
|
||||
// custom fields like the Service which is the same for all requests (static binding)
|
||||
// and the Session which depends on the current context (dynamic binding).
|
||||
Ctx iris.Context
|
||||
|
||||
// Our UserService, it's an interface which
|
||||
// is binded from the main application.
|
||||
Service services.UserService
|
||||
|
||||
// Session-relative things.
|
||||
Manager *sessions.Sessions
|
||||
// Session, binded using dependency injection from the main.go.
|
||||
Session *sessions.Session
|
||||
}
|
||||
|
||||
// BeginRequest will set the current session to the controller.
|
||||
//
|
||||
// Remember: iris.Context and context.Context is exactly the same thing,
|
||||
// iris.Context is just a type alias for go 1.9 users.
|
||||
// We use context.Context here because we don't need all iris' root functions,
|
||||
// when we see the import paths, we make it visible to ourselves that this file is using only the context.
|
||||
func (c *UserController) BeginRequest(ctx context.Context) {
|
||||
c.C.BeginRequest(ctx)
|
||||
|
||||
if c.Manager == nil {
|
||||
ctx.Application().Logger().Errorf(`UserController: sessions manager is nil, you should bind it`)
|
||||
ctx.StopExecution() // dont run the main method handler and any "done" handlers.
|
||||
return
|
||||
}
|
||||
|
||||
c.Session = c.Manager.Start(ctx)
|
||||
}
|
||||
|
||||
const userIDKey = "UserID"
|
||||
|
||||
func (c *UserController) getCurrentUserID() int64 {
|
||||
@@ -65,12 +47,12 @@ func (c *UserController) isLoggedIn() bool {
|
||||
}
|
||||
|
||||
func (c *UserController) logout() {
|
||||
c.Manager.DestroyByID(c.Session.ID())
|
||||
c.Session.Destroy()
|
||||
}
|
||||
|
||||
var registerStaticView = mvc.View{
|
||||
Name: "user/register.html",
|
||||
Data: context.Map{"Title": "User Registration"},
|
||||
Data: iris.Map{"Title": "User Registration"},
|
||||
}
|
||||
|
||||
// GetRegister handles GET: http://localhost:8080/user/register.
|
||||
@@ -119,7 +101,7 @@ func (c *UserController) PostRegister() mvc.Result {
|
||||
|
||||
var loginStaticView = mvc.View{
|
||||
Name: "user/login.html",
|
||||
Data: context.Map{"Title": "User Login"},
|
||||
Data: iris.Map{"Title": "User Login"},
|
||||
}
|
||||
|
||||
// GetLogin handles GET: http://localhost:8080/user/login.
|
||||
@@ -172,7 +154,7 @@ func (c *UserController) GetMe() mvc.Result {
|
||||
|
||||
return mvc.View{
|
||||
Name: "user/me.html",
|
||||
Data: context.Map{
|
||||
Data: iris.Map{
|
||||
"Title": "Profile of " + u.Username,
|
||||
"User": u,
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"github.com/kataras/iris/_examples/mvc/login/datamodels"
|
||||
"github.com/kataras/iris/_examples/mvc/login/services"
|
||||
|
||||
"github.com/kataras/iris/mvc"
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// UsersController is our /users API controller.
|
||||
@@ -14,8 +14,14 @@ import (
|
||||
// DELETE /users/{id:long} | delete by id
|
||||
// Requires basic authentication.
|
||||
type UsersController struct {
|
||||
mvc.C
|
||||
// context is auto-binded by Iris on each request,
|
||||
// remember that on each incoming request iris creates a new UserController each time,
|
||||
// so all fields are request-scoped by-default, only dependency injection is able to set
|
||||
// custom fields like the Service which is the same for all requests (static binding).
|
||||
Ctx iris.Context
|
||||
|
||||
// Our UserService, it's an interface which
|
||||
// is binded from the main application.
|
||||
Service services.UserService
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user