10.12.2018

This commit is contained in:
Mikolaj 2018-12-10 19:42:28 +01:00
parent 4091075186
commit 83a5778208
5 changed files with 78 additions and 1 deletions

View File

@ -0,0 +1,8 @@
body {
padding-top: 65px;
}
textarea {
width: 100%;
height: 200px;
}

32
main.go
View File

@ -4,9 +4,26 @@ import (
"fmt"
"html/template"
"net/http"
"github.com/blog/models"
)
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",
)
if err != nil {
fmt.Fprintf(w, err.Error())
}
t.ExecuteTemplate(w, "write", nil)
}
func write(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles(
"views/index.html",
"views/header.html",
@ -18,11 +35,24 @@ func index(w http.ResponseWriter, r *http.Request) {
t.ExecuteTemplate(w, "index", nil)
}
func savePost(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
title := r.FormValue("title")
content := r.FormValue("content")
post := models.NewPost(id, title, content)
post[post.Id] = post
http.Redirect(w, r, "/", 302)
}
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.Handle("/bower_components/", http.StripPrefix("/bower_components/", http.FileServer(http.Dir("./bower_components/"))))
http.HandleFunc("/", index)
http.HandleFunc("/write", write)
http.HandleFunc("/savePost", savePost)
http.ListenAndServe(":8080", nil)
}

View File

@ -23,6 +23,7 @@
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="/">Home</a></li>
<li><a href="/write">New Post</a></li>
</ul>
</div>
</div>

11
views/models/post.go Normal file
View File

@ -0,0 +1,11 @@
package models
type Psot struct {
Id string
Title string
Content string
}
func NewPost(id, title, content string) *Post {
return &Post{id, title, content}
}

27
views/write.html Normal file
View File

@ -0,0 +1,27 @@
{{ define "write" }}
{{ template "header" }}
<div class="row">
<div class="col-xs-4"></div>
<div class="col-xs-4">
<form action="/savePost" method="POST" role="form">
<input type="hidden" name="id"/>
<div class="form-group">
<label for="title">Tytul</label>
<input type="text" class="form-control" id="title" name="title"/>
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea name="content" id="content"></textarea>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="col-xs-4">
<a href="deletePost">Usun</a>
</div>
</div>
{{ template "footer" }}
{{ end }}