13.12.2018

This commit is contained in:
Mikolaj 2018-12-13 18:13:47 +01:00
parent e0b131be8f
commit 9a02f795a0
5 changed files with 45 additions and 80 deletions

86
main.go
View File

@ -2,61 +2,36 @@ package main
import (
"fmt"
"html/template"
"net/http"
"git.wmi.amu.edu.pl/s439508/Pracownia.Programowania/helpers"
"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
func index(w http.ResponseWriter, r *http.Request) {
fmt.Println(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 index(rend render.Render) {
rend.HTML(http.StatusOK, "index", posts)
}
func write(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(
"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 write(rend render.Render) {
rend.HTML(http.StatusOK, "write", &models.Post{})
}
func edit(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(
"views/index.html",
"views/header.html",
"views/footer.html",
"views/write.html",
)
if err != nil {
fmt.Fprintf(w, err.Error())
}
id := r.FormValue("id")
func edit(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"]
post, found := posts[id]
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")
title := r.FormValue("title")
content := r.FormValue("content")
@ -71,30 +46,41 @@ func savePost(w http.ResponseWriter, r *http.Request) {
posts[post.Id] = post
}
http.Redirect(w, r, "/", 302)
rend.Redirect("/")
}
func deletePost(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
func deletePost(rend render.Render, w http.ResponseWriter, r *http.Request, params martini.Params) {
id := params["id"]
if id == "" {
http.NotFound(w, r)
}
delete(posts, id)
http.Redirect(w, r, "/", 302)
rend.Redirect("/")
}
func main() {
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/"))))
http.HandleFunc("/", index)
http.HandleFunc("/write", write)
http.HandleFunc("/edit", edit)
http.HandleFunc("/savePost", savePost)
http.HandleFunc("/deletePost", deletePost)
fmt.Println("Listening port 3000")
m := martini.Classic()
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()
}

View File

@ -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 }}

View File

@ -1,13 +1,8 @@
{{ define "index" }}
{{ template "header" }}
{{ range $key, $value := . }}
<div class="row">
<div class="col-xs-2"></div>
<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 class="col-xs-2"></div>
</div>
@ -22,7 +17,3 @@
</div>
{{end}}
{{ template "footer" }}
{{ end }}

View File

@ -1,12 +1,11 @@
{{ define "header" }}
<!DOCTYPE HTML>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>BLOOG</title>
<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/css/app.css">
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
@ -28,9 +27,10 @@
</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>
</html>
{{ end }}

View File

@ -1,6 +1,3 @@
{{ define "write" }}
{{ template "header" }}
<div class="row">
<div class="col-xs-4"></div>
<div class="col-xs-4">
@ -19,11 +16,8 @@
</div>
<div class="col-xs-4">
{{ if.Id }}
<a href="deletePost?id={{.Id}}">Delete</a>
<a href="/deletePost/{{.Id}}">Delete</a>
{{ end }}
</div>
</div>
{{ template "footer" }}
{{ end }}