Compare commits

...

5 Commits
dev ... master

Author SHA1 Message Date
kageru b44ef17476
Better fonts in template
also no longer list previously-used fonts after a generic because I
never removed them
2018-11-04 22:26:04 +01:00
kageru b941c4f901
Fixed wrong column names in select 2018-11-04 22:22:24 +01:00
kageru ff9ae9eb14
updated publish script 2018-11-04 22:19:15 +01:00
kageru 645756d800
Remove unused title field 2018-11-04 22:14:34 +01:00
kageru 99d20cf1f6
Made template prettier
Also more formatting for the output. This is now actually usable.
2018-11-04 22:05:48 +01:00
6 changed files with 53 additions and 18 deletions

14
db.go
View File

@ -20,14 +20,14 @@ func initializeDatabase() {
}
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())
stmt, _ := db.Prepare("INSERT INTO blogposts(author, content, time) VALUES (?, ?, ?)")
stmt.Exec(post.Author, post.Content, time.Now().Unix())
}
func readBlogpostsFromDB() []BlogPost {
res, err := db.Query("SELECT content, author, title, time from blogposts")
res, err := db.Query("SELECT content, author, time from blogposts limit 50")
if err != nil {
log.Println("Error reading blogposts")
log.Println("Error reading blogposts", err)
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, title string
var content, author string
var timeInt int64
res.Scan(&content, &author, &title, &timeInt)
post := BlogPost{Content:content, Author:author, Title:title, Time:time.Unix(timeInt, 0)}
res.Scan(&content, &author, &timeInt)
post := BlogPost{Content:content, Author:author, FTime:time.Unix(timeInt, 0).Format("02.01. 15:04:05")}
entries = append(entries, post)
}
return entries

View File

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

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

View File

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

View File

@ -1,7 +1,43 @@
<html>
<body style="background-color: #333; color: #ccc;">
<div id="content" style="width: 70%; margin: auto; padding-top: 10%;">
{{.Author}} said: {{.Content}}
<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}}] &lt;{{$p.Author}}&gt;
</td>
<td>
{{$p.Content}}
</td>
</tr>
{{- end}}
<table>
</div>
</body>
</html>

View File

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