feat: updated docs and added description to files
Some checks failed
Tests / Run Go Tests (push) Failing after 1m35s
Arbeitszeitmessung Deploy / Build Webserver (push) Successful in 2m48s

This commit is contained in:
2026-01-29 18:28:28 +01:00
parent 41c34c42cf
commit ba034f1c33
52 changed files with 458 additions and 3413 deletions

View File

@@ -1,5 +1,15 @@
// endpoints contains all http endpoints
// for more complex endpoints the *Handler function is executed first
// by the main programm and it will then run other functions as needed
//
// the filenames represent the route/url for the given endpoint
// when "-" is a "/" so this file is server at "/auto/feiertage"
package endpoints
// this endpoint will be called by crontab and generates the public holidays for a given year
// after that manually added holidays with a "wiederholen" flag are copied over to the new year
import (
"arbeitszeitmessung/helper/paramParser"
"arbeitszeitmessung/models"

View File

@@ -1,5 +1,10 @@
package endpoints
// this served as "/auto/kurzarbeit" will add a booking to every kurzarbeitstag
// to make them reach the full lenght of workday.
//
// right now this is not in use because the time is calculated
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/helper/paramParser"
@@ -58,8 +63,8 @@ func fillKurzarbeit(r *http.Request, w http.ResponseWriter) {
workday, _ := day.(*models.WorkDay)
lastBookingTime := workday.Bookings[len(workday.Bookings)-1].Timestamp
kurzarbeitBegin := (*models.Booking).New(nil, user.CardUID, 0, 1, bookingTypeKurzarbeit.Id)
kurzarbeitEnd := (*models.Booking).New(nil, user.CardUID, 0, 2, bookingTypeKurzarbeit.Id)
kurzarbeitBegin := (*models.Booking).NewBooking(nil, user.CardUID, 0, 1, bookingTypeKurzarbeit.Id)
kurzarbeitEnd := (*models.Booking).NewBooking(nil, user.CardUID, 0, 2, bookingTypeKurzarbeit.Id)
kurzarbeitBegin.Timestamp = lastBookingTime.Add(time.Minute)
kurzarbeitEnd.Timestamp = lastBookingTime.Add(worktimeKurzarbeit)

View File

@@ -1,5 +1,8 @@
package endpoints
// this endpoint served at "/auto/logout" will be executed by crontab
// and will log out all users that are currently still logged in
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"

View File

@@ -1,5 +1,8 @@
package endpoints
// this endpoint served at "/pdf/create" accepts the contents from the pdf form
// and renders a pdf according to this form
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/helper/paramParser"

View File

@@ -1,5 +1,7 @@
package endpoints
// this endpoint served at "/pdf" handles the rendering of the pdf form
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"

View File

@@ -1,5 +1,7 @@
package endpoints
// this endpoint served at "/team/presence" shows the presence page
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
@@ -8,7 +10,7 @@ import (
"net/http"
)
func TeamPresenceHandler(w http.ResponseWriter, r *http.Request) {
func PresenceHandler(w http.ResponseWriter, r *http.Request) {
helper.RequiresLogin(Session, w, r)
helper.SetCors(w)
switch r.Method {

View File

@@ -1,5 +1,8 @@
package endpoints
// this endpoint served at "/team/report" handles the report page
// and also the submission/change of reports
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/helper/paramParser"
@@ -12,7 +15,7 @@ import (
"time"
)
func TeamHandler(w http.ResponseWriter, r *http.Request) {
func ReportHandler(w http.ResponseWriter, r *http.Request) {
helper.RequiresLogin(Session, w, r)
switch r.Method {
case http.MethodPost:
@@ -76,5 +79,5 @@ func showWeeks(w http.ResponseWriter, r *http.Request) {
workWeeks = append(workWeeks, weeks...)
}
// isRunningWeek := time.Since(lastSub) < 24*5*time.Hour //the last submission is this week and cannot be send yet
templates.TeamPage(workWeeks, userWeek).Render(r.Context(), w)
templates.ReportPage(workWeeks, userWeek).Render(r.Context(), w)
}

View File

@@ -1,5 +1,8 @@
package endpoints
// this endpoint served at "/time/create" is for the esp api and creates bookings
// either via HTTP GET or HTTP PUT
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"

View File

@@ -1,5 +1,9 @@
package endpoints
// this endpoint served at "/time" handles the time page
// this includes normal show + creation of bookings from the webpage +
// edit functionality
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/helper/paramParser"
@@ -158,7 +162,7 @@ func updateBooking(w http.ResponseWriter, r *http.Request) {
return
}
newBooking := (*models.Booking).New(nil, user.CardUID, 0, int16(check_in_out), 1)
newBooking := (*models.Booking).NewBooking(nil, user.CardUID, 0, int16(check_in_out), 1)
newBooking.Timestamp = timestamp
if newBooking.Verify() {
err = newBooking.InsertWithTimestamp()

View File

@@ -0,0 +1,17 @@
package endpoints
// this endpoint server at "/user/login" will show the login page or
// directly login the user based on the http method used
import "net/http"
func LoginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
showLoginPage(w, r, true, "")
case http.MethodPost:
loginUser(w, r)
default:
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
}
}

View File

@@ -1,74 +0,0 @@
package endpoints
import (
"arbeitszeitmessung/models"
"arbeitszeitmessung/templates"
"context"
"log"
"net/http"
"strconv"
"time"
"github.com/alexedwards/scs/v2"
)
var Session *scs.SessionManager
func CreateSessionManager(lifetime time.Duration) *scs.SessionManager {
Session = scs.New()
Session.Lifetime = lifetime
return Session
}
func showLoginPage(w http.ResponseWriter, r *http.Request, success bool, errorMsg string) {
r = r.WithContext(context.WithValue(r.Context(), "session", Session))
if Session.Exists(r.Context(), "user") {
http.Redirect(w, r, "/time", http.StatusSeeOther)
}
templates.LoginPage(success, errorMsg).Render(r.Context(), w)
}
func loginUser(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println("Error parsing form!", err)
showLoginPage(w, r, false, "Internal error!")
return
}
_personal_nummer := r.FormValue("personal_nummer")
if _personal_nummer == "" {
log.Println("No personal_nummer provided!")
showLoginPage(w, r, false, "Keine Personalnummer gesetzt.")
return
}
personal_nummer, err := strconv.Atoi(_personal_nummer)
if err != nil {
log.Println("Cannot parse personal nubmer!")
showLoginPage(w, r, false, "Personalnummer ist nicht valide gesetzt.")
return
}
user, err := models.GetUserByPersonalNr(personal_nummer)
if err != nil {
log.Println("No user found under this personal number!", err)
showLoginPage(w, r, false, "Nutzer unter dieser Personalnummer nicht gefunden.")
return
}
password := r.FormValue("password")
if user.Login(password) {
log.Printf("New succesfull user login from %s %s (%d)!\n", user.Vorname, user.Name, user.PersonalNummer)
Session.Put(r.Context(), "user", user.PersonalNummer)
Session.Commit(r.Context())
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
}
showLoginPage(w, r, false, "")
}
func logoutUser(w http.ResponseWriter, r *http.Request) {
log.Println("Loggin out user!")
err := Session.Destroy(r.Context())
if err != nil {
log.Println("Error destroying session!", err)
}
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
}

View File

@@ -1,6 +1,11 @@
package endpoints
// this endpoint server at "/user/settings" will show the settings page
// depeding on which action is taken the user will be logged out or
// the password will be changed
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
"arbeitszeitmessung/templates"
"context"
@@ -8,6 +13,23 @@ import (
"net/http"
)
func UserSettingsHandler(w http.ResponseWriter, r *http.Request) {
helper.RequiresLogin(Session, w, r)
switch r.Method {
case http.MethodGet:
showUserPage(w, r, 0)
case http.MethodPost:
switch r.FormValue("action") {
case "change-pass":
changePassword(w, r)
case "logout-user":
logoutUser(w, r)
}
default:
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
}
}
// change user password and store salted hash in db
func changePassword(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()

View File

@@ -1,8 +1,18 @@
package endpoints
// this is not directly an endpoint as it servers all requests for "/user"
// and routes the furter to "login", "logout", and "settings"
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
"arbeitszeitmessung/templates"
"context"
"log"
"net/http"
"strconv"
"time"
"github.com/alexedwards/scs/v2"
)
func UserHandler(w http.ResponseWriter, r *http.Request) {
@@ -16,31 +26,63 @@ func UserHandler(w http.ResponseWriter, r *http.Request) {
}
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
showLoginPage(w, r, true, "")
case http.MethodPost:
loginUser(w, r)
default:
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
}
var Session *scs.SessionManager
func CreateSessionManager(lifetime time.Duration) *scs.SessionManager {
Session = scs.New()
Session.Lifetime = lifetime
return Session
}
func UserSettingsHandler(w http.ResponseWriter, r *http.Request) {
helper.RequiresLogin(Session, w, r)
switch r.Method {
case http.MethodGet:
showUserPage(w, r, 0)
case http.MethodPost:
switch r.FormValue("action") {
case "change-pass":
changePassword(w, r)
case "logout-user":
logoutUser(w, r)
}
default:
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
func showLoginPage(w http.ResponseWriter, r *http.Request, success bool, errorMsg string) {
r = r.WithContext(context.WithValue(r.Context(), "session", Session))
if Session.Exists(r.Context(), "user") {
http.Redirect(w, r, "/time", http.StatusSeeOther)
}
templates.LoginPage(success, errorMsg).Render(r.Context(), w)
}
func loginUser(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Println("Error parsing form!", err)
showLoginPage(w, r, false, "Internal error!")
return
}
_personal_nummer := r.FormValue("personal_nummer")
if _personal_nummer == "" {
log.Println("No personal_nummer provided!")
showLoginPage(w, r, false, "Keine Personalnummer gesetzt.")
return
}
personal_nummer, err := strconv.Atoi(_personal_nummer)
if err != nil {
log.Println("Cannot parse personal nubmer!")
showLoginPage(w, r, false, "Personalnummer ist nicht valide gesetzt.")
return
}
user, err := models.GetUserByPersonalNr(personal_nummer)
if err != nil {
log.Println("No user found under this personal number!", err)
showLoginPage(w, r, false, "Nutzer unter dieser Personalnummer nicht gefunden.")
return
}
password := r.FormValue("password")
if user.Login(password) {
log.Printf("New succesfull user login from %s %s (%d)!\n", user.Vorname, user.Name, user.PersonalNummer)
Session.Put(r.Context(), "user", user.PersonalNummer)
Session.Commit(r.Context())
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
}
showLoginPage(w, r, false, "")
}
func logoutUser(w http.ResponseWriter, r *http.Request) {
log.Println("Loggin out user!")
err := Session.Destroy(r.Context())
if err != nil {
log.Println("Error destroying session!", err)
}
http.Redirect(w, r, "/user/login", http.StatusSeeOther)
}