templates don’t like me

This commit is contained in:
kageru 2018-11-04 19:58:34 +01:00
parent f7b775cb38
commit 07d4f36a3f
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
5 changed files with 36 additions and 52 deletions

BIN
content Normal file

Binary file not shown.

48
db.go
View File

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

4
notes
View File

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

View File

@ -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) {

7
templates/overview.html Normal file
View File

@ -0,0 +1,7 @@
<html>
<body style="background-color: #333; color: #ccc;">
<div id="content" style="width: 70%; margin: auto; padding-top: 10%;">
{{.}}
</div>
</body>
</html>