package main import ( "database/sql" "html/template" "log" "net/http" _ "modernc.org/sqlite" ) type user struct { id uint name string handle string pass string email string about string } type persona struct { id uint userid uint handle string name string about string colour string } type Post struct { Id uint Time uint Userid uint Personaid uint Text string PersonaName string UserName string PersonaHandle string UserHandle string } type comment struct { id uint time uint userid uint personaid uint postid uint text string } type page struct { Title string Posts []Post } var db *sql.DB func main() { var err error db, err = sql.Open("sqlite", "database.db") if err != nil { log.Fatal(err) } http.Handle("/static/", http.FileServer(http.Dir("./"))) http.HandleFunc("/", httpRootHandler) err = http.ListenAndServe(":6900", nil) if err != nil { log.Fatal(err) } db.Close() } func httpRootHandler(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("index.html") posts, err := getPosts() if err != nil { log.Fatal(err) http.Error(w, http.StatusText(500), 500) return } var p page p.Title = "Snuffler" p.Posts = posts t.Execute(w, p) } func getPosts() ([]Post, error) { rows, err := db.Query("SELECT post.id, post.time, post.userid, post.personaid, post.text, user.name, user.handle, persona.name, persona.handle FROM posts AS post LEFT JOIN users AS user ON post.userid=user.id LEFT JOIN personas AS persona ON post.personaid=persona.id") if err != nil { log.Fatal(err) return nil, err } var posts []Post for rows.Next() { var post Post err := rows.Scan(&post.Id, &post.Time, &post.Userid, &post.Personaid, &post.Text, &post.UserName, &post.UserHandle, &post.PersonaName, &post.PersonaHandle) if err != nil { log.Fatal(err) return nil, err } posts = append(posts, post) } err = rows.Err() if err != nil { log.Fatal(err) return nil, err } return posts, nil }