actency-mysql57-replication
Andrey Ivanov 2021-01-08 14:40:05 -05:00 committed by Andrey Ivanov
parent dbec9460ee
commit 58e31116e8
9 changed files with 135 additions and 33 deletions

View File

@ -55,7 +55,7 @@ func main() {
// Регистрация пользователя, после которой нас перебрасывает на страницу логина
m.Get("/signup", handlers.GetSigned)
m.Post("/signup", handlers.PostSigned)
m.Post("/signup", binding.Bind(auth.UserModel{}), handlers.PostSigned)
//Анкета текущего пользователя
m.Get("/", auth.LoginRequired, handlers.GetHome)

View File

@ -1,24 +1,23 @@
package auth
import (
"database/sql"
"fmt"
"github.com/tiburon-777/OTUS_HighLoad/internal/application"
"time"
)
type UserModel struct {
Id int64 `form:"id" db:"id"`
Username string `form:"name" db:"username"`
Password string `form:"password" db:"password"`
Name string `form:"name" db:"name"`
Surname string `form:"surname" db:"surname"`
BirthDate time.Time `form:"birthdate" db:"birthdate"`
Male bool `form:"male" db:"male"`
City string `form:"city" db:"city"`
Interests string `form:"interests" db:"interests"`
authenticated bool `form:"-" db:"-"`
Db *sql.DB
Id int64 `db:"id" form:"id"`
Username string `db:"username" form:"username"`
Password string `db:"password" form:"password"`
Name string `db:"name" form:"name"`
Surname string `db:"surname" form:"surname"`
BirthDate time.Time `db:"birthdate"`
FormBirthDate string `form:"birthdate"`
Gender string `db:"gender" form:"gender"`
City string `db:"city" form:"city"`
Interests string `db:"interests" form:"interests"`
authenticated bool `db:"-" form:"-"`
}
func GenerateAnonymousUser() User {
@ -49,11 +48,16 @@ func (u *UserModel) UniqueId() interface{} {
}
func (u *UserModel) GetById(app application.App, id interface{}) error {
query := fmt.Sprintf("SELECT username FROM users WHERE id=%d", id)
var v []uint8
err := app.DB.QueryRow(query).Scan(&v)
var v string
query := fmt.Sprintf("SELECT username, name, surname, birthdate, gender, city, interests FROM users WHERE id=%d", id)
err := app.DB.QueryRow(query).Scan(&u.Username, &u.Name, &u.Surname, &v, &u.Gender, &u.City, &u.Interests)
if err != nil {
return err
}
u.BirthDate, err = time.Parse("2006-01-02 15:04:05", v)
if err != nil {
return err
}
u.Id=id.(int64)
return nil
}

View File

@ -1,34 +1,61 @@
package handlers
import (
"encoding/base64"
"fmt"
"github.com/codegangsta/martini-contrib/render"
"github.com/codegangsta/martini-contrib/sessions"
"github.com/tiburon-777/OTUS_HighLoad/internal/application"
"github.com/tiburon-777/OTUS_HighLoad/internal/auth"
"net"
"log"
"net/http"
"time"
)
func GetHome(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "Вы имеете доступ к проектам",
}
r.HTML(200, "index", doc)
func GetHome(r render.Render, user auth.User) {
r.HTML(200, "index", user)
}
func GetSigned(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "page not exists",
}
r.HTML(200, "signin", doc)
r.HTML(200, "signup", doc)
}
func PostSigned(app application.App, r render.Render) {
r.Redirect(net.JoinHostPort(app.Config.Server.Address, app.Config.Server.Port)+"/login")
func PostSigned(app application.App, session sessions.Session, postedUser auth.UserModel, r render.Render, req *http.Request) {
t, err := time.Parse("2006-1-2", postedUser.FormBirthDate)
if err != nil {
e := fmt.Errorf("can't parce date: %w", err)
log.Println(e)
doc := map[string]interface{}{
"Error": e,
}
r.HTML(500, "500", doc)
}
query := fmt.Sprintf(`INSERT INTO users (username, password, name, surname, birthdate, gender, city, interests)
values ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")`,
postedUser.Username,
base64.StdEncoding.EncodeToString([]byte(postedUser.Username + ":" + postedUser.Password)),
postedUser.Name,
postedUser.Surname,
t.Format("2006-01-02 15:04:05"),
postedUser.Gender,
postedUser.City,
postedUser.Interests,
)
_, err = app.DB.Exec(query)
if err != nil {
e := fmt.Errorf("can't create account in DB: %w", err)
log.Println(e)
doc := map[string]interface{}{
"Error": e,
}
r.HTML(500, "500", doc)
}
r.Redirect("/login")
}
func GetUserList(r render.Render) {
doc := map[string]interface{}{
"PageTitle": "page not exists",
@ -37,9 +64,10 @@ func GetUserList(r render.Render) {
}
func PostLogin(app application.App, session sessions.Session, postedUser auth.UserModel, r render.Render, req *http.Request) {
hash := base64.StdEncoding.EncodeToString([]byte(postedUser.Username + ":" + postedUser.Password))
user := auth.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)
query := fmt.Sprintf("SELECT id FROM users WHERE username=\"%s\" and password =\"%s\"", postedUser.Username, hash)
err := app.DB.QueryRow(query).Scan(&user.Id)
if err != nil || user.Id==0 {
r.Redirect(auth.RedirectUrl)

View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<p>404. Page not found</p>
<a href="/">Home</a><br/>
</body>
</html>

7
templates/500.tmpl Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<body>
<p>500. INTERNAL SERVER ERROR!</p>
<p>{{ .Error }}</p>
</body>
</html>

View File

@ -1,8 +1,13 @@
<!DOCTYPE html>
<html>
<body>
<p>This is a private link!</p>
<p> Hello {{ .Username }}</p>
<a href="/logout">Logout</a><br/>
<p> Hello <b>{{ .Name }} {{ .Surname }}</b></p>
<p>You gender is <b>{{ .Gender }}</b></p>
<p>You <b>{{ .BirthDate }}</b> years old</p>
<p>You now live in <b>{{ .City }}</b></p>
<p>You interests is: <b>{{ .Interests }}</b></p>
<input type="button" onclick="location.href='/list';" value="User list" />
<input type="button" onclick="location.href='/logout';" value="Logout" /><br />
</body>
</html>

View File

@ -3,9 +3,10 @@
<body>
<h2>You must login!</h2>
<form method="POST">
<input type="text" placeholder="Username" name="name" /><br />
<input type="password" placeholder="Password" name="password" />
<input type="text" placeholder="Username" name="username" /><br />
<input type="password" placeholder="Password" name="password" /><br />
<button>Login</button>
<input type="button" onclick="location.href='/signup';" value="or Sign Up" />
</form>
</body>
</html>

View File

50
templates/signup.tmpl Normal file
View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
</head>
<body>
<h2>Create account</h2>
<form method="POST">
<table>
<tr>
<td>Логин</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Имя</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Фамилия</td>
<td><input type="text" name="surname" /></td>
</tr>
<tr>
<td>Дата рождения</td>
<td><input type="date" name="birthdate" placeholder="dd-mm-yyyy" class="form-control" id="date" required /></td>
</tr>
<tr>
<td>Пол</td>
<td>
<a><input name="gender" type="radio" value="male"> Мужской</a>
<a><input name="gender" type="radio" value="female"> Женский</a>
<a><input name="gender" type="radio" value="other"> Другое</a>
</td>
</tr>
<tr>
<td>Интересы</td>
<td><input type="text"name="interests" /></td>
</tr>
<tr>
<td>Город</td>
<td><input type="text"name="city" /></td>
</tr>
<tr>
<td>Пароль</td>
<td><input type="password" name="password" /></td>
</tr>
</table>
<button>Sign Up</button>
</form>
</body>
</html>