06.03.2019

This commit is contained in:
Mikolaj 2019-03-06 20:06:19 +01:00
parent d2bff63605
commit 27ee05ae64
17 changed files with 517 additions and 84 deletions

11
helpers/FileHelper.go Normal file
View File

@ -0,0 +1,11 @@
package helpers
import "io/ioutil"
func LoadFile(fileName string) (string, error) {
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
return "", err
}
return string(bytes), nil
}

9
helpers/StringHelper.go Normal file
View File

@ -0,0 +1,9 @@
package helpers
func IsEmpty(data string) bool {
if len(data) <= 0 {
return true
} else {
return false
}
}

153
main.go
View File

@ -10,15 +10,38 @@ import (
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"golang.org/x/crypto/bcrypt"
"github.com/satori/go.uuid"
)
func index(rend render.Render) {
type user struct {
UserName string
Password []byte
First string
Last string
}
var tpl *template.Template
var dbUsers = map[string]user{} // user ID, user
var dbSessions = map[string]string{} // session ID, user ID
func init() {
tpl = template.Must(template.ParseGlob("templates/*"))
bs, _ := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.MinCost)
dbUsers["test@test.com"] = user{"test@test.com", bs, "James", "Bond"}
}
func index1(rend render.Render, w http.ResponseWriter, req *http.Request) {
if !alreadyLoggedIn(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
posts, err := models.Posts.FindAll()
if err != nil {
rend.Error(http.StatusBadRequest)
return
}
rend.HTML(http.StatusOK, "index", posts)
rend.HTML(http.StatusOK, "index1", posts)
}
@ -61,7 +84,7 @@ func savePost(rend render.Render, w http.ResponseWriter, r *http.Request) {
}
}
rend.Redirect("/")
rend.Redirect("/index1")
}
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
@ -70,7 +93,7 @@ func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, para
return
}
rend.Redirect("/")
rend.Redirect("/index1")
}
func getHtmlPost(rend render.Render, w http.ResponseWriter, r *http.Request) {
@ -80,8 +103,115 @@ func getHtmlPost(rend render.Render, w http.ResponseWriter, r *http.Request) {
})
}
func login(rend render.Render) {
rend.HTML(http.StatusOK, "login", &models.Post{})
func index(w http.ResponseWriter, req *http.Request) {
u := getUser(w, req)
tpl.ExecuteTemplate(w, "index.html", u)
}
func login(w http.ResponseWriter, req *http.Request) {
if alreadyLoggedIn(req) {
http.Redirect(w, req, "/index1", http.StatusSeeOther)
return
}
var u user
// process form submission
if req.Method == http.MethodPost {
un := req.FormValue("username")
p := req.FormValue("password")
// is there a username?
u, ok := dbUsers[un]
if !ok {
http.Error(w, "NIEUDANA PRÓBA LOGOWANIA, SPRAWDŹ CZY HASŁO I LOGIN SĄ POPRAWNĘ, JEŚLI NIE MASZ JESZCZE KONTA ZAŁÓŻ JE", http.StatusForbidden)
return
}
// does the entered password match the stored password?
err := bcrypt.CompareHashAndPassword(u.Password, []byte(p))
if err != nil {
http.Error(w, "NIEUDANA PRÓBA LOGOWANIA, SPRAWDŹ CZY HASŁO I LOGIN SĄ POPRAWNĘ, JEŚLI NIE MASZ JESZCZE KONTA ZAŁÓŻ JE ", http.StatusForbidden)
return
}
// create session
sID, _ := uuid.NewV4()
c := &http.Cookie{
Name: "session",
Value: sID.String(),
}
http.SetCookie(w, c)
dbSessions[c.Value] = un
http.Redirect(w, req, "/index1", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "login.html", u)
}
func signup(w http.ResponseWriter, req *http.Request) {
if alreadyLoggedIn(req) {
http.Redirect(w, req, "/index1", http.StatusSeeOther)
return
}
var u user
// process form submission
if req.Method == http.MethodPost {
// get form values
un := req.FormValue("username")
p := req.FormValue("password")
f := req.FormValue("firstname")
l := req.FormValue("lastname")
// username taken?
if _, ok := dbUsers[un]; ok {
http.Error(w, "Username already taken", http.StatusForbidden)
return
}
// create session
sID, _ := uuid.NewV4()
c := &http.Cookie{
Name: "session",
Value: sID.String(),
}
http.SetCookie(w, c)
dbSessions[c.Value] = un
// store user in dbUsers
bs, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.MinCost)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
u = user{un, bs, f, l}
dbUsers[un] = u
// redirect
http.Redirect(w, req, "/index1", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "signup.html", u)
}
func bar(w http.ResponseWriter, req *http.Request) {
u := getUser(w, req)
if !alreadyLoggedIn(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
tpl.ExecuteTemplate(w, "bar.html", u)
}
func logout(w http.ResponseWriter, req *http.Request) {
if !alreadyLoggedIn(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
return
}
c, _ := req.Cookie("session")
// delete the session
delete(dbSessions, c.Value)
// remove the cookie
c = &http.Cookie{
Name: "session",
Value: "",
MaxAge: -1,
}
http.SetCookie(w, c)
http.Redirect(w, req, "/login", http.StatusSeeOther)
}
func unescape(s string) interface{} {
@ -107,13 +237,22 @@ func main() {
staticOptions := martini.StaticOptions{Prefix: "bower_components"}
m.Use(martini.Static("bower_components", staticOptions))
m.Get("/", index)
m.Get("/index1", index1)
m.Post("/index1", index1)
m.Get("/write", write)
m.Get("/", index)
m.Get("/login", login)
m.Post("/login", login)
m.Get("/signup", signup)
m.Post("/signup", signup)
m.Get("/bar", bar)
m.Get("/edit/:id", edit)
m.Post("/savePost", savePost)
m.Get("/deletePost/:id", deletePost)
m.Post("/getHtml", getHtmlPost)
m.Post("/logout", logout)
m.Get("/logout", logout)
http.Handle("/favicon.ico", http.NotFoundHandler())
m.Run()
}

37
session.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"github.com/satori/go.uuid"
"net/http"
)
func getUser(w http.ResponseWriter, req *http.Request) user {
// get cookie
c, err := req.Cookie("session")
if err != nil {
sID, _ := uuid.NewV4()
c = &http.Cookie{
Name: "session",
Value: sID.String(),
}
}
http.SetCookie(w, c)
// if the user exists already, get user
var u user
if un, ok := dbSessions[c.Value]; ok {
u = dbUsers[un]
}
return u
}
func alreadyLoggedIn(req *http.Request) bool {
c, err := req.Cookie("session")
if err != nil {
return false
}
un := dbSessions[c.Value]
_, ok := dbUsers[un]
return ok
}

18
templates/bar.html Normal file
View File

@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BAR</title>
</head>
<body>
<h1>Welcome to the bar. What can I get you to drink?</h1>
{{if .First}}
USER NAME {{.UserName}}<br>
<h2><a href="/logout">log out</a></h2>
{{end}}
</body>
</html>

42
templates/index.html Normal file
View File

@ -0,0 +1,42 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="Stylesheet" href="style.css">
<title>Document</title>
<style>
h2
{ color: red }
.container
{
width: 200px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="container">Witaj na Blogu</a></div>
{{if .First}}
USER NAME {{.UserName}}<br>
PASSWORD {{.Password}}<br>
FIRST {{.First}}<br>
LAST {{.Last}}<br>
<h2><a href="/logout">log out</a></h2>
{{else}}
<h2><a href="/signup">sign up</a></h2>
<h2><a href="/login">log in</a></h2>
{{end}}
<br>
</body>
</html>

97
templates/login.html Normal file
View File

@ -0,0 +1,97 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="Stylesheet" href="style.css">
<title>Document</title>
<style>
body{
background-color: rgb(197, 207, 224);
}
#panel {
width: 400px;
margin: 0 auto;
padding: 15px 0 0;
background: rgb(169, 176, 187);
border: 1px solid silver;
font: 16px calibri;
letter-spacing: -1px;
-webkit-box-shadow: 0 0 2px silver;
-moz-box-shadow: 0 0 2px silver;
box-shadow: 0 0 2px silver;
}
form {
margin: 0;
}
input {
display: block;
width: 260px;
padding: 10px 20px;
color: #696969;
font-size: 16px;
text-shadow: 0 0 1px silver;
}
#username, #password {
display: block;
width: 360px;
margin: 0 auto;
padding: 10px 5px;
border: 1px solid silver;
outline: 5px solid #ebebeb;
font-size: 22px;
}
#username:focus, #password:focus {
outline: 5px solid #e5f2f8;
}
input[type="submit"] {
width: 70px;
padding: 5px 13px;
border: 1px solid #005f85;
color: white;
text-shadow: 0 0 1px black;
background: #98c9dc;
position: static;
left: 180px;
}
#panel, input[type="submit"] {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.sig {
display: block;
width: 260px;
padding: 10px 20px;
color: #696969;
font-size: 32px;
text-shadow: 0 0 1px silver;
text-decoration: none;
}
a:link { color: #696; text-decoration: none; background-color: transparent }
a:visited { color: rgb(29, 29, 39); text-decoration: none; background-color: transparent }
a:hover { color: rgb(70, 110, 155); text-decoration: none; background-color: transparent }
a:active { color: #900; text-decoration: underline; background-color: transparent }
</style>
</head>
<body c red;>
<div id="panel">
<h1>LOGIN</h1>
<form method="post">
<input type="text" name="username" placeholder="email">
<input type="password" name="password" placeholder="password">
<input type="submit">
</form>
<h2><span class="sig"><a href="/signup">Zarejestruj Się</a></span></h2>
</body>
</div>
</html>

20
templates/signup.html Normal file
View File

@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post">
<input type="email" name="username" placeholder="email"><br>
<input type="password" name="password" placeholder="password"><br>
<input type="text" name="firstname" placeholder="first name"><br>
<input type="text" name="lastname" placeholder="last name"><br>
<input type="submit">
</form>
</body>
</html>

15
templates/style.css Normal file
View File

@ -0,0 +1,15 @@
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
color: #003868;
background-color: #80B8E8;
margin: 6mm;
}
p {
text-align: justify;
}
pre, code {
font-size: 8pt;
}

18
views/bar.html Normal file
View File

@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BAR</title>
</head>
<body>
<h1>Welcome to the bar. What can I get you to drink?</h1>
{{if .First}}
USER NAME {{.UserName}}<br>
<h2><a href="/logout">log out</a></h2>
{{end}}
</body>
</html>

View File

@ -1,38 +1,24 @@
{{ range $key, $value := . }}
<div class="row">
<div class="col-xs-2"></div>
<div class="col-xs-8">
<h1><a href ="/edit/{{$value.Id}}">{{ $value.Title }}</a></h1>
</div>
<div class="col-xs-2"></div>
</div>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
{{ $value.ContentHtml | unescape }}
<div class="col-xs-8">
<br/><br/>Komentarz
<fieldset style="border: 2px solid; width: 450px; height: 160px; padding: 3;">{{ $value.ContentCom | unescape }}</fieldset>
<form action="/createCom" method="POST" role="form"></form>
<input type="hidden" name="id" value="{{.Id}}"/>
<div class="form-group">
</div>
</form>
</div>
</div>
<div class="col-xs-2"></div>
</div>
{{if .First}}
USER NAME {{.UserName}}<br>
PASSWORD {{.Password}}<br>
FIRST {{.First}}<br>
LAST {{.Last}}<br>
<h2><a href="/logout">log out</a></h2>
{{else}}
<h2><a href="/signup">sign up</a></h2>
<h2><a href="/login">log in</a></h2>
{{end}}
<br>
<h2>Go to <a href="/bar">the bar</a></h2>
</body>
</html>

37
views/index1.html Normal file
View File

@ -0,0 +1,37 @@
{{ range $key, $value := . }}
<div class="row">
<div class="col-xs-2"></div>
<div class="col-xs-8">
<h1><a href ="/edit/{{$value.Id}}">{{ $value.Title }}</a></h1>
</div>
<div class="col-xs-2"></div>
</div>
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-8">
{{ $value.ContentHtml | unescape }}
<div class="col-xs-8">
<br/><br/>Komentarz
<fieldset style="border: 2px solid; width: 450px; height: 160px; padding: 3;">{{ $value.ContentCom | unescape }}</fieldset>
<form action="/createCom" method="POST" role="form"></form>
<input type="hidden" name="id" value="{{.Id}}"/>
<div class="form-group">
</div>
</form>
</div>
</div>
<div class="col-xs-2"></div>
</div>
{{end}}

View File

@ -17,13 +17,19 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">GO BLOG</a>
<a class="navbar-brand" href="/index1">GO BLOG</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Strona Głowna</a></li>
<li class="active"><a href="/index1">Strona Głowna</a></li>
<li><a href="/write">Nowy Post</a></li>
</ul>
<ul class="nav navbar-nav">
<li class="active"><a href="/logout">Wyloguj</a></li>
<li><a href="/write">sad</a></li>
</ul>
</div>
</div>
</div>

View File

@ -1,8 +0,0 @@
<div class="log-form">
<h1>Index</h1>
<hr>
<small>User: %s</small>
<form method="post" action="/logout">
<button type="submit">Logout</button>
</form>
</div>

View File

@ -1,10 +1,18 @@
<div class="log-form">
<h2>Login to your account</h2>
<form method="post" action="/login">
<label for="name">User name</label>
<input type="text" id="name" name="name"> <br />
<label for="password">Password</label>
<input type="password" id="password" name="password"> <br />
<button type="submit">Login</button>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>LOGIN</h1>
<form method="post" action="/index1">
<input type="text" name="username" placeholder="email">
<input type="password" name="password" placeholder="password">
<input type="submit">
</form>
</div>
<h2><a href="/signup">signup</a></h2>
</body>
</html>

View File

@ -1,22 +0,0 @@
<div class="container">
<h1>Register</h1>
<hr>
<small>This is register page</small>
<form method="post" action="/register">
<label for="name">Username</label>
<input type="text" id="username" name="username"> <br />
<label for="name">Email</label>
<input type="text" id="email" name="email"> <br />
<label for="password">password</label>
<input type="password" id="password" name="password"> <br />
<label for="ConfirmPassword">Confirm Password</label>
<input type="password" id="ConfirmPassword" name="ConfirmPassword"> <br />
<button type="submit">Register</button>
</form>
</div>

20
views/signup.html Normal file
View File

@ -0,0 +1,20 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" action="/index1">
<input type="email" name="username" placeholder="email"><br>
<input type="text" name="password" placeholder="password"><br>
<input type="text" name="firstname" placeholder="first name"><br>
<input type="text" name="lastname" placeholder="last name"><br>
<input type="submit">
</form>
</body>
</html>