-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dab74c4
Showing
542 changed files
with
67,114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
package controller | ||
|
||
import ( | ||
"WebRest/database" | ||
"WebRest/helper" | ||
"WebRest/structyring" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
func RegGet(g *gin.Context) { | ||
|
||
g.HTML(http.StatusOK, "register.html", nil) | ||
} | ||
|
||
func Resgister(g *gin.Context) { | ||
var regist structyring.Regis | ||
if err := g.ShouldBind(®ist); err != nil { | ||
g.HTML(http.StatusBadRequest, "register.html", gin.H{ | ||
"error": "Ошибка регистрации", | ||
}) | ||
return | ||
} | ||
|
||
if regist.Password != regist.RepPassword { | ||
g.HTML(http.StatusBadRequest, "register.html", gin.H{ | ||
"error": "Пароль не совпадает", | ||
}) | ||
return | ||
} | ||
|
||
database.AddPeople(regist) | ||
|
||
g.HTML(http.StatusOK, "login.html", gin.H{ | ||
"success": "Пользователь зарегистрирован", | ||
}) | ||
} | ||
|
||
var loginCount = make(map[string]int) | ||
|
||
func Login(g *gin.Context) { | ||
var login structyring.Log | ||
ip := g.ClientIP() | ||
attack := loginCount[ip] | ||
if err := g.ShouldBind(&login); err != nil { | ||
g.HTML(http.StatusBadRequest, "login.html", gin.H{ | ||
"error": "Неправильный логин или пароль", | ||
}) | ||
return | ||
} | ||
|
||
if attack > 5 { | ||
g.HTML(http.StatusBadRequest, "login.html", gin.H{ | ||
"error": "Слишком много раз попыток входа", | ||
}) | ||
return | ||
} | ||
|
||
verif, name := database.CheckPeople(login) | ||
|
||
if verif == false { | ||
|
||
loginCount[ip]++ | ||
g.HTML(http.StatusUnauthorized, "login.html", gin.H{ | ||
"error": "Неправильный логин или пароль", | ||
}) | ||
return | ||
} | ||
|
||
token := helper.GenerateJWT(login) | ||
g.SetCookie("jwt", token, 3600, "/", "localhost", true, true) | ||
g.HTML(http.StatusOK, "registerPeople.html", gin.H{ | ||
"name": name, | ||
}) | ||
} | ||
|
||
func LoginGet(g *gin.Context) { | ||
g.HTML(http.StatusOK, "login.html", nil) | ||
} | ||
|
||
func HomePage(g *gin.Context) { | ||
g.HTML(http.StatusOK, "index.html", nil) | ||
} | ||
|
||
func RespBook(g *gin.Context) { | ||
var booking structyring.Book | ||
if err := g.ShouldBind(&booking); err != nil { | ||
g.HTML(http.StatusBadRequest, "index.html", gin.H{ | ||
"error": "Форма заполнена не корректно или с ошибкой", | ||
}) | ||
return | ||
} | ||
|
||
tokenString, err := g.Cookie("jwt") | ||
|
||
if err != nil { | ||
g.HTML(http.StatusUnauthorized, "index.html", gin.H{ | ||
"error": "Not have token", | ||
}) | ||
return | ||
} | ||
|
||
token, err := helper.GetSubFromToken(tokenString) | ||
|
||
if err != nil { | ||
g.HTML(http.StatusUnauthorized, "index.html", gin.H{ | ||
"error": "Not have token", | ||
}) | ||
return | ||
} | ||
database.InsertTable(token, booking) | ||
|
||
g.Redirect(http.StatusSeeOther, "/people") | ||
} | ||
|
||
func LogOut(g *gin.Context) { | ||
g.SetCookie("NotAuthorise", "", -1, "/", "localhost", true, true) | ||
g.Redirect(http.StatusSeeOther, "/") | ||
} | ||
|
||
func BookPeople(g *gin.Context) { | ||
tokenString, err := g.Cookie("jwt") | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// tok, err := helper.VerifyToken(tokenString) | ||
|
||
// if err == nil || tok == nil { | ||
// g.Redirect(http.StatusSeeOther, "/") | ||
// return | ||
// } | ||
|
||
token, errs := helper.GetSubFromToken(tokenString) | ||
|
||
if errs != nil { | ||
log.Fatal(errs) | ||
} | ||
|
||
people := database.GiveDateAndTime(token) | ||
drink := database.GetDrink(token) | ||
|
||
if !isEmptyStruct(drink) { | ||
g.HTML(http.StatusOK, "people.html", gin.H{ | ||
"name": token, | ||
"people": people.Name, | ||
"date": people.Date_day, | ||
"time": people.Time_day, | ||
"count": people.Count, | ||
"drink": drink.Name, | ||
"price": drink.Price, | ||
}) | ||
} | ||
|
||
g.HTML(http.StatusOK, "people.html", gin.H{ | ||
"name": token, | ||
"people": people.Name, | ||
"date": people.Date_day, | ||
"time": people.Time_day, | ||
"count": people.Count, | ||
}) | ||
|
||
} | ||
|
||
func isEmptyStruct(s interface{}) bool { | ||
v := reflect.ValueOf(s) | ||
|
||
if v.Kind() != reflect.Struct { | ||
return false | ||
} | ||
|
||
for i := 0; i < v.NumField(); i++ { | ||
if !v.Field(i).IsZero() { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func Menu(g *gin.Context) { | ||
tokenString, err := g.Cookie("jwt") | ||
|
||
if err != nil { | ||
g.Redirect(http.StatusSeeOther, "/") | ||
} | ||
|
||
token, errs := helper.GetSubFromToken(tokenString) | ||
|
||
if errs != nil { | ||
g.Redirect(http.StatusSeeOther, "/") | ||
} | ||
|
||
g.HTML(http.StatusOK, "menu.html", gin.H{ | ||
"name": token, | ||
}) | ||
} | ||
|
||
func MenuIns(g *gin.Context) { | ||
var drink structyring.Drink | ||
|
||
if err := g.ShouldBindJSON(&drink); err != nil { | ||
g.HTML(http.StatusBadRequest, "menu.html", gin.H{ | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
tokenString, err := g.Cookie("jwt") | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
token, errs := helper.GetSubFromToken(tokenString) | ||
|
||
if errs != nil { | ||
g.HTML(http.StatusBadRequest, "menu.html", gin.H{ | ||
"error": errs.Error(), | ||
}) | ||
return | ||
} | ||
|
||
database.MenuInsert(token, drink) | ||
|
||
g.HTML(http.StatusOK, "menu.html", gin.H{ | ||
"success": "Заказ оформлен", | ||
}) | ||
} |
Oops, something went wrong.