From 07d4f36a3fad3cccbdcc85e883701a39ee8b64df Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 4 Nov 2018 19:58:34 +0100 Subject: [PATCH] =?UTF-8?q?templates=20don=E2=80=99t=20like=20me?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content | Bin 0 -> 12288 bytes db.go | 48 ---------------------------------------- notes | 4 ---- server.go | 29 ++++++++++++++++++++++++ templates/overview.html | 7 ++++++ 5 files changed, 36 insertions(+), 52 deletions(-) create mode 100644 content delete mode 100644 db.go delete mode 100644 notes create mode 100644 templates/overview.html diff --git a/content b/content new file mode 100644 index 0000000000000000000000000000000000000000..d4a23af2441d530a1625dc571c1e2e9e60fbe17e GIT binary patch literal 12288 zcmeI&ze~eF6bJCTrs||oER+t?ha$ALIJDy8Vw9l8SZj0oL-Yb8Idxq`Uq;(z4- z;a||*&B5JEvBehL97MiHj(hjsyBj{+K~9dXjudni-uX>QWj4<^XWK-Kv6Pycn!_=r zg5+?cR{kyiDNM2T+|8`2Cgwia+=ssRQzP;afB*y_009U<00Izz00bZafnOJ>C8pDb z0ZxU^lI%!NxFp5B9Hv@fAVna*l0nb&27P|q*6{_!nSA^<*Yp>r zzv-`P!GHh+AOHafKmY;|fB*y_009U<;LiwTv}v}GUP}66@a});wM*rV=(5pYTTLh8 zQf9o==&wAdcwAWgR%rB>Gx7VM>+ei`7!ZH}1Rwwb2tWV=5P$##AOHaf{1<^)u4Vb? H_XF({zl?wh literal 0 HcmV?d00001 diff --git a/db.go b/db.go deleted file mode 100644 index 86514ff..0000000 --- a/db.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "database/sql" - "time" - _ "github.com/mattn/go-sqlite3" - "log" -) - - -var db *sql.DB - -func initializeDatabase() { - dbo, err := sql.Open("sqlite3", "content") - if err != nil { - log.Fatal("couldn’t open database. exiting...") - return - } - db = dbo -} - -func writeToDB(post BlogPost) { - stmt, _ := db.Prepare("INSERT INTO blogposts(author, title, content, time) VALUES (?, ?, ?, ?)") - stmt.Exec(post.Author, post.Title, post.Content, time.Now().Unix()) -} - -func readBlogpostsFromDB() []BlogPost { - res, err := db.Query("SELECT content, author, title, time from blogposts") - if err != nil { - log.Println("Error reading blogposts") - return make([]BlogPost, 0) - } else { - return resultToBlogposts(res) - } -} - -func resultToBlogposts(res *sql.Rows) []BlogPost { - defer res.Close() - entries := make([]BlogPost, 0) - for res.Next() { - var content, author, title string - var timeInt int64 - res.Scan(&content, &author, &title, &timeInt) - post := BlogPost{Content:content, Author:author, Title:title, Time:time.Unix(timeInt, 0)} - entries = append(entries, post) - } - return entries -} diff --git a/notes b/notes deleted file mode 100644 index 143de00..0000000 --- a/notes +++ /dev/null @@ -1,4 +0,0 @@ -curl localhost:12345/add -d '{"content": "a message", "Title": "title", "Secret": "asdawdwd", "author": "me"}' -H 'Content-Type: application/json' -v - -for sqlite3 -CGO_ENABLED=1 diff --git a/server.go b/server.go index 00463cf..95bc337 100644 --- a/server.go +++ b/server.go @@ -7,8 +7,19 @@ import ( "encoding/hex" "crypto/sha512" "io" + "html/template" + "os" ) +type OutputFormatter struct { + Author string + Content string +} + +type Wrapper struct { + Post OutputFormatter +} + func startServer() { http.HandleFunc("/", get) http.HandleFunc("/add", post) @@ -16,11 +27,29 @@ func startServer() { } func get(w http.ResponseWriter, r *http.Request) { + log.Println("rip?") + t := template.New("overview") + //t, err := t.ParseFiles("templates/overview.html") + + t, err := t.Parse("{{.Author}} said: {{.Content}}") + + if err != nil { + log.Panic(err) + } + w.WriteHeader(http.StatusOK) + /* w.Write([]byte("response:\n")) for _, post := range readBlogpostsFromDB() { w.Write([]byte(post.Content)) } + */ + //results := readBlogpostsFromDB() + //t.Execute(w, OutputFormatter{Author: results[0].Author, Content: results[0].Content}) + t.Execute(w, "asdasd") + t.Execute(os.Stdout, "asdasd") + //err = t.Execute(os.Stdout, OutputFormatter{Author: results[0].Author, Content: results[0].Content}) + log.Println(err) } func post(w http.ResponseWriter, r *http.Request) { diff --git a/templates/overview.html b/templates/overview.html new file mode 100644 index 0000000..9b18fe4 --- /dev/null +++ b/templates/overview.html @@ -0,0 +1,7 @@ + + +
+ {{.}} +
+ +