13.12.2018
This commit is contained in:
parent
e0b131be8f
commit
9a02f795a0
86
main.go
86
main.go
@ -2,61 +2,36 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers"
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers"
|
||||||
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
|
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/models"
|
||||||
|
"github.com/go-martini/martini"
|
||||||
|
"github.com/martini-contrib/render"
|
||||||
)
|
)
|
||||||
|
|
||||||
var posts map[string]*models.Post
|
var posts map[string]*models.Post
|
||||||
|
|
||||||
func index(w http.ResponseWriter, r *http.Request) {
|
func index(rend render.Render) {
|
||||||
fmt.Println(posts)
|
rend.HTML(http.StatusOK, "index", posts)
|
||||||
t, err := template.ParseFiles(
|
|
||||||
"views/write.html",
|
|
||||||
"views/header.html",
|
|
||||||
"views/footer.html",
|
|
||||||
"views/index.html",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, err.Error())
|
|
||||||
}
|
|
||||||
t.ExecuteTemplate(w, "index", posts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func write(w http.ResponseWriter, r *http.Request) {
|
func write(rend render.Render) {
|
||||||
t, err := template.ParseFiles(
|
rend.HTML(http.StatusOK, "write", &models.Post{})
|
||||||
"views/index.html",
|
|
||||||
"views/header.html",
|
|
||||||
"views/footer.html",
|
|
||||||
"views/write.html",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, err.Error())
|
|
||||||
}
|
|
||||||
t.ExecuteTemplate(w, "write", nil)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func edit(w http.ResponseWriter, r *http.Request) {
|
func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
||||||
t, err := template.ParseFiles(
|
id := params["id"]
|
||||||
"views/index.html",
|
|
||||||
"views/header.html",
|
|
||||||
"views/footer.html",
|
|
||||||
"views/write.html",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(w, err.Error())
|
|
||||||
}
|
|
||||||
id := r.FormValue("id")
|
|
||||||
post, found := posts[id]
|
post, found := posts[id]
|
||||||
if !found {
|
if !found {
|
||||||
http.NotFound(w, r)
|
rend.Redirect("/")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
t.ExecuteTemplate(w, "write", post)
|
rend.HTML(http.StatusOK, "write", post)
|
||||||
}
|
}
|
||||||
|
|
||||||
func savePost(w http.ResponseWriter, r *http.Request) {
|
func savePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
||||||
id := r.FormValue("id")
|
id := r.FormValue("id")
|
||||||
title := r.FormValue("title")
|
title := r.FormValue("title")
|
||||||
content := r.FormValue("content")
|
content := r.FormValue("content")
|
||||||
@ -71,30 +46,41 @@ func savePost(w http.ResponseWriter, r *http.Request) {
|
|||||||
posts[post.Id] = post
|
posts[post.Id] = post
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/", 302)
|
rend.Redirect("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func deletePost(w http.ResponseWriter, r *http.Request) {
|
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
|
||||||
id := r.FormValue("id")
|
id := params["id"]
|
||||||
if id == "" {
|
if id == "" {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(posts, id)
|
delete(posts, id)
|
||||||
|
|
||||||
http.Redirect(w, r, "/", 302)
|
rend.Redirect("/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
posts = make(map[string]*models.Post, 0)
|
posts = make(map[string]*models.Post, 0)
|
||||||
fmt.Println("Listening port 8080")
|
|
||||||
|
|
||||||
http.Handle("/bower_components/", http.StripPrefix("/bower_components/", http.FileServer(http.Dir("./bower_components/"))))
|
fmt.Println("Listening port 3000")
|
||||||
http.HandleFunc("/", index)
|
m := martini.Classic()
|
||||||
http.HandleFunc("/write", write)
|
|
||||||
http.HandleFunc("/edit", edit)
|
|
||||||
http.HandleFunc("/savePost", savePost)
|
|
||||||
http.HandleFunc("/deletePost", deletePost)
|
|
||||||
|
|
||||||
http.ListenAndServe(":8080", nil)
|
m.Use(render.Renderer(render.Options{
|
||||||
|
Directory: "views",
|
||||||
|
Layout: "layout",
|
||||||
|
Extensions: []string{".html"},
|
||||||
|
Charset: "UTF-8",
|
||||||
|
IndentJSON: true,
|
||||||
|
}))
|
||||||
|
|
||||||
|
staticOptions := martini.StaticOptions{Prefix: "bower_components"}
|
||||||
|
m.Use(martini.Static("bower_components", staticOptions))
|
||||||
|
m.Get("/", index)
|
||||||
|
m.Get("/write", write)
|
||||||
|
m.Get("/edit/:id", edit)
|
||||||
|
m.Post("/savePost", savePost)
|
||||||
|
m.Get("/deletePost/:id", deletePost)
|
||||||
|
|
||||||
|
m.Run()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +0,0 @@
|
|||||||
{{ define "footer" }}
|
|
||||||
<script src="bower_components/jquery/dist/jquery.min.js"></script>
|
|
||||||
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
{{ end }}
|
|
@ -1,13 +1,8 @@
|
|||||||
{{ define "index" }}
|
|
||||||
|
|
||||||
{{ template "header" }}
|
|
||||||
|
|
||||||
{{ range $key, $value := . }}
|
{{ range $key, $value := . }}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
<div class="col-xs-8">
|
<div class="col-xs-8">
|
||||||
<h1><a href ="/edit?id={{$value.Id}}">{{ $value.Title }}</a></h1>
|
<h1><a href ="/edit/{{$value.Id}}">{{ $value.Title }}</a></h1>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-xs-2"></div>
|
<div class="col-xs-2"></div>
|
||||||
</div>
|
</div>
|
||||||
@ -22,7 +17,3 @@
|
|||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
|
||||||
{{ template "footer" }}
|
|
||||||
|
|
||||||
{{ end }}
|
|
@ -1,12 +1,11 @@
|
|||||||
{{ define "header" }}
|
|
||||||
|
|
||||||
<!DOCTYPE HTML>
|
<!DOCTYPE HTML>
|
||||||
<html lang="pl">
|
<html lang="pl">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<title>BLOOG</title>
|
<title>BLOOG</title>
|
||||||
<link rel="stylesheet" href="bower_components/css/app.css">
|
<link rel="stylesheet" href="/bower_components/css/app.css">
|
||||||
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
|
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
|
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
|
||||||
@ -28,9 +27,10 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{{ yield }}
|
||||||
|
|
||||||
|
<script src="/bower_components/jquery/dist/jquery.min.js"></script>
|
||||||
|
<script src="/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
{{ end }}
|
|
@ -1,6 +1,3 @@
|
|||||||
{{ define "write" }}
|
|
||||||
|
|
||||||
{{ template "header" }}
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-xs-4"></div>
|
<div class="col-xs-4"></div>
|
||||||
<div class="col-xs-4">
|
<div class="col-xs-4">
|
||||||
@ -19,11 +16,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="col-xs-4">
|
<div class="col-xs-4">
|
||||||
{{ if.Id }}
|
{{ if.Id }}
|
||||||
<a href="deletePost?id={{.Id}}">Delete</a>
|
<a href="/deletePost/{{.Id}}">Delete</a>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{{ template "footer" }}
|
|
||||||
|
|
||||||
{{ end }}
|
|
Loading…
Reference in New Issue
Block a user