Compare commits

..

No commits in common. "master" and "dev" have entirely different histories.
master ... dev

6 changed files with 18 additions and 53 deletions

14
db.go
View File

@ -20,14 +20,14 @@ func initializeDatabase() {
} }
func writeToDB(post BlogPost) { func writeToDB(post BlogPost) {
stmt, _ := db.Prepare("INSERT INTO blogposts(author, content, time) VALUES (?, ?, ?)") stmt, _ := db.Prepare("INSERT INTO blogposts(author, title, content, time) VALUES (?, ?, ?, ?)")
stmt.Exec(post.Author, post.Content, time.Now().Unix()) stmt.Exec(post.Author, post.Title, post.Content, time.Now().Unix())
} }
func readBlogpostsFromDB() []BlogPost { func readBlogpostsFromDB() []BlogPost {
res, err := db.Query("SELECT content, author, time from blogposts limit 50") res, err := db.Query("SELECT content, author, title, time from blogposts")
if err != nil { if err != nil {
log.Println("Error reading blogposts", err) log.Println("Error reading blogposts")
return make([]BlogPost, 0) return make([]BlogPost, 0)
} else { } else {
return resultToBlogposts(res) return resultToBlogposts(res)
@ -38,10 +38,10 @@ func resultToBlogposts(res *sql.Rows) []BlogPost {
defer res.Close() defer res.Close()
entries := make([]BlogPost, 0) entries := make([]BlogPost, 0)
for res.Next() { for res.Next() {
var content, author string var content, author, title string
var timeInt int64 var timeInt int64
res.Scan(&content, &author, &timeInt) res.Scan(&content, &author, &title, &timeInt)
post := BlogPost{Content:content, Author:author, FTime:time.Unix(timeInt, 0).Format("02.01. 15:04:05")} post := BlogPost{Content:content, Author:author, Title:title, Time:time.Unix(timeInt, 0)}
entries = append(entries, post) entries = append(entries, post)
} }
return entries return entries

View File

@ -16,9 +16,9 @@ func main() {
type BlogPost struct { type BlogPost struct {
Id int64 `"id"` Id int64 `"id"`
Content string `"content"` Content string `"content"`
Title string `"title"`
Author string `"author"` Author string `"author"`
Secret string `"secret"` Secret string `"secret"`
Time time.Time `"time"` Time time.Time `"time"`
FTime string `"ftime"`
} }

2
schema
View File

@ -1 +1 @@
CREATE TABLE `blogposts` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `author` VARCHAR(32) NULL, `content` text null, `time` time null); CREATE TABLE `blogposts` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `author` VARCHAR(32) NULL, `title` text NULL, `content` text null, `time` time null);

View File

@ -15,8 +15,8 @@ type OutputFormatter struct {
Content string Content string
} }
type Blog struct { type Wrapper struct {
Posts []BlogPost Post OutputFormatter
} }
func startServer() { func startServer() {
@ -40,7 +40,8 @@ func get(w http.ResponseWriter, r *http.Request) {
} }
*/ */
results := readBlogpostsFromDB() results := readBlogpostsFromDB()
t.ExecuteTemplate(w, "overview.html", Blog{results}) err = t.ExecuteTemplate(w, "overview.html", OutputFormatter{Author: results[0].Author, Content: results[0].Content})
log.Println(err)
} }
func post(w http.ResponseWriter, r *http.Request) { func post(w http.ResponseWriter, r *http.Request) {

View File

@ -1,43 +1,7 @@
<html> <html>
<style> <body style="background-color: #333; color: #ccc;">
body { <div id="content" style="width: 70%; margin: auto; padding-top: 10%;">
background-color: #333; {{.Author}} said: {{.Content}}
color: #ccc;
font-family: "Hack", "Ubuntu Mono", "Lucida Console", monospace;
}
#content {
width: 70%;
margin: auto;
padding-top: 2em;
}
td.first {
width: 20%;
}
td.second {
padding-top: 0;
margin-top: 0;
top: 0;
width: 80%;
}
</style>
<body>
<div id="content">
<h2 style="text-align: center">Random Thought Dump</h2>
<table>
{{range $i, $p := $.Posts -}}
<tr>
<td>
[{{$p.FTime}}] &lt;{{$p.Author}}&gt;
</td>
<td>
{{$p.Content}}
</td>
</tr>
{{- end}}
<table>
</div> </div>
</body> </body>
</html> </html>

View File

@ -1,9 +1,9 @@
if [ -z ${1+x} ]; then if [ -z ${1+x} ]; then
read msg msg="test message"
else else
msg="$1" msg="$1"
fi fi
curl localhost:12345/add -d "{\"content\": \"$msg\", \"Secret\": \"asdf\", \"author\": \"kageru\"}" -H "Content-Type: application/json" curl localhost:12345/add -d "{\"content\": \"$msg\", \"Title\": \"title\", \"Secret\": \"asdf\", \"author\": \"kageru\"}" -H "Content-Type: application/json" -v