further extension of the system
It is now possible to use the database. The authentication middleware is the first module to use it. We will see, how it will work out.
This commit is contained in:
parent
0ce5d76e07
commit
fce1df3f21
|
@ -0,0 +1,23 @@
|
|||
package admin
|
||||
|
||||
import (
|
||||
"log"
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
)
|
||||
|
||||
func LoginGet(c *lib.Context) error {
|
||||
c.Env.Template.Lookup("login/index").Execute(c.Response, struct {}{})
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoginPost(c *lib.Context) error {
|
||||
username := c.Request.PostFormValue("username")
|
||||
password := c.Request.PostFormValue("password")
|
||||
if username == "" && password == "" {
|
||||
log.Print("There was nothing at all!")
|
||||
} else {
|
||||
log.Print(username, password)
|
||||
}
|
||||
c.Env.Template.Lookup("login/index").Execute(c.Response, struct {}{})
|
||||
return nil
|
||||
}
|
|
@ -2,9 +2,15 @@ package controller
|
|||
|
||||
import (
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
"github.com/gibheer/zero-blog/middleware"
|
||||
"github.com/gibheer/zero-blog/controller/admin"
|
||||
"github.com/gibheer/zero-blog/controller/welcome"
|
||||
)
|
||||
|
||||
func DefineRoutes(router *lib.Router) {
|
||||
router.Get("/", welcome.Welcome)
|
||||
router.Get( "/login", admin.LoginGet)
|
||||
router.Post("/login", admin.LoginPost)
|
||||
authentication := router.NewGroup("/admin")
|
||||
authentication.Use(middleware.Authentication)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gibheer/zero-blog/repository"
|
||||
)
|
||||
|
||||
// the following is inspired by the gin framework
|
||||
|
@ -36,6 +37,8 @@ type Context struct {
|
|||
Response http.ResponseWriter
|
||||
// parameters provided by the router
|
||||
Params httprouter.Params
|
||||
// current session
|
||||
Session *repository.Session
|
||||
|
||||
// the list of functions to run
|
||||
funcList []ContextFunc
|
||||
|
@ -93,10 +96,16 @@ func (r *Router) createHandleFunction(target ContextFunc) httprouter.Handle {
|
|||
params httprouter.Params) {
|
||||
|
||||
ctx := &Context{
|
||||
request, response, params, append(r.fullFuncList(), target), 0, r.env,
|
||||
Request: request,
|
||||
Response: response,
|
||||
Params: params,
|
||||
funcList: append(r.fullFuncList(), target),
|
||||
current: 0,
|
||||
Env: r.env,
|
||||
}
|
||||
for i := 0; i < len(ctx.funcList); i++ {
|
||||
ctx.funcList[i](ctx)
|
||||
for !ctx.Aborted() && ctx.current < len(ctx.funcList) {
|
||||
ctx.funcList[ctx.current](ctx)
|
||||
ctx.current++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -113,3 +122,14 @@ func (r *Router) Start() {
|
|||
log.Print("Starting to listen for incoming requests ...")
|
||||
log.Fatal(http.ListenAndServe(":9292", r.router))
|
||||
}
|
||||
|
||||
func (c *Context) Aborted() bool {
|
||||
if c.current < 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Context) Abort() {
|
||||
c.current = -1
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"github.com/gibheer/zero-blog/lib"
|
||||
"github.com/gibheer/zero-blog/repository"
|
||||
)
|
||||
|
||||
func Authentication(c *lib.Context) error {
|
||||
session_id, err := c.Request.Cookie("session")
|
||||
if err != nil {
|
||||
redirectToLogin(c)
|
||||
return err
|
||||
}
|
||||
session := repository.GetSession(c.Env.DB.Conn, session_id.Value)
|
||||
if session == nil {
|
||||
redirectToLogin(c)
|
||||
return err
|
||||
}
|
||||
c.Session = session
|
||||
return nil
|
||||
}
|
||||
|
||||
func redirectToLogin(c *lib.Context) {
|
||||
c.Abort()
|
||||
http.Redirect(c.Response, c.Request, "/login", 307)
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
Id int
|
||||
Username string
|
||||
Email string
|
||||
Password string
|
||||
Role string
|
||||
}
|
||||
|
||||
func GetAccount(db *sql.DB, account_id string) *Account {
|
||||
account := &Account{}
|
||||
err := db.QueryRow(
|
||||
`select id, username, email from accounts where id = ?`,
|
||||
account_id).Scan(
|
||||
&account.Id,
|
||||
&account.Username,
|
||||
&account.Email,
|
||||
)
|
||||
// TODO do something with the error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return account
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
Id string
|
||||
Account *Account
|
||||
}
|
||||
|
||||
func GetSession(db *sql.DB, session_id string) *Session {
|
||||
session := &Session{Account: &Account{}}
|
||||
|
||||
err := db.QueryRow(
|
||||
`select session_id, id, username
|
||||
from sessions s
|
||||
join accounts a
|
||||
on s.account_id = a.id
|
||||
where session_id = ?
|
||||
and last_change > now() - interval '1 hour'`,
|
||||
session_id).Scan(&session.Id, &session.Account.Id, &session.Account.Username)
|
||||
// TODO do something with the error
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return session
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head></head>
|
||||
<body>
|
||||
This is not working correctly yet, but who cares
|
||||
<form action="/login" method="post">
|
||||
<input name="username" />
|
||||
<input name="password" />
|
||||
<button type="submit">send?</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue