Compare commits
No commits in common. "master" and "dev" have entirely different histories.
14
db.go
14
db.go
|
@ -20,14 +20,14 @@ func initializeDatabase() {
|
|||
}
|
||||
|
||||
func writeToDB(post BlogPost) {
|
||||
stmt, _ := db.Prepare("INSERT INTO blogposts(author, content, time) VALUES (?, ?, ?)")
|
||||
stmt.Exec(post.Author, post.Content, time.Now().Unix())
|
||||
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, time from blogposts limit 50")
|
||||
res, err := db.Query("SELECT content, author, title, time from blogposts")
|
||||
if err != nil {
|
||||
log.Println("Error reading blogposts", err)
|
||||
log.Println("Error reading blogposts")
|
||||
return make([]BlogPost, 0)
|
||||
} else {
|
||||
return resultToBlogposts(res)
|
||||
|
@ -38,10 +38,10 @@ func resultToBlogposts(res *sql.Rows) []BlogPost {
|
|||
defer res.Close()
|
||||
entries := make([]BlogPost, 0)
|
||||
for res.Next() {
|
||||
var content, author string
|
||||
var content, author, title string
|
||||
var timeInt int64
|
||||
res.Scan(&content, &author, &timeInt)
|
||||
post := BlogPost{Content:content, Author:author, FTime:time.Unix(timeInt, 0).Format("02.01. 15:04:05")}
|
||||
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
|
||||
|
|
2
main.go
2
main.go
|
@ -16,9 +16,9 @@ func main() {
|
|||
type BlogPost struct {
|
||||
Id int64 `"id"`
|
||||
Content string `"content"`
|
||||
Title string `"title"`
|
||||
Author string `"author"`
|
||||
Secret string `"secret"`
|
||||
Time time.Time `"time"`
|
||||
FTime string `"ftime"`
|
||||
}
|
||||
|
||||
|
|
2
schema
2
schema
|
@ -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);
|
||||
|
|
|
@ -15,8 +15,8 @@ type OutputFormatter struct {
|
|||
Content string
|
||||
}
|
||||
|
||||
type Blog struct {
|
||||
Posts []BlogPost
|
||||
type Wrapper struct {
|
||||
Post OutputFormatter
|
||||
}
|
||||
|
||||
func startServer() {
|
||||
|
@ -40,7 +40,8 @@ func get(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
*/
|
||||
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) {
|
||||
|
|
|
@ -1,43 +1,7 @@
|
|||
<html>
|
||||
<style>
|
||||
body {
|
||||
background-color: #333;
|
||||
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}}] <{{$p.Author}}>
|
||||
</td>
|
||||
<td>
|
||||
{{$p.Content}}
|
||||
</td>
|
||||
</tr>
|
||||
{{- end}}
|
||||
<table>
|
||||
<body style="background-color: #333; color: #ccc;">
|
||||
<div id="content" style="width: 70%; margin: auto; padding-top: 10%;">
|
||||
{{.Author}} said: {{.Content}}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
4
test.sh
4
test.sh
|
@ -1,9 +1,9 @@
|
|||
|
||||
if [ -z ${1+x} ]; then
|
||||
read msg
|
||||
msg="test message"
|
||||
else
|
||||
msg="$1"
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user