Compare commits

..

No commits in common. "d6fe1544e09fc316361d3395916c7897ef7349ad" and "2c7634da43cb43c44ca3912991a2358fd4083cbe" have entirely different histories.

3 changed files with 37 additions and 52 deletions

View File

@ -26,7 +26,6 @@ CREATE USER threadr IDENTIFIED BY 'super secure password';
CREATE DATABASE `threadr`; CREATE DATABASE `threadr`;
GRANT ALL PRIVILEGES ON `threadr`.* TO 'threadr'; GRANT ALL PRIVILEGES ON `threadr`.* TO 'threadr';
``` ```
Note: The config file contains secrets. Keep `config/config.json` out of version control and only commit `config/config.json.sample`.
2. Create a config file: In the `config` subdirectory, `cp config.json.sample config.json` and edit it to suit your needs. 2. Create a config file: In the `config` subdirectory, `cp config.json.sample config.json` and edit it to suit your needs.
3. Create an about page: Also in the `config` subdirectory, `cp about_page.htmlbody.sample about_page.htmlbody` and edit it to suit your needs. 3. Create an about page: Also in the `config` subdirectory, `cp about_page.htmlbody.sample about_page.htmlbody` and edit it to suit your needs.

View File

@ -1,11 +1,11 @@
{ {
"domain_name": "localhost", "domain_name": "localhost",
"threadr_dir": "/threadr", "threadr_dir": "/threadr",
"db_username": "threadr", "db_username": "threadr_user",
"db_password": "change-me", "db_password": "threadr_password",
"db_database": "threadr", "db_database": "threadr_db",
"db_svr_host": "localhost:3306", "db_svr_host": "localhost:3306",
"file_storage_dir": "files", "file_storage_dir": "files",
"session_secret": "change-me-to-32-byte-random-string", "session_secret": "change-me-to-32-byte-random",
"session_secure": false "session_secure": false
} }

80
main.go
View File

@ -356,35 +356,28 @@ func main() {
log.Fatal("Error getting working directory:", err) log.Fatal("Error getting working directory:", err)
} }
// Parse base and partial templates // Parse partial templates
tmpl := template.Must(template.ParseFiles( tmpl := template.Must(template.ParseFiles(
filepath.Join(dir, "templates/base.html"),
filepath.Join(dir, "templates/partials/navbar.html"), filepath.Join(dir, "templates/partials/navbar.html"),
filepath.Join(dir, "templates/partials/cookie_banner.html"), filepath.Join(dir, "templates/partials/cookie_banner.html"),
)) ))
// Parse page-specific templates with unique names // Parse page-specific templates with unique names
pageTemplates := []string{ tmpl, err = tmpl.ParseFiles(
"about.html", filepath.Join(dir, "templates/pages/about.html"),
"board.html", filepath.Join(dir, "templates/pages/board.html"),
"boards.html", filepath.Join(dir, "templates/pages/boards.html"),
"home.html", filepath.Join(dir, "templates/pages/home.html"),
"login.html", filepath.Join(dir, "templates/pages/login.html"),
"news.html", filepath.Join(dir, "templates/pages/news.html"),
"profile.html", filepath.Join(dir, "templates/pages/profile.html"),
"profile_edit.html", filepath.Join(dir, "templates/pages/profile_edit.html"),
"signup.html", filepath.Join(dir, "templates/pages/signup.html"),
"thread.html", filepath.Join(dir, "templates/pages/thread.html"),
"userhome.html", filepath.Join(dir, "templates/pages/userhome.html"),
"chat.html", filepath.Join(dir, "templates/pages/chat.html"),
"preferences.html", filepath.Join(dir, "templates/pages/preferences.html"),
} )
pagePaths := make([]string, 0, len(pageTemplates))
for _, name := range pageTemplates {
pagePaths = append(pagePaths, filepath.Join(dir, "templates/pages", name))
}
tmpl, err = tmpl.ParseFiles(pagePaths...)
if err != nil { if err != nil {
log.Fatal("Error parsing page templates:", err) log.Fatal("Error parsing page templates:", err)
} }
@ -411,30 +404,23 @@ func main() {
fs := http.FileServer(http.Dir("static")) fs := http.FileServer(http.Dir("static"))
http.Handle(config.ThreadrDir+"/static/", http.StripPrefix(config.ThreadrDir+"/static/", fs)) http.Handle(config.ThreadrDir+"/static/", http.StripPrefix(config.ThreadrDir+"/static/", fs))
handle := func(path string, handler http.HandlerFunc) { http.HandleFunc(config.ThreadrDir+"/", app.SessionMW(handlers.HomeHandler(app)))
http.HandleFunc(config.ThreadrDir+path, app.SessionMW(handler)) http.HandleFunc(config.ThreadrDir+"/login/", app.SessionMW(handlers.LoginHandler(app)))
} http.HandleFunc(config.ThreadrDir+"/logout/", app.SessionMW(handlers.LogoutHandler(app)))
handleAuthed := func(path string, handler http.HandlerFunc) { http.HandleFunc(config.ThreadrDir+"/userhome/", app.SessionMW(app.RequireLoginMW(handlers.UserHomeHandler(app))))
http.HandleFunc(config.ThreadrDir+path, app.SessionMW(app.RequireLoginMW(handler))) http.HandleFunc(config.ThreadrDir+"/boards/", app.SessionMW(handlers.BoardsHandler(app)))
} http.HandleFunc(config.ThreadrDir+"/board/", app.SessionMW(handlers.BoardHandler(app)))
http.HandleFunc(config.ThreadrDir+"/thread/", app.SessionMW(handlers.ThreadHandler(app)))
handle("/", handlers.HomeHandler(app)) http.HandleFunc(config.ThreadrDir+"/about/", app.SessionMW(handlers.AboutHandler(app)))
handle("/login/", handlers.LoginHandler(app)) http.HandleFunc(config.ThreadrDir+"/profile/", app.SessionMW(app.RequireLoginMW(handlers.ProfileHandler(app))))
handle("/logout/", handlers.LogoutHandler(app)) http.HandleFunc(config.ThreadrDir+"/profile/edit/", app.SessionMW(app.RequireLoginMW(handlers.ProfileEditHandler(app))))
handleAuthed("/userhome/", handlers.UserHomeHandler(app)) http.HandleFunc(config.ThreadrDir+"/preferences/", app.SessionMW(app.RequireLoginMW(handlers.PreferencesHandler(app))))
handle("/boards/", handlers.BoardsHandler(app)) http.HandleFunc(config.ThreadrDir+"/like/", app.SessionMW(app.RequireLoginMW(handlers.LikeHandler(app))))
handle("/board/", handlers.BoardHandler(app)) http.HandleFunc(config.ThreadrDir+"/news/", app.SessionMW(handlers.NewsHandler(app)))
handle("/thread/", handlers.ThreadHandler(app)) http.HandleFunc(config.ThreadrDir+"/signup/", app.SessionMW(handlers.SignupHandler(app)))
handle("/about/", handlers.AboutHandler(app)) http.HandleFunc(config.ThreadrDir+"/accept_cookie/", app.SessionMW(handlers.AcceptCookieHandler(app)))
handleAuthed("/profile/", handlers.ProfileHandler(app)) http.HandleFunc(config.ThreadrDir+"/chat/", app.SessionMW(app.RequireLoginMW(handlers.ChatHandler(app))))
handleAuthed("/profile/edit/", handlers.ProfileEditHandler(app)) http.HandleFunc(config.ThreadrDir+"/file", app.SessionMW(handlers.FileHandler(app)))
handleAuthed("/preferences/", handlers.PreferencesHandler(app))
handleAuthed("/like/", handlers.LikeHandler(app))
handle("/news/", handlers.NewsHandler(app))
handle("/signup/", handlers.SignupHandler(app))
handle("/accept_cookie/", handlers.AcceptCookieHandler(app))
handleAuthed("/chat/", handlers.ChatHandler(app))
handle("/file", handlers.FileHandler(app))
log.Println("Server starting on :8080") log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(":8080", nil))