Added documentation to functions and basic JWT generation

This commit is contained in:
2025-02-09 00:48:30 +11:00
parent 597fc6f072
commit 25868becf3
29 changed files with 254 additions and 58 deletions

View File

@@ -2,9 +2,12 @@ package handlers
import (
"net/http"
"projectreshoot/view/page"
)
// Handles responses to the / path. Also serves a 404 Page for paths that
// don't have explicit handlers
func HandleRoot() http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {

View File

@@ -13,6 +13,8 @@ import (
"github.com/pkg/errors"
)
// Validates the username matches a user in the database and the password
// is correct. Returns the corresponding user
func validateLogin(conn *sql.DB, r *http.Request) (db.User, error) {
formUsername := r.FormValue("username")
formPassword := r.FormValue("password")
@@ -29,6 +31,7 @@ func validateLogin(conn *sql.DB, r *http.Request) (db.User, error) {
return user, nil
}
// Returns result of the "Remember me?" checkbox as a boolean
func checkRememberMe(r *http.Request) bool {
rememberMe := r.FormValue("remember-me")
if rememberMe == "on" {
@@ -38,7 +41,10 @@ func checkRememberMe(r *http.Request) bool {
}
}
func HandleLoginRequest(conn *sql.DB) http.Handler {
// Handles an attempted login request. On success will return a HTMX redirect
// and on fail will return the login form again, passing the error to the
// template for user feedback
func HandleLoginRequest(conn *sql.DB, secretKey string) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
@@ -62,6 +68,8 @@ func HandleLoginRequest(conn *sql.DB) http.Handler {
)
}
// Handles a request to view the login page. Will attempt to set "pagefrom"
// cookie so a successful login can redirect the user to the page they came
func HandleLoginPage(trustedHost string) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {

View File

@@ -1,10 +1,13 @@
package handlers
import (
"github.com/a-h/templ"
"net/http"
"github.com/a-h/templ"
)
// Handler for static pages. Will render the given templ.Component to the
// http.ResponseWriter
func HandlePage(Page templ.Component) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {

View File

@@ -5,26 +5,43 @@ import (
"os"
)
// Wrapper for default FileSystem
type justFilesFilesystem struct {
fs http.FileSystem
}
// Wrapper for default File
type neuteredReaddirFile struct {
http.File
}
// Modifies the behavior of FileSystem.Open to return the neutered version of File
func (fs justFilesFilesystem) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
// Check if the requested path is a directory
// and explicitly return an error to trigger a 404
fileInfo, err := f.Stat()
if err != nil {
return nil, err
}
if fileInfo.IsDir() {
return nil, os.ErrNotExist
}
return neuteredReaddirFile{f}, nil
}
// Overrides the Readdir method of File to always return nil
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
// Handles requests for static files, without allowing access to the
// directory viewer and returning 404 if an exact file is not found
func HandleStatic() http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {