HighLoad_HomeWork/internal/handlers/handlers.go

59 lines
1.5 KiB
Go
Executable File

package handlers
import (
"fmt"
"github.com/codegangsta/martini-contrib/render"
"github.com/codegangsta/martini-contrib/sessionauth"
"github.com/codegangsta/martini-contrib/sessions"
"github.com/tiburon-777/OTUS_HighLoad/internal/application"
"github.com/tiburon-777/OTUS_HighLoad/internal/models"
"net"
"net/http"
)
func GetHome(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "Вы имеете доступ к проектам",
}
r.HTML(200, "index", doc)
}
func GetSigned(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "page not exists",
}
r.HTML(200, "signin", doc)
}
func PostSigned(app application.App, r render.Render) {
r.Redirect(net.JoinHostPort(app.Config.Server.Address, app.Config.Server.Port)+"/login")
}
func GetUserList(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "page not exists",
}
r.HTML(200, "list", doc)
}
func PostLogin(app application.App, session sessions.Session, postedUser models.UserModel, r render.Render, req *http.Request) {
user := models.UserModel{}
query := fmt.Sprintf("SELECT * FROM users WHERE username=\"%s\" and password =\"%s\"", postedUser.Username, postedUser.Password)
err := app.DB.QueryRow(query).Scan(&user.Id, &user.Username, &user.Password)
if err != nil || user.Id==0 {
r.Redirect(sessionauth.RedirectUrl)
return
} else {
err := sessionauth.AuthenticateSession(session, &user)
if err != nil {
r.JSON(500, err)
}
params := req.URL.Query()
redirect := params.Get(sessionauth.RedirectParam)
r.Redirect(redirect)
return
}
}