templates don’t like me
This commit is contained in:
parent
f7b775cb38
commit
07d4f36a3f
48
db.go
48
db.go
|
@ -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
4
notes
|
@ -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
|
|
29
server.go
29
server.go
|
@ -7,8 +7,19 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"io"
|
"io"
|
||||||
|
"html/template"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type OutputFormatter struct {
|
||||||
|
Author string
|
||||||
|
Content string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Wrapper struct {
|
||||||
|
Post OutputFormatter
|
||||||
|
}
|
||||||
|
|
||||||
func startServer() {
|
func startServer() {
|
||||||
http.HandleFunc("/", get)
|
http.HandleFunc("/", get)
|
||||||
http.HandleFunc("/add", post)
|
http.HandleFunc("/add", post)
|
||||||
|
@ -16,11 +27,29 @@ func startServer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(w http.ResponseWriter, r *http.Request) {
|
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.WriteHeader(http.StatusOK)
|
||||||
|
/*
|
||||||
w.Write([]byte("response:\n"))
|
w.Write([]byte("response:\n"))
|
||||||
for _, post := range readBlogpostsFromDB() {
|
for _, post := range readBlogpostsFromDB() {
|
||||||
w.Write([]byte(post.Content))
|
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) {
|
func post(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue
Block a user