cerca

lean forum software (pmc local branch)
Log | Files | Refs | README | LICENSE

commit 946d49d1cee65e1c1b5692535c7e12582dc2c6e8
parent 77786d2328c4c43466eebc9b8706ed64a6f3123d
Author: cblgh <cblgh@cblgh.org>
Date:   Sun, 18 Sep 2022 15:38:15 +0200

use new CercaForum constructor as part of Serve

Diffstat:
Mserver/server.go | 43+++++++++++++++++++++----------------------
1 file changed, 21 insertions(+), 22 deletions(-)

diff --git a/server/server.go b/server/server.go @@ -7,6 +7,7 @@ import ( "fmt" "html/template" "log" + "net" "net/http" "net/url" "os" @@ -667,33 +668,24 @@ func (h RequestHandler) DeletePostRoute(res http.ResponseWriter, req *http.Reque func Serve(allowlist []string, sessionKey string, isdev bool) { port := ":8272" - dbpath := "./data/forum.db" + dir := "./data/" if isdev { developing = true - dbpath = "./data/forum.test.db" + dir = "./testdata/" port = ":8277" } - db := database.InitDB(dbpath) - handler := RequestHandler{&db, session.New(sessionKey, developing), allowlist} - /* note: be careful with trailing slashes; go's default handler is a bit sensitive */ - // TODO (2022-01-10): introduce middleware to make sure there is never an issue with trailing slashes - http.HandleFunc("/reset/", handler.ResetPasswordRoute) - http.HandleFunc("/about", handler.AboutRoute) - http.HandleFunc("/logout", handler.LogoutRoute) - http.HandleFunc("/login", handler.LoginRoute) - http.HandleFunc("/register", handler.RegisterRoute) - http.HandleFunc("/post/delete/", handler.DeletePostRoute) - http.HandleFunc("/thread/new/", handler.NewThreadRoute) - http.HandleFunc("/thread/", handler.ThreadRoute) - http.HandleFunc("/robots.txt", handler.RobotsRoute) - http.HandleFunc("/", handler.IndexRoute) - - fileserver := http.FileServer(http.Dir("html/assets/")) - http.Handle("/assets/", http.StripPrefix("/assets/", fileserver)) + forum, err := NewServer(allowlist, sessionKey, dir) + if err != nil { + util.Check(err, "instantiate CercaForum") + } + l, err := net.Listen("tcp", port) + if err != nil { + util.Check(err, "setting up tcp listener") + } fmt.Println("Serving forum on", port) - http.ListenAndServe(port, nil) + http.Serve(l, forum) } // CercaForum is an HTTP.ServeMux which is set up to initialize and run @@ -720,13 +712,20 @@ func (u *CercaForum) directory() string { // NewServer sets up a new CercaForum object. Always use this to initialize // new CercaForum objects. Pass the result to http.Serve() with your choice // of net.Listener. -func NewServer(allowlist []string, sessionKey string) (*CercaForum, error) { +func NewServer(allowlist []string, sessionKey, dir string) (*CercaForum, error) { s := &CercaForum{ ServeMux: http.ServeMux{}, } - dbpath := filepath.Join(s.Directory, "forum.db") + if dir != "" { + s.Directory = dir + } + + dbpath := filepath.Join(s.directory(), "forum.db") db := database.InitDB(dbpath) + + /* note: be careful with trailing slashes; go's default handler is a bit sensitive */ + // TODO (2022-01-10): introduce middleware to make sure there is never an issue with trailing slashes handler := RequestHandler{&db, session.New(sessionKey, developing), allowlist} s.ServeMux.HandleFunc("/reset/", handler.ResetPasswordRoute) s.ServeMux.HandleFunc("/about", handler.AboutRoute)