implemented more logging functionality

This commit is contained in:
Gregory Wells
2026-03-24 20:06:06 -04:00
parent d4512e9cce
commit 92f7c0f127
3 changed files with 56 additions and 15 deletions

View File

@@ -3,6 +3,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"astraltech.xyz/accountmanager/src/logging"
) )
type LDAPConfig struct { type LDAPConfig struct {
@@ -30,12 +32,20 @@ type ServerConfig struct {
} }
func loadServerConfig(path string) (*ServerConfig, error) { func loadServerConfig(path string) (*ServerConfig, error) {
logging.Debugf("Loading server config file: %s", path)
file, err := os.ReadFile(path) file, err := os.ReadFile(path)
if err != nil { if err != nil {
logging.Errorf("Failed to load server config")
logging.Error(err.Error())
return nil, err return nil, err
} }
var cfg ServerConfig var cfg ServerConfig
logging.Debugf("Unmarshaling JSON data")
err = json.Unmarshal(file, &cfg) err = json.Unmarshal(file, &cfg)
return &cfg, err if err != nil {
logging.Error("Failed to read JSON data")
logging.Error(err.Error())
}
return &cfg, nil
} }

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"net/http"
"os" "os"
"astraltech.xyz/accountmanager/src/logging" "astraltech.xyz/accountmanager/src/logging"
@@ -29,3 +30,29 @@ func ReadRequiredFile(path string) []byte {
logging.Infof("Successfully read file at %s", path) logging.Infof("Successfully read file at %s", path)
return data return data
} }
func Mkdir(path string, perm os.FileMode) error {
logging.Infof("Making directory %s", path)
err := os.Mkdir(path, perm)
if err != nil {
logging.Errorf("Failed to make %s directory", path)
logging.Error(err.Error())
return err
}
return nil
}
func CreateFile(path string) (*os.File, error) {
logging.Infof("Creating %s", path)
file, err := os.Create(path)
if err != nil {
logging.Errorf("Faile to create %s", path)
logging.Error(err.Error())
}
return file, nil
}
func HandleFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
logging.Infof("Handling %s", path)
http.HandleFunc(path, handler)
}

View File

@@ -35,20 +35,20 @@ var (
) )
func createUserPhoto(username string, photoData []byte) error { func createUserPhoto(username string, photoData []byte) error {
os.Mkdir("./avatars", os.ModePerm) Mkdir("./avatars", os.ModePerm)
path := fmt.Sprintf("./avatars/%s.jpeg", username) path := fmt.Sprintf("./avatars/%s.jpeg", username)
cleaned := filepath.Clean(path) cleaned := filepath.Clean(path)
dst, err := os.Create(cleaned) dst, err := CreateFile(cleaned)
if err != nil { if err != nil {
fmt.Printf("Not saving file\n")
return fmt.Errorf("Could not save file") return fmt.Errorf("Could not save file")
} }
photoCreatedMutex.Lock() photoCreatedMutex.Lock()
photoCreatedTimestamp[username] = time.Now() photoCreatedTimestamp[username] = time.Now()
photoCreatedMutex.Unlock() photoCreatedMutex.Unlock()
defer dst.Close() defer dst.Close()
logging.Info("Writing to avarar file")
_, err = dst.Write(photoData) _, err = dst.Write(photoData)
if err != nil { if err != nil {
return err return err
@@ -98,9 +98,12 @@ type LoginPageData struct {
} }
func loginHandler(w http.ResponseWriter, r *http.Request) { func loginHandler(w http.ResponseWriter, r *http.Request) {
logging.Info("Handing login page")
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl := template.Must(template.ParseFiles("src/pages/login_page.html")) tmpl := template.Must(template.ParseFiles("src/pages/login_page.html"))
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
logging.Info("Rending login page")
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "hidden"}) tmpl.Execute(w, LoginPageData{IsHiddenClassList: "hidden"})
return return
} }
@@ -111,10 +114,9 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
if strings.Contains(username, "/") { if strings.Contains(username, "/") {
tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""}) tmpl.Execute(w, LoginPageData{IsHiddenClassList: ""})
} }
password := r.FormValue("password") password := r.FormValue("password")
log.Printf("New Login request for %s\n", username) logging.Infof("New Login request for %s\n", username)
userData, err := authenticateUser(username, password) userData, err := authenticateUser(username, password)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@@ -281,10 +283,12 @@ func uploadPhotoHandler(w http.ResponseWriter, r *http.Request) {
} }
func faviconHandler(w http.ResponseWriter, r *http.Request) { func faviconHandler(w http.ResponseWriter, r *http.Request) {
logging.Info("Requesting Favicon")
http.ServeFile(w, r, serverConfig.StyleConfig.FaviconPath) http.ServeFile(w, r, serverConfig.StyleConfig.FaviconPath)
} }
func logoHandler(w http.ResponseWriter, r *http.Request) { func logoHandler(w http.ResponseWriter, r *http.Request) {
logging.Info("Requesting Logo")
http.ServeFile(w, r, serverConfig.StyleConfig.LogoPath) http.ServeFile(w, r, serverConfig.StyleConfig.LogoPath)
} }
@@ -327,21 +331,21 @@ func main() {
defer closeLDAPServer(ldapServer) defer closeLDAPServer(ldapServer)
createWorker(time.Minute*5, cleanupSessions) createWorker(time.Minute*5, cleanupSessions)
http.HandleFunc("/favicon.ico", faviconHandler) HandleFunc("/favicon.ico", faviconHandler)
http.HandleFunc("/logo", logoHandler) HandleFunc("/logo", logoHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/login", loginHandler) HandleFunc("/login", loginHandler)
http.HandleFunc("/profile", profileHandler) HandleFunc("/profile", profileHandler)
http.HandleFunc("/logout", logoutHandler) HandleFunc("/logout", logoutHandler)
http.HandleFunc("/avatar", avatarHandler) HandleFunc("/avatar", avatarHandler)
http.HandleFunc("/change-photo", uploadPhotoHandler) HandleFunc("/change-photo", uploadPhotoHandler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/profile", http.StatusFound) // 302 redirect http.Redirect(w, r, "/profile", http.StatusFound) // 302 redirect
}) })
serverAddress := fmt.Sprintf(":%d", serverConfig.WebserverConfig.Port) serverAddress := fmt.Sprintf(":%d", serverConfig.WebserverConfig.Port)
log.Fatal(http.ListenAndServe(serverAddress, nil)) logging.Fatal(http.ListenAndServe(serverAddress, nil).Error())
} }