hnakra/examples/board/main.go
2023-05-27 03:57:27 -04:00

92 lines
2.0 KiB
Go

package main
import "sync"
import "path"
import "net/http"
import "html/template"
import "hnakra/service"
type Post struct {
Author, Content string
Next *Post
}
type Board struct {
service.Service
root string
mux *http.ServeMux
template *template.Template
postsMutex sync.RWMutex
posts *Post
}
func main () {
board := Board { root: "/board/" }
board.mux = http.NewServeMux()
board.Service = service.Service {
&service.HTTP {
Mount: service.M (
"Board",
"A board where you can post things.",
"@", board.root),
Handler: board.mux,
},
}
handle := func (pattern string, handler func (http.ResponseWriter, *http.Request)) {
board.mux.HandleFunc(pattern, handler)
}
handle(board.root, board.serveHome)
handle(path.Join(board.root, "actionPost"), board.serveActionPost)
board.template = template.Must(template.New("board").Parse(tem))
board.Run()
}
func (board *Board) getPosts (max int) []*Post {
board.postsMutex.RLock()
defer board.postsMutex.RUnlock()
posts := make([]*Post, max)
count := 0
current := board.posts
for current != nil && count < max {
posts[count] = current
count ++
current = current.Next
}
return posts[:count]
}
func (board *Board) serveHome (res http.ResponseWriter, req *http.Request) {
if req.URL.Path != board.root {
http.NotFoundHandler().ServeHTTP(res, req)
return
}
res.Header().Add("content-type", "text/html")
res.WriteHeader(http.StatusOK)
board.template.ExecuteTemplate(res, "pageHome", board.getPosts(16))
}
func (board *Board) serveActionPost (res http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
res.Header().Add("content-type", "text/plain")
res.WriteHeader(http.StatusMethodNotAllowed)
res.Write([]byte("Method not allowed"))
return
}
defer http.Redirect(res, req, board.root, http.StatusSeeOther)
req.ParseForm()
author := req.PostFormValue("author")
content := req.PostFormValue("content")
board.posts = &Post {
Author: author,
Content: content,
Next: board.posts,
}
}