From 83a57782083c610c5ca776544b5a82c40c2a96cd Mon Sep 17 00:00:00 2001 From: Mikolaj Date: Mon, 10 Dec 2018 19:42:28 +0100 Subject: [PATCH] 10.12.2018 --- bower_components/css/app.css | 8 ++++++++ main.go | 32 +++++++++++++++++++++++++++++++- views/header.html | 1 + views/models/post.go | 11 +++++++++++ views/write.html | 27 +++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 views/models/post.go create mode 100644 views/write.html diff --git a/bower_components/css/app.css b/bower_components/css/app.css index e69de29..beb7607 100644 --- a/bower_components/css/app.css +++ b/bower_components/css/app.css @@ -0,0 +1,8 @@ +body { + padding-top: 65px; +} + +textarea { + width: 100%; + height: 200px; +} \ No newline at end of file diff --git a/main.go b/main.go index 5a1bc4c..d71eca3 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/views/header.html b/views/header.html index c62bcd6..64fd9b4 100644 --- a/views/header.html +++ b/views/header.html @@ -23,6 +23,7 @@ diff --git a/views/models/post.go b/views/models/post.go new file mode 100644 index 0000000..4275b29 --- /dev/null +++ b/views/models/post.go @@ -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} +} diff --git a/views/write.html b/views/write.html new file mode 100644 index 0000000..3677cce --- /dev/null +++ b/views/write.html @@ -0,0 +1,27 @@ +{{ define "write" }} + +{{ template "header" }} +
+
+
+
+ +
+ + +
+
+ + +
+ +
+
+
+ Usun +
+
+ +{{ template "footer" }} + +{{ end }} \ No newline at end of file