conform service design/usage patterns

This commit is contained in:
ari melody 2026-07-31 10:02:34 +01:00
parent 89a5fdc4e5
commit fd3b2a0dba
Signed by: ari
GPG key ID: CF99829C92678188
13 changed files with 65 additions and 60 deletions

View file

@ -115,7 +115,7 @@ func changePasswordHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ACCOUNT, "\"%s\" changed password by user request. (%s)", session.Account.Username, controller.ResolveIP(app, r))
app.LogService.Info(model.LOG_ACCOUNT, "\"%s\" changed password by user request. (%s)", session.Account.Username, controller.ResolveIP(app, r))
controller.SetSessionError(app.DB, session, "")
controller.SetSessionMessage(app.DB, session, "Password updated successfully.")
@ -145,7 +145,7 @@ func deleteAccountHandler(app *app.AppState) http.Handler {
// check password
if err := bcrypt.CompareHashAndPassword([]byte(session.Account.Password), []byte(r.Form.Get("password"))); err != nil {
app.Log.Warn(model.LOG_ACCOUNT, "Account \"%s\" attempted account deletion with incorrect password. (%s)", session.Account.Username, controller.ResolveIP(app, r))
app.LogService.Warn(model.LOG_ACCOUNT, "Account \"%s\" attempted account deletion with incorrect password. (%s)", session.Account.Username, controller.ResolveIP(app, r))
controller.SetSessionError(app.DB, session, "Incorrect password.")
http.Redirect(w, r, "/admin/account", http.StatusFound)
return
@ -159,7 +159,7 @@ func deleteAccountHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ACCOUNT, "Account \"%s\" deleted by user request. (%s)", session.Account.Username, controller.ResolveIP(app, r))
app.LogService.Info(model.LOG_ACCOUNT, "Account \"%s\" deleted by user request. (%s)", session.Account.Username, controller.ResolveIP(app, r))
controller.SetSessionAccount(app.DB, session, nil)
controller.SetSessionError(app.DB, session, "")
@ -310,7 +310,7 @@ func totpConfirmHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ACCOUNT, "\"%s\" created TOTP method \"%s\".", session.Account.Username, totp.Name)
app.LogService.Info(model.LOG_ACCOUNT, "\"%s\" created TOTP method \"%s\".", session.Account.Username, totp.Name)
controller.SetSessionError(app.DB, session, "")
controller.SetSessionMessage(app.DB, session, fmt.Sprintf("TOTP method \"%s\" created successfully.", totp.Name))
@ -358,7 +358,7 @@ func totpDeleteHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ACCOUNT, "\"%s\" deleted TOTP method \"%s\".", session.Account.Username, totp.Name)
app.LogService.Info(model.LOG_ACCOUNT, "\"%s\" deleted TOTP method \"%s\".", session.Account.Username, totp.Name)
controller.SetSessionError(app.DB, session, "")
controller.SetSessionMessage(app.DB, session, fmt.Sprintf("TOTP method \"%s\" deleted successfully.", totp.Name))

View file

@ -241,7 +241,7 @@ func registerAccountHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(
app.LogService.Info(
model.LOG_ACCOUNT,
"Account \"%s\" (%s) created using invite \"%s\". (%s)",
credentials.Username,
@ -252,7 +252,7 @@ func registerAccountHandler(app *app.AppState) http.Handler {
err = controller.DeleteInvite(app.DB, invite.Code)
if err != nil {
app.Log.Warn(model.LOG_ACCOUNT, "Failed to delete expired invite \"%s\": %v", invite.Code, err)
app.LogService.Warn(model.LOG_ACCOUNT, "Failed to delete expired invite \"%s\": %v", invite.Code, err)
}
// registration success!
@ -332,7 +332,7 @@ func loginHandler(app *app.AppState) http.Handler {
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(password))
if err != nil {
app.Log.Warn(model.LOG_ACCOUNT, "\"%s\" attempted login with incorrect password. (%s)", account.Username, controller.ResolveIP(app, r))
app.LogService.Warn(model.LOG_ACCOUNT, "\"%s\" attempted login with incorrect password. (%s)", account.Username, controller.ResolveIP(app, r))
if locked := handleFailedLogin(app, account, r); locked {
controller.SetSessionError(app.DB, session, "Too many failed attempts. This account is now locked.")
} else {
@ -366,8 +366,8 @@ func loginHandler(app *app.AppState) http.Handler {
// login success!
// TODO: log login activity to user
app.Log.Info(model.LOG_ACCOUNT, "\"%s\" logged in. (%s)", account.Username, controller.ResolveIP(app, r))
app.Log.Warn(model.LOG_ACCOUNT, "\"%s\" does not have any TOTP methods assigned.", account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "\"%s\" logged in. (%s)", account.Username, controller.ResolveIP(app, r))
app.LogService.Warn(model.LOG_ACCOUNT, "\"%s\" does not have any TOTP methods assigned.", account.Username)
err = controller.SetSessionAccount(app.DB, session, account)
if err != nil {
@ -420,7 +420,7 @@ func loginTOTPHandler(app *app.AppState) http.Handler {
totpCode := r.FormValue("totp")
if len(totpCode) != controller.TOTP_CODE_LENGTH {
app.Log.Warn(model.LOG_ACCOUNT, "\"%s\" failed login (Invalid TOTP). (%s)", session.AttemptAccount.Username, controller.ResolveIP(app, r))
app.LogService.Warn(model.LOG_ACCOUNT, "\"%s\" failed login (Invalid TOTP). (%s)", session.AttemptAccount.Username, controller.ResolveIP(app, r))
controller.SetSessionError(app.DB, session, "Invalid TOTP.")
render()
return
@ -434,7 +434,7 @@ func loginTOTPHandler(app *app.AppState) http.Handler {
return
}
if totpMethod == nil {
app.Log.Warn(model.LOG_ACCOUNT, "\"%s\" failed login (Incorrect TOTP). (%s)", session.AttemptAccount.Username, controller.ResolveIP(app, r))
app.LogService.Warn(model.LOG_ACCOUNT, "\"%s\" failed login (Incorrect TOTP). (%s)", session.AttemptAccount.Username, controller.ResolveIP(app, r))
if locked := handleFailedLogin(app, session.AttemptAccount, r); locked {
controller.SetSessionError(app.DB, session, "Too many failed attempts. This account is now locked.")
controller.SetSessionAttemptAccount(app.DB, session, nil)
@ -446,7 +446,7 @@ func loginTOTPHandler(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ACCOUNT, "\"%s\" logged in with TOTP method \"%s\". (%s)", session.AttemptAccount.Username, totpMethod.Name, controller.ResolveIP(app, r))
app.LogService.Info(model.LOG_ACCOUNT, "\"%s\" logged in with TOTP method \"%s\". (%s)", session.AttemptAccount.Username, totpMethod.Name, controller.ResolveIP(app, r))
err = controller.SetSessionAccount(app.DB, session, session.AttemptAccount)
if err != nil {
@ -574,7 +574,7 @@ func handleFailedLogin(app *app.AppState, account *model.Account, r *http.Reques
account.Username,
err,
)
app.Log.Warn(
app.LogService.Warn(
model.LOG_ACCOUNT,
"Failed to lock account \"%s\"",
account.Username,
@ -588,7 +588,7 @@ func handleFailedLogin(app *app.AppState, account *model.Account, r *http.Reques
model.MAX_LOGIN_FAIL_ATTEMPTS,
controller.ResolveIP(app, r),
)
app.Log.Warn(
app.LogService.Warn(
model.LOG_ACCOUNT,
"Account \"%s\" was locked: %d failed login attempts (IP: %s)",
account.Username,
@ -606,7 +606,7 @@ func handleFailedLogin(app *app.AppState, account *model.Account, r *http.Reques
account.Username,
err,
)
app.Log.Warn(
app.LogService.Warn(
model.LOG_ACCOUNT,
"Failed to increment login failures for \"%s\"",
account.Username,

View file

@ -43,7 +43,7 @@ func logsHandler(app *app.AppState) http.Handler {
}
}
logs, err := app.Log.Search(levelFilter, typeFilter, query, 100, 0)
logs, err := app.LogService.Search(levelFilter, typeFilter, query, 100, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch audit logs: %v\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)

View file

@ -115,7 +115,7 @@ func CreateArtist(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" created by \"%s\".", artist.Name, session.Account.Username)
app.LogService.Info(model.LOG_ARTIST, "Artist \"%s\" created by \"%s\".", artist.Name, session.Account.Username)
w.WriteHeader(http.StatusCreated)
})
@ -166,7 +166,7 @@ func UpdateArtist(app *app.AppState, artist *model.Artist) http.Handler {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" updated by \"%s\".", artist.Name, session.Account.Username)
app.LogService.Info(model.LOG_ARTIST, "Artist \"%s\" updated by \"%s\".", artist.Name, session.Account.Username)
})
}
@ -184,6 +184,6 @@ func DeleteArtist(app *app.AppState, artist *model.Artist) http.Handler {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_ARTIST, "Artist \"%s\" deleted by \"%s\".", artist.Name, session.Account.Username)
app.LogService.Info(model.LOG_ARTIST, "Artist \"%s\" deleted by \"%s\".", artist.Name, session.Account.Username)
})
}

View file

@ -224,7 +224,7 @@ func CreateRelease(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" created by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Release \"%s\" created by \"%s\".", release.ID, session.Account.Username)
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
@ -307,7 +307,7 @@ func UpdateRelease(app *app.AppState, release *model.Release) http.Handler {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
})
}
@ -336,7 +336,7 @@ func UpdateReleaseTracks(app *app.AppState, release *model.Release) http.Handler
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Tracklist for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Tracklist for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
})
}
@ -381,7 +381,7 @@ func UpdateReleaseCredits(app *app.AppState, release *model.Release) http.Handle
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Credits for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Credits for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
})
}
@ -410,7 +410,7 @@ func UpdateReleaseLinks(app *app.AppState, release *model.Release) http.Handler
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Links for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Links for release \"%s\" updated by \"%s\".", release.ID, session.Account.Username)
})
}
@ -428,6 +428,6 @@ func DeleteRelease(app *app.AppState, release *model.Release) http.Handler {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Release \"%s\" deleted by \"%s\".", release.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Release \"%s\" deleted by \"%s\".", release.ID, session.Account.Username)
})
}

View file

@ -97,7 +97,7 @@ func CreateTrack(app *app.AppState) http.Handler {
return
}
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) created by \"%s\".", track.Title, track.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Track \"%s\" (%s) created by \"%s\".", track.Title, track.ID, session.Account.Username)
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusCreated)
@ -132,7 +132,7 @@ func UpdateTrack(app *app.AppState, track *model.Track) http.Handler {
return
}
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) updated by \"%s\".", track.Title, track.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Track \"%s\" (%s) updated by \"%s\".", track.Title, track.ID, session.Account.Username)
w.Header().Add("Content-Type", "application/json")
encoder := json.NewEncoder(w)
@ -160,6 +160,6 @@ func DeleteTrack(app *app.AppState, track *model.Track) http.Handler {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
app.Log.Info(model.LOG_MUSIC, "Track \"%s\" (%s) deleted by \"%s\".", track.Title, track.ID, session.Account.Username)
app.LogService.Info(model.LOG_MUSIC, "Track \"%s\" (%s) deleted by \"%s\".", track.Title, track.ID, session.Account.Username)
})
}

View file

@ -50,7 +50,7 @@ func HandleImageUpload(app *app.AppState, data *string, directory string, filena
return "", nil
}
app.Log.Info(model.LOG_FILES, "\"%s\" created.", imagePath)
app.LogService.Info(model.LOG_FILES, "\"%s\" created.", imagePath)
return filename, nil
}

View file

@ -38,10 +38,10 @@ func GetSessionFromRequest(app *app.AppState, r *http.Request) (*model.Session,
account, _ := app.AccountService.GetByID(session.Account.ID)
msg += " (Account \"" + account.Username + "\")"
}
app.Log.Warn(model.LOG_ACCOUNT, msg)
app.LogService.Warn(model.LOG_ACCOUNT, msg)
err = DeleteSession(app.DB, session.Token)
if err != nil {
app.Log.Warn(model.LOG_ACCOUNT, "Failed to delete affected session")
app.LogService.Warn(model.LOG_ACCOUNT, "Failed to delete affected session")
}
return nil, nil
}

22
main.go
View file

@ -92,7 +92,7 @@ func main() {
app.DB = psqlDB
logRepo := logRepo.NewLogRepositoryPostgres(psqlDB)
app.Log = logService.NewLogService(
app.LogService = logService.NewLogService(
logRepo,
log.New(os.Stderr, "logger", model.DEFAULT_LOG_FLAGS),
)
@ -139,7 +139,7 @@ func main() {
logger.Fatalf("FATAL: Failed to create TOTP method: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "TOTP method \"%s\" for \"%s\" created via config utility.", totp.Name, account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "TOTP method \"%s\" for \"%s\" created via config utility.", totp.Name, account.Username)
url := controller.GenerateTOTPURI(account.Username, totp.Secret)
logger.Printf("%s\n", url)
return
@ -165,7 +165,7 @@ func main() {
logger.Fatalf("FATAL: Failed to create TOTP method: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "TOTP method \"%s\" for \"%s\" deleted via config utility.", totpName, account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "TOTP method \"%s\" for \"%s\" deleted via config utility.", totpName, account.Username)
logger.Printf("TOTP method \"%s\" deleted.\n", totpName)
return
@ -231,7 +231,7 @@ func main() {
if err != nil {
logger.Fatalf("FATAL: Failed to clean up TOTP methods: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "TOTP methods pruned via config utility.")
app.LogService.Info(model.LOG_ACCOUNT, "TOTP methods pruned via config utility.")
logger.Printf("Cleaned up dangling TOTP methods successfully.\n")
return
@ -242,7 +242,7 @@ func main() {
logger.Fatalf("FATAL: Failed to create invite code: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Invite generted via config utility (%s).", invite.Code)
app.LogService.Info(model.LOG_ACCOUNT, "Invite generted via config utility (%s).", invite.Code)
logger.Printf(
"Here you go! This code expires in %d hours: %s\n",
int(math.Ceil(invite.ExpiresAt.Sub(invite.CreatedAt).Hours())),
@ -257,7 +257,7 @@ func main() {
logger.Fatalf("FATAL: Failed to delete invites: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Invites purged via config utility.")
app.LogService.Info(model.LOG_ACCOUNT, "Invites purged via config utility.")
logger.Printf("Invites deleted successfully.\n")
return
@ -310,7 +310,7 @@ func main() {
logger.Fatalf("FATAL: Failed to update password: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Password for '%s' updated via config utility.", account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "Password for '%s' updated via config utility.", account.Username)
logger.Printf("Password for \"%s\" updated successfully.\n", account.Username)
return
@ -342,7 +342,7 @@ func main() {
logger.Fatalf("FATAL: Failed to delete account: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Account '%s' deleted via config utility.", account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "Account '%s' deleted via config utility.", account.Username)
logger.Printf("Account \"%s\" deleted successfully.\n", account.Username)
return
@ -367,7 +367,7 @@ func main() {
logger.Fatalf("FATAL: Failed to lock account: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Account '%s' locked via config utility.", account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "Account '%s' locked via config utility.", account.Username)
logger.Printf("Account \"%s\" locked successfully.\n", account.Username)
return
@ -392,13 +392,13 @@ func main() {
logger.Fatalf("FATAL: Failed to unlock account: %v\n", err)
}
app.Log.Info(model.LOG_ACCOUNT, "Account '%s' unlocked via config utility.", account.Username)
app.LogService.Info(model.LOG_ACCOUNT, "Account '%s' unlocked via config utility.", account.Username)
logger.Printf("Account \"%s\" unlocked successfully.\n", account.Username)
return
case "logs":
// TODO: add log search parameters
logs, err := app.Log.Search([]model.LogLevel{}, []string{}, "", 100, 0)
logs, err := app.LogService.Search([]model.LogLevel{}, []string{}, "", 100, 0)
if err != nil {
logger.Fatalf("FATAL: Failed to fetch logs: %v\n", err)
}

View file

@ -6,8 +6,14 @@ import (
"github.com/jmoiron/sqlx"
"arimelody-web/model/twitch"
logService "arimelody-web/service/log"
inviteService "arimelody-web/service/invite"
accountService "arimelody-web/service/account"
"arimelody-web/service/log"
sessionService "arimelody-web/service/session"
artistService "arimelody-web/service/artist"
releaseService "arimelody-web/service/release"
trackService "arimelody-web/service/track"
)
type (
@ -45,10 +51,15 @@ type (
AppState struct {
DB *sqlx.DB
Config Config
Log *log.LogService
Twitch *twitch.State
PublicFS embed.FS
LogService *logService.LogService
InviteService *inviteService.InviteService
AccountService *accountService.AccountService
SesisonService *sessionService.SessionService
ArtistService *artistService.ArtistService
ReleaseService *releaseService.ReleaseService
TrackService *trackService.TrackService
}
)

View file

@ -7,11 +7,9 @@ import (
"strconv"
)
type (
AccountRepositoryMemory struct {
type AccountRepositoryMemory struct {
accounts []*model.Account
}
)
var _ AccountRepository = new(AccountRepositoryMemory)

View file

@ -8,11 +8,9 @@ import (
_ "github.com/lib/pq"
)
type (
AccountRepositoryPostgres struct {
type AccountRepositoryPostgres struct {
db *sqlx.DB
}
)
var _ AccountRepository = new(AccountRepositoryPostgres)

View file

@ -8,11 +8,9 @@ import (
_ "github.com/lib/pq"
)
type (
LogRepositoryPostgres struct {
type LogRepositoryPostgres struct {
db *sqlx.DB
}
)
var _ LogRepository = new(LogRepositoryPostgres)