commit 77786d2328c4c43466eebc9b8706ed64a6f3123d
parent 58514d59cb487aba1101323bda78aeb64448c281
Author: Alexander Cobleigh <cblgh@cblgh.org>
Date: Sun, 18 Sep 2022 15:10:50 +0200
Merge pull request #35 from eyedeekay/main
adds an object implementing http.ServeMux to allow for use with alternate listeners(TLS, Onion, I2P, etc)
Diffstat:
M | server/server.go | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
1 file changed, 62 insertions(+), 10 deletions(-)
diff --git a/server/server.go b/server/server.go
@@ -6,8 +6,11 @@ import (
"errors"
"fmt"
"html/template"
+ "log"
"net/http"
"net/url"
+ "os"
+ "path/filepath"
"strings"
"time"
@@ -31,9 +34,9 @@ type TemplateData struct {
}
type PasswordResetData struct {
- Action string
- Username string
- Payload string
+ Action string
+ Username string
+ Payload string
}
type IndexData struct {
@@ -317,13 +320,13 @@ func (h RequestHandler) ResetPasswordRoute(res http.ResponseWriter, req *http.Re
renderErr := func(errFmt string, args ...interface{}) {
errMessage := fmt.Sprintf(errFmt, args...)
fmt.Println(errMessage)
- data := GenericMessageData{
- Title: "Reset password",
- Message: errMessage,
- Link: "/reset",
- LinkText: "Go back",
- }
- h.renderView(res, "generic-message", TemplateData{Data: data, Title: "password reset"})
+ data := GenericMessageData{
+ Title: "Reset password",
+ Message: errMessage,
+ Link: "/reset",
+ LinkText: "Go back",
+ }
+ h.renderView(res, "generic-message", TemplateData{Data: data, Title: "password reset"})
}
switch req.Method {
@@ -692,3 +695,52 @@ func Serve(allowlist []string, sessionKey string, isdev bool) {
fmt.Println("Serving forum on", port)
http.ListenAndServe(port, nil)
}
+
+// CercaForum is an HTTP.ServeMux which is set up to initialize and run
+// a cerca-based forum. Software developers who wish to customize the
+// networks and security which they use to operate may wish to use this
+// to listen with TLS, Onion, or I2P addresses without manual setup.
+type CercaForum struct {
+ http.ServeMux
+ Directory string
+}
+
+func (u *CercaForum) directory() string {
+ if u.Directory == "" {
+ dir, err := os.Getwd()
+ if err != nil {
+ log.Fatal(err)
+ }
+ u.Directory = filepath.Join(dir, "CercaForum")
+ }
+ os.MkdirAll(u.Directory, 0755)
+ return u.Directory
+}
+
+// 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) {
+ s := &CercaForum{
+ ServeMux: http.ServeMux{},
+ }
+ dbpath := filepath.Join(s.Directory, "forum.db")
+
+ db := database.InitDB(dbpath)
+ handler := RequestHandler{&db, session.New(sessionKey, developing), allowlist}
+ s.ServeMux.HandleFunc("/reset/", handler.ResetPasswordRoute)
+ s.ServeMux.HandleFunc("/about", handler.AboutRoute)
+ s.ServeMux.HandleFunc("/logout", handler.LogoutRoute)
+ s.ServeMux.HandleFunc("/login", handler.LoginRoute)
+ s.ServeMux.HandleFunc("/register", handler.RegisterRoute)
+ s.ServeMux.HandleFunc("/post/delete/", handler.DeletePostRoute)
+ s.ServeMux.HandleFunc("/thread/new/", handler.NewThreadRoute)
+ s.ServeMux.HandleFunc("/thread/", handler.ThreadRoute)
+ s.ServeMux.HandleFunc("/robots.txt", handler.RobotsRoute)
+ s.ServeMux.HandleFunc("/", handler.IndexRoute)
+
+ fileserver := http.FileServer(http.Dir("html/assets/"))
+ s.ServeMux.Handle("/assets/", http.StripPrefix("/assets/", fileserver))
+
+ return s, nil
+}