fix: remove redundante names
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 37s
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 37s
This commit is contained in:
34
db/auth.go
34
db/auth.go
@@ -78,7 +78,7 @@ func NewToken(userId uuid.UUID, token string, tokenType string, createdAt time.T
|
||||
}
|
||||
}
|
||||
|
||||
type AuthDb interface {
|
||||
type Auth interface {
|
||||
InsertUser(user *User) error
|
||||
UpdateUser(user *User) error
|
||||
GetUserByEmail(email string) (*User, error)
|
||||
@@ -96,15 +96,15 @@ type AuthDb interface {
|
||||
DeleteOldSessions(userId uuid.UUID) error
|
||||
}
|
||||
|
||||
type AuthDbSqlite struct {
|
||||
type AuthSqlite struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewAuthDbSqlite(db *sql.DB) *AuthDbSqlite {
|
||||
return &AuthDbSqlite{db: db}
|
||||
func NewAuthSqlite(db *sql.DB) *AuthSqlite {
|
||||
return &AuthSqlite{db: db}
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) InsertUser(user *User) error {
|
||||
func (db AuthSqlite) InsertUser(user *User) error {
|
||||
_, err := db.db.Exec(`
|
||||
INSERT INTO user (user_uuid, email, email_verified, email_verified_at, is_admin, password, salt, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
@@ -122,7 +122,7 @@ func (db AuthDbSqlite) InsertUser(user *User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) UpdateUser(user *User) error {
|
||||
func (db AuthSqlite) UpdateUser(user *User) error {
|
||||
_, err := db.db.Exec(`
|
||||
UPDATE user
|
||||
SET email_verified = ?, email_verified_at = ?, password = ?
|
||||
@@ -137,7 +137,7 @@ func (db AuthDbSqlite) UpdateUser(user *User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) GetUserByEmail(email string) (*User, error) {
|
||||
func (db AuthSqlite) GetUserByEmail(email string) (*User, error) {
|
||||
var (
|
||||
userId uuid.UUID
|
||||
emailVerified bool
|
||||
@@ -164,7 +164,7 @@ func (db AuthDbSqlite) GetUserByEmail(email string) (*User, error) {
|
||||
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
||||
func (db AuthSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
||||
var (
|
||||
email string
|
||||
emailVerified bool
|
||||
@@ -191,7 +191,7 @@ func (db AuthDbSqlite) GetUser(userId uuid.UUID) (*User, error) {
|
||||
return NewUser(userId, email, emailVerified, emailVerifiedAt, isAdmin, password, salt, createdAt), nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) DeleteUser(userId uuid.UUID) error {
|
||||
func (db AuthSqlite) DeleteUser(userId uuid.UUID) error {
|
||||
|
||||
tx, err := db.db.Begin()
|
||||
if err != nil {
|
||||
@@ -236,7 +236,7 @@ func (db AuthDbSqlite) DeleteUser(userId uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) InsertToken(token *Token) error {
|
||||
func (db AuthSqlite) InsertToken(token *Token) error {
|
||||
_, err := db.db.Exec(`
|
||||
INSERT INTO user_token (user_uuid, type, token, created_at, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?)`, token.UserId, token.Type, token.Token, token.CreatedAt, token.ExpiresAt)
|
||||
@@ -249,7 +249,7 @@ func (db AuthDbSqlite) InsertToken(token *Token) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) GetToken(token string) (*Token, error) {
|
||||
func (db AuthSqlite) GetToken(token string) (*Token, error) {
|
||||
var (
|
||||
userId uuid.UUID
|
||||
tokenType string
|
||||
@@ -290,7 +290,7 @@ func (db AuthDbSqlite) GetToken(token string) (*Token, error) {
|
||||
return NewToken(userId, token, tokenType, createdAt, expiresAt), nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType string) ([]*Token, error) {
|
||||
func (db AuthSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType string) ([]*Token, error) {
|
||||
|
||||
query, err := db.db.Query(`
|
||||
SELECT token, created_at, expires_at
|
||||
@@ -338,7 +338,7 @@ func (db AuthDbSqlite) GetTokensByUserIdAndType(userId uuid.UUID, tokenType stri
|
||||
return tokens, nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) DeleteToken(token string) error {
|
||||
func (db AuthSqlite) DeleteToken(token string) error {
|
||||
_, err := db.db.Exec("DELETE FROM user_token WHERE token = ?", token)
|
||||
if err != nil {
|
||||
log.Error("Could not delete token: %v", err)
|
||||
@@ -347,7 +347,7 @@ func (db AuthDbSqlite) DeleteToken(token string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) InsertSession(session *Session) error {
|
||||
func (db AuthSqlite) InsertSession(session *Session) error {
|
||||
|
||||
_, err := db.db.Exec(`
|
||||
INSERT INTO session (session_id, user_uuid, created_at)
|
||||
@@ -361,7 +361,7 @@ func (db AuthDbSqlite) InsertSession(session *Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) GetSession(sessionId string) (*Session, error) {
|
||||
func (db AuthSqlite) GetSession(sessionId string) (*Session, error) {
|
||||
|
||||
var (
|
||||
userId uuid.UUID
|
||||
@@ -381,7 +381,7 @@ func (db AuthDbSqlite) GetSession(sessionId string) (*Session, error) {
|
||||
return NewSession(sessionId, userId, sessionCreatedAt), nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
||||
func (db AuthSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
||||
// Delete old inactive sessions
|
||||
_, err := db.db.Exec("DELETE FROM session WHERE created_at < datetime('now','-8 hours') AND user_uuid = ?", userId)
|
||||
if err != nil {
|
||||
@@ -391,7 +391,7 @@ func (db AuthDbSqlite) DeleteOldSessions(userId uuid.UUID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db AuthDbSqlite) DeleteSession(sessionId string) error {
|
||||
func (db AuthSqlite) DeleteSession(sessionId string) error {
|
||||
if sessionId != "" {
|
||||
|
||||
_, err := db.db.Exec("DELETE FROM session WHERE session_id = ?", sessionId)
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := setupDb(t)
|
||||
|
||||
underTest := AuthDbSqlite{db: db}
|
||||
underTest := AuthSqlite{db: db}
|
||||
|
||||
_, err := underTest.GetUserByEmail("someNonExistentEmail")
|
||||
assert.Equal(t, ErrNotFound, err)
|
||||
@@ -43,7 +43,7 @@ func TestUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := setupDb(t)
|
||||
|
||||
underTest := AuthDbSqlite{db: db}
|
||||
underTest := AuthSqlite{db: db}
|
||||
|
||||
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
||||
@@ -62,7 +62,7 @@ func TestUser(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := setupDb(t)
|
||||
|
||||
underTest := AuthDbSqlite{db: db}
|
||||
underTest := AuthSqlite{db: db}
|
||||
|
||||
verifiedAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||
createAt := time.Date(2020, 1, 5, 12, 0, 0, 0, time.UTC)
|
||||
@@ -83,7 +83,7 @@ func TestEmailVerification(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := setupDb(t)
|
||||
|
||||
underTest := AuthDbSqlite{db: db}
|
||||
underTest := AuthSqlite{db: db}
|
||||
|
||||
token, err := underTest.GetToken("someNonExistentToken")
|
||||
|
||||
@@ -94,7 +94,7 @@ func TestEmailVerification(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := setupDb(t)
|
||||
|
||||
underTest := AuthDbSqlite{db: db}
|
||||
underTest := AuthSqlite{db: db}
|
||||
tokenStr := "some secure token"
|
||||
createdAt := time.Date(2020, 1, 5, 13, 0, 0, 0, time.UTC)
|
||||
|
||||
|
||||
@@ -13,23 +13,23 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type HandlerAuth interface {
|
||||
type Auth interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type HandlerAuthImpl struct {
|
||||
service service.AuthService
|
||||
type AuthImpl struct {
|
||||
service service.Auth
|
||||
render *Render
|
||||
}
|
||||
|
||||
func NewHandlerAuth(service service.AuthService, render *Render) HandlerAuth {
|
||||
return HandlerAuthImpl{
|
||||
func NewAuth(service service.Auth, render *Render) Auth {
|
||||
return AuthImpl{
|
||||
service: service,
|
||||
render: render,
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) Handle(router *http.ServeMux) {
|
||||
func (handler AuthImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/auth/signin", handler.handleSignInPage())
|
||||
router.Handle("/api/auth/signin", handler.handleSignIn())
|
||||
|
||||
@@ -56,7 +56,7 @@ var (
|
||||
securityWaitDuration = 250 * time.Millisecond
|
||||
)
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignInPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignInPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if user != nil {
|
||||
@@ -74,7 +74,7 @@ func (handler HandlerAuthImpl) handleSignInPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignIn() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := utils.WaitMinimumTime(securityWaitDuration, func() (*service.User, error) {
|
||||
var email = r.FormValue("email")
|
||||
@@ -119,7 +119,7 @@ func (handler HandlerAuthImpl) handleSignIn() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignUpPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignUpPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
|
||||
@@ -137,7 +137,7 @@ func (handler HandlerAuthImpl) handleSignUpPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if user == nil {
|
||||
@@ -155,7 +155,7 @@ func (handler HandlerAuthImpl) handleSignUpVerifyPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -172,7 +172,7 @@ func (handler HandlerAuthImpl) handleVerifyResendComp() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
token := r.URL.Query().Get("token")
|
||||
@@ -187,7 +187,7 @@ func (handler HandlerAuthImpl) handleSignUpVerifyResponsePage() http.HandlerFunc
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignUp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var email = r.FormValue("email")
|
||||
var password = r.FormValue("password")
|
||||
@@ -217,7 +217,7 @@ func (handler HandlerAuthImpl) handleSignUp() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleSignOut() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleSignOut() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
err := handler.service.SignOut(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -241,7 +241,7 @@ func (handler HandlerAuthImpl) handleSignOut() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// An unverified email should be able to delete their account
|
||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
@@ -254,7 +254,7 @@ func (handler HandlerAuthImpl) handleDeleteAccountPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -280,7 +280,7 @@ func (handler HandlerAuthImpl) handleDeleteAccountComp() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
isPasswordReset := r.URL.Query().Has("token")
|
||||
@@ -297,7 +297,7 @@ func (handler HandlerAuthImpl) handleChangePasswordPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
@@ -319,7 +319,7 @@ func (handler HandlerAuthImpl) handleChangePasswordComp() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user, err := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
@@ -333,7 +333,7 @@ func (handler HandlerAuthImpl) handleResetPasswordPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
email := r.FormValue("email")
|
||||
@@ -351,7 +351,7 @@ func (handler HandlerAuthImpl) handleForgotPasswordComp() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler HandlerAuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||
func (handler AuthImpl) handleForgotPasswordResponseComp() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
pageUrl, err := url.Parse(r.Header.Get("HX-Current-URL"))
|
||||
|
||||
@@ -10,27 +10,27 @@ import (
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
type IndexHandler interface {
|
||||
type Index interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type IndexHandlerImpl struct {
|
||||
service service.AuthService
|
||||
type IndexImpl struct {
|
||||
service service.Auth
|
||||
render *Render
|
||||
}
|
||||
|
||||
func NewIndexHandler(service service.AuthService, render *Render) IndexHandler {
|
||||
return IndexHandlerImpl{
|
||||
func NewIndex(service service.Auth, render *Render) Index {
|
||||
return IndexImpl{
|
||||
service: service,
|
||||
render: render,
|
||||
}
|
||||
}
|
||||
|
||||
func (handler IndexHandlerImpl) Handle(router *http.ServeMux) {
|
||||
func (handler IndexImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/", handler.handleIndexAnd404())
|
||||
}
|
||||
|
||||
func (handler IndexHandlerImpl) handleIndexAnd404() http.HandlerFunc {
|
||||
func (handler IndexImpl) handleIndexAnd404() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, _ := handler.service.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Cors(serverSettings *types.ServerSettings) func(http.Handler) http.Handler {
|
||||
func Cors(serverSettings *types.Settings) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", serverSettings.BaseUrl)
|
||||
|
||||
@@ -13,12 +13,12 @@ import (
|
||||
)
|
||||
|
||||
type Render struct {
|
||||
serverSettings *types.ServerSettings
|
||||
settings *types.Settings
|
||||
}
|
||||
|
||||
func NewRender(serverSettings *types.ServerSettings) *Render {
|
||||
func NewRender(settings *types.Settings) *Render {
|
||||
return &Render{
|
||||
serverSettings: serverSettings,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ func (render *Render) Render(r *http.Request, w http.ResponseWriter, comp templ.
|
||||
|
||||
func (render *Render) RenderLayout(r *http.Request, w http.ResponseWriter, slot templ.Component, user *service.User) {
|
||||
userComp := render.getUserComp(user)
|
||||
layout := template.Layout(slot, userComp, render.serverSettings.Environment)
|
||||
layout := template.Layout(slot, userComp, render.settings.Environment)
|
||||
|
||||
render.Render(r, w, layout)
|
||||
}
|
||||
|
||||
@@ -11,32 +11,32 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type WorkoutHandler interface {
|
||||
type Workout interface {
|
||||
Handle(router *http.ServeMux)
|
||||
}
|
||||
|
||||
type WorkoutHandlerImpl struct {
|
||||
service service.WorkoutService
|
||||
auth service.AuthService
|
||||
type WorkoutImpl struct {
|
||||
service service.Workout
|
||||
auth service.Auth
|
||||
render *Render
|
||||
}
|
||||
|
||||
func NewWorkoutHandler(service service.WorkoutService, auth service.AuthService, render *Render) WorkoutHandler {
|
||||
return WorkoutHandlerImpl{
|
||||
func NewWorkout(service service.Workout, auth service.Auth, render *Render) Workout {
|
||||
return WorkoutImpl{
|
||||
service: service,
|
||||
auth: auth,
|
||||
render: render,
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutHandlerImpl) Handle(router *http.ServeMux) {
|
||||
func (handler WorkoutImpl) Handle(router *http.ServeMux) {
|
||||
router.Handle("/workout", handler.handleWorkoutPage())
|
||||
router.Handle("POST /api/workout", handler.handleAddWorkout())
|
||||
router.Handle("GET /api/workout", handler.handleGetWorkout())
|
||||
router.Handle("DELETE /api/workout/{id}", handler.handleDeleteWorkout())
|
||||
}
|
||||
|
||||
func (handler WorkoutHandlerImpl) handleWorkoutPage() http.HandlerFunc {
|
||||
func (handler WorkoutImpl) handleWorkoutPage() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -50,7 +50,7 @@ func (handler WorkoutHandlerImpl) handleWorkoutPage() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutHandlerImpl) handleAddWorkout() http.HandlerFunc {
|
||||
func (handler WorkoutImpl) handleAddWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -77,7 +77,7 @@ func (handler WorkoutHandlerImpl) handleAddWorkout() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutHandlerImpl) handleGetWorkout() http.HandlerFunc {
|
||||
func (handler WorkoutImpl) handleGetWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
@@ -100,7 +100,7 @@ func (handler WorkoutHandlerImpl) handleGetWorkout() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (handler WorkoutHandlerImpl) handleDeleteWorkout() http.HandlerFunc {
|
||||
func (handler WorkoutImpl) handleDeleteWorkout() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := handler.auth.GetUserFromSessionId(utils.GetSessionID(r))
|
||||
if err != nil {
|
||||
|
||||
28
main.go
28
main.go
@@ -44,7 +44,7 @@ func run(ctx context.Context, database *sql.DB, env func(string) string) {
|
||||
log.Info("Starting server...")
|
||||
|
||||
// init server settings
|
||||
serverSettings := types.NewServerSettingsFromEnv(env)
|
||||
serverSettings := types.NewSettingsFromEnv(env)
|
||||
|
||||
// init db
|
||||
err := db.RunMigrations(database, "")
|
||||
@@ -101,33 +101,31 @@ func shutdownServer(s *http.Server, ctx context.Context, wg *sync.WaitGroup) {
|
||||
}
|
||||
}
|
||||
|
||||
func createHandler(d *sql.DB, serverSettings *types.ServerSettings) http.Handler {
|
||||
func createHandler(d *sql.DB, serverSettings *types.Settings) http.Handler {
|
||||
var router = http.NewServeMux()
|
||||
|
||||
authDb := db.NewAuthDbSqlite(d)
|
||||
authDb := db.NewAuthSqlite(d)
|
||||
workoutDb := db.NewWorkoutDbSqlite(d)
|
||||
|
||||
randomService := service.NewRandomServiceImpl()
|
||||
clockService := service.NewClockServiceImpl()
|
||||
mailService := service.NewMailServiceImpl(serverSettings)
|
||||
randomService := service.NewRandomImpl()
|
||||
clockService := service.NewClockImpl()
|
||||
mailService := service.NewMailImpl(serverSettings)
|
||||
|
||||
authService := service.NewAuthServiceImpl(authDb, randomService, clockService, mailService, serverSettings)
|
||||
workoutService := service.NewWorkoutServiceImpl(workoutDb, randomService, clockService, mailService, serverSettings)
|
||||
authService := service.NewAuthImpl(authDb, randomService, clockService, mailService, serverSettings)
|
||||
workoutService := service.NewWorkoutImpl(workoutDb, randomService, clockService, mailService, serverSettings)
|
||||
|
||||
render := handler.NewRender(serverSettings)
|
||||
indexHandler := handler.NewIndexHandler(authService, render)
|
||||
authHandler := handler.NewHandlerAuth(authService, render)
|
||||
workoutHandler := handler.NewWorkoutHandler(workoutService, authService, render)
|
||||
indexHandler := handler.NewIndex(authService, render)
|
||||
authHandler := handler.NewAuth(authService, render)
|
||||
workoutHandler := handler.NewWorkout(workoutService, authService, render)
|
||||
|
||||
indexHandler.Handle(router)
|
||||
workoutHandler.Handle(router)
|
||||
authHandler.Handle(router)
|
||||
|
||||
// Serve static files (CSS, JS and images)
|
||||
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
|
||||
|
||||
workoutHandler.Handle(router)
|
||||
|
||||
authHandler.Handle(router)
|
||||
|
||||
return middleware.Wrapper(
|
||||
router,
|
||||
middleware.Log,
|
||||
|
||||
110
service/auth.go
110
service/auth.go
@@ -53,7 +53,7 @@ func NewSession(session *db.Session, user *User) *Session {
|
||||
}
|
||||
}
|
||||
|
||||
type AuthService interface {
|
||||
type Auth interface {
|
||||
SignUp(email string, password string) (*User, error)
|
||||
SendVerificationMail(userId uuid.UUID, email string)
|
||||
VerifyUserEmail(token string) error
|
||||
@@ -71,26 +71,26 @@ type AuthService interface {
|
||||
GetUserFromSessionId(sessionId string) (*User, error)
|
||||
}
|
||||
|
||||
type AuthServiceImpl struct {
|
||||
dbAuth db.AuthDb
|
||||
randomGenerator RandomService
|
||||
clock ClockService
|
||||
mailService MailService
|
||||
serverSettings *types.ServerSettings
|
||||
type AuthImpl struct {
|
||||
db db.Auth
|
||||
random Random
|
||||
clock Clock
|
||||
mail Mail
|
||||
serverSettings *types.Settings
|
||||
}
|
||||
|
||||
func NewAuthServiceImpl(dbAuth db.AuthDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) *AuthServiceImpl {
|
||||
return &AuthServiceImpl{
|
||||
dbAuth: dbAuth,
|
||||
randomGenerator: randomGenerator,
|
||||
clock: clock,
|
||||
mailService: mailService,
|
||||
serverSettings: serverSettings,
|
||||
func NewAuthImpl(db db.Auth, random Random, clock Clock, mail Mail, serverSettings *types.Settings) *AuthImpl {
|
||||
return &AuthImpl{
|
||||
db: db,
|
||||
random: random,
|
||||
clock: clock,
|
||||
mail: mail,
|
||||
serverSettings: serverSettings,
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignIn(email string, password string) (*Session, error) {
|
||||
user, err := service.dbAuth.GetUserByEmail(email)
|
||||
func (service AuthImpl) SignIn(email string, password string) (*Session, error) {
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, ErrInvaidCredentials
|
||||
@@ -113,13 +113,13 @@ func (service AuthServiceImpl) SignIn(email string, password string) (*Session,
|
||||
return NewSession(session, NewUser(user)), nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
sessionId, err := service.randomGenerator.String(32)
|
||||
func (service AuthImpl) createSession(userId uuid.UUID) (*db.Session, error) {
|
||||
sessionId, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
err = service.dbAuth.DeleteOldSessions(userId)
|
||||
err = service.db.DeleteOldSessions(userId)
|
||||
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
@@ -127,7 +127,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
||||
|
||||
session := db.NewSession(sessionId, userId, service.clock.Now())
|
||||
|
||||
err = service.dbAuth.InsertSession(session)
|
||||
err = service.db.InsertSession(session)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -135,7 +135,7 @@ func (service AuthServiceImpl) createSession(userId uuid.UUID) (*db.Session, err
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignUp(email string, password string) (*User, error) {
|
||||
func (service AuthImpl) SignUp(email string, password string) (*User, error) {
|
||||
_, err := mail.ParseAddress(email)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidEmail
|
||||
@@ -145,12 +145,12 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
return nil, ErrInvalidPassword
|
||||
}
|
||||
|
||||
userId, err := service.randomGenerator.UUID()
|
||||
userId, err := service.random.UUID()
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
salt, err := service.randomGenerator.Bytes(16)
|
||||
salt, err := service.random.Bytes(16)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
|
||||
dbUser := db.NewUser(userId, email, false, nil, false, hash, salt, service.clock.Now())
|
||||
|
||||
err = service.dbAuth.InsertUser(dbUser)
|
||||
err = service.db.InsertUser(dbUser)
|
||||
if err != nil {
|
||||
if err == db.ErrUserExists {
|
||||
return nil, ErrAccountExists
|
||||
@@ -171,9 +171,9 @@ func (service AuthServiceImpl) SignUp(email string, password string) (*User, err
|
||||
return NewUser(dbUser), nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
func (service AuthImpl) SendVerificationMail(userId uuid.UUID, email string) {
|
||||
|
||||
tokens, err := service.dbAuth.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
||||
tokens, err := service.db.GetTokensByUserIdAndType(userId, db.TokenTypeEmailVerify)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -185,14 +185,14 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
||||
}
|
||||
|
||||
if token == nil {
|
||||
newTokenStr, err := service.randomGenerator.String(32)
|
||||
newTokenStr, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
token = db.NewToken(userId, newTokenStr, db.TokenTypeEmailVerify, service.clock.Now(), service.clock.Now().Add(24*time.Hour))
|
||||
|
||||
err = service.dbAuth.InsertToken(token)
|
||||
err = service.db.InsertToken(token)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -205,21 +205,21 @@ func (service AuthServiceImpl) SendVerificationMail(userId uuid.UUID, email stri
|
||||
return
|
||||
}
|
||||
|
||||
service.mailService.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||
service.mail.SendMail(email, "Welcome to ME-FIT", w.String())
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
||||
func (service AuthImpl) VerifyUserEmail(tokenStr string) error {
|
||||
|
||||
if tokenStr == "" {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
token, err := service.dbAuth.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -237,31 +237,31 @@ func (service AuthServiceImpl) VerifyUserEmail(tokenStr string) error {
|
||||
user.EmailVerified = true
|
||||
user.EmailVerifiedAt = &now
|
||||
|
||||
err = service.dbAuth.UpdateUser(user)
|
||||
err = service.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
_ = service.dbAuth.DeleteToken(token.Token)
|
||||
_ = service.db.DeleteToken(token.Token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SignOut(sessionId string) error {
|
||||
func (service AuthImpl) SignOut(sessionId string) error {
|
||||
|
||||
return service.dbAuth.DeleteSession(sessionId)
|
||||
return service.db.DeleteSession(sessionId)
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||
func (service AuthImpl) GetUserFromSessionId(sessionId string) (*User, error) {
|
||||
if sessionId == "" {
|
||||
return nil, ErrSessionIdInvalid
|
||||
}
|
||||
|
||||
session, err := service.dbAuth.GetSession(sessionId)
|
||||
session, err := service.db.GetSession(sessionId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(session.UserId)
|
||||
user, err := service.db.GetUser(session.UserId)
|
||||
if err != nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -273,19 +273,19 @@ func (service AuthServiceImpl) GetUserFromSessionId(sessionId string) (*User, er
|
||||
}
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) DeleteAccount(user *User) error {
|
||||
func (service AuthImpl) DeleteAccount(user *User) error {
|
||||
|
||||
err := service.dbAuth.DeleteUser(user.Id)
|
||||
err := service.db.DeleteUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go service.mailService.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||
go service.mail.SendMail(user.Email, "Account deleted", "Your account has been deleted")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass string) error {
|
||||
func (service AuthImpl) ChangePassword(user *User, currPass, newPass string) error {
|
||||
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
@@ -300,7 +300,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
return err
|
||||
}
|
||||
|
||||
userDb, err := service.dbAuth.GetUser(user.Id)
|
||||
userDb, err := service.db.GetUser(user.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -309,7 +309,7 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
|
||||
userDb.Password = newHash
|
||||
|
||||
err = service.dbAuth.UpdateUser(userDb)
|
||||
err = service.db.UpdateUser(userDb)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -317,14 +317,14 @@ func (service AuthServiceImpl) ChangePassword(user *User, currPass, newPass stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
func (service AuthImpl) SendForgotPasswordMail(email string) error {
|
||||
|
||||
tokenStr, err := service.randomGenerator.String(32)
|
||||
tokenStr, err := service.random.String(32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUserByEmail(email)
|
||||
user, err := service.db.GetUserByEmail(email)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return nil
|
||||
@@ -335,7 +335,7 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
|
||||
token := db.NewToken(user.Id, tokenStr, db.TokenTypePasswordReset, service.clock.Now(), service.clock.Now().Add(15*time.Minute))
|
||||
|
||||
err = service.dbAuth.InsertToken(token)
|
||||
err = service.db.InsertToken(token)
|
||||
if err != nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
@@ -346,28 +346,28 @@ func (service AuthServiceImpl) SendForgotPasswordMail(email string) error {
|
||||
log.Error("Could not render reset password email: %v", err)
|
||||
return types.ErrInternal
|
||||
}
|
||||
go service.mailService.SendMail(email, "Reset Password", mail.String())
|
||||
go service.mail.SendMail(email, "Reset Password", mail.String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
func (service AuthImpl) ForgotPassword(tokenStr string, newPass string) error {
|
||||
|
||||
if !isPasswordValid(newPass) {
|
||||
return ErrInvalidPassword
|
||||
}
|
||||
|
||||
token, err := service.dbAuth.GetToken(tokenStr)
|
||||
token, err := service.db.GetToken(tokenStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = service.dbAuth.DeleteToken(tokenStr)
|
||||
err = service.db.DeleteToken(tokenStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := service.dbAuth.GetUser(token.UserId)
|
||||
user, err := service.db.GetUser(token.UserId)
|
||||
if err != nil {
|
||||
log.Error("Could not get user from token: %v", err)
|
||||
return types.ErrInternal
|
||||
@@ -376,7 +376,7 @@ func (service AuthServiceImpl) ForgotPassword(tokenStr string, newPass string) e
|
||||
passHash := GetHashPassword(newPass, user.Salt)
|
||||
|
||||
user.Password = passHash
|
||||
err = service.dbAuth.UpdateUser(user)
|
||||
err = service.db.UpdateUser(user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func TestSignIn(t *testing.T) {
|
||||
mockClock.EXPECT().Now().Return(time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC))
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actualSession, err := underTest.SignIn(user.Email, "password")
|
||||
assert.Nil(t, err)
|
||||
@@ -76,7 +76,7 @@ func TestSignIn(t *testing.T) {
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test@test.de", "wrong password")
|
||||
|
||||
@@ -91,7 +91,7 @@ func TestSignIn(t *testing.T) {
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
assert.Equal(t, ErrInvaidCredentials, err)
|
||||
@@ -105,7 +105,7 @@ func TestSignIn(t *testing.T) {
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignIn("test", "test")
|
||||
|
||||
@@ -123,7 +123,7 @@ func TestSignUp(t *testing.T) {
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err := underTest.SignUp("invalid email address", "SomeStrongPassword123!")
|
||||
|
||||
@@ -137,7 +137,7 @@ func TestSignUp(t *testing.T) {
|
||||
mockClock := mocks.NewMockClockService(t)
|
||||
mockMail := mocks.NewMockMailService(t)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
weakPasswords := []string{
|
||||
"123!ab", // too short
|
||||
@@ -165,7 +165,7 @@ func TestSignUp(t *testing.T) {
|
||||
EmailVerified: false,
|
||||
}
|
||||
|
||||
random := NewRandomServiceImpl()
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
@@ -179,7 +179,7 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(expected.Id, expected.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(nil)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
actual, err := underTest.SignUp(expected.Email, password)
|
||||
|
||||
@@ -200,7 +200,7 @@ func TestSignUp(t *testing.T) {
|
||||
Email: "some@valid.email",
|
||||
}
|
||||
|
||||
random := NewRandomServiceImpl()
|
||||
random := NewRandomImpl()
|
||||
salt, err := random.Bytes(16)
|
||||
assert.Nil(t, err)
|
||||
password := "SomeStrongPassword123!"
|
||||
@@ -214,7 +214,7 @@ func TestSignUp(t *testing.T) {
|
||||
|
||||
mockAuthDb.EXPECT().InsertUser(db.NewUser(user.Id, user.Email, false, nil, false, GetHashPassword(password, salt), salt, createTime)).Return(db.ErrUserExists)
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
_, err = underTest.SignUp(user.Email, password)
|
||||
assert.Equal(t, ErrAccountExists, err)
|
||||
@@ -244,7 +244,7 @@ func TestSendVerificationMail(t *testing.T) {
|
||||
return strings.Contains(message, token.Token)
|
||||
})).Return()
|
||||
|
||||
underTest := NewAuthServiceImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.ServerSettings{})
|
||||
underTest := NewAuthImpl(mockAuthDb, mockRandom, mockClock, mockMail, &types.Settings{})
|
||||
|
||||
underTest.SendVerificationMail(userId, email)
|
||||
})
|
||||
|
||||
@@ -2,16 +2,16 @@ package service
|
||||
|
||||
import "time"
|
||||
|
||||
type ClockService interface {
|
||||
type Clock interface {
|
||||
Now() time.Time
|
||||
}
|
||||
|
||||
type ClockServiceImpl struct{}
|
||||
type ClockImpl struct{}
|
||||
|
||||
func NewClockServiceImpl() ClockService {
|
||||
return &ClockServiceImpl{}
|
||||
func NewClockImpl() Clock {
|
||||
return &ClockImpl{}
|
||||
}
|
||||
|
||||
func (c *ClockServiceImpl) Now() time.Time {
|
||||
func (c *ClockImpl) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
@@ -8,25 +8,25 @@ import (
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type MailService interface {
|
||||
type Mail interface {
|
||||
// Sending an email is a fire and forget operation. Thus no error handling
|
||||
SendMail(to string, subject string, message string)
|
||||
}
|
||||
|
||||
type MailServiceImpl struct {
|
||||
serverSettings *types.ServerSettings
|
||||
type MailImpl struct {
|
||||
server *types.Settings
|
||||
}
|
||||
|
||||
func NewMailServiceImpl(serverSettings *types.ServerSettings) MailServiceImpl {
|
||||
return MailServiceImpl{serverSettings: serverSettings}
|
||||
func NewMailImpl(server *types.Settings) MailImpl {
|
||||
return MailImpl{server: server}
|
||||
}
|
||||
|
||||
func (m MailServiceImpl) SendMail(to string, subject string, message string) {
|
||||
if m.serverSettings.Smtp == nil {
|
||||
func (m MailImpl) SendMail(to string, subject string, message string) {
|
||||
if m.server.Smtp == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := m.serverSettings.Smtp
|
||||
s := m.server.Smtp
|
||||
|
||||
auth := smtp.PlainAuth("", s.User, s.Pass, s.Host)
|
||||
|
||||
|
||||
@@ -10,20 +10,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RandomService interface {
|
||||
type Random interface {
|
||||
Bytes(size int) ([]byte, error)
|
||||
String(size int) (string, error)
|
||||
UUID() (uuid.UUID, error)
|
||||
}
|
||||
|
||||
type RandomServiceImpl struct {
|
||||
type RandomImpl struct {
|
||||
}
|
||||
|
||||
func NewRandomServiceImpl() *RandomServiceImpl {
|
||||
return &RandomServiceImpl{}
|
||||
func NewRandomImpl() *RandomImpl {
|
||||
return &RandomImpl{}
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
||||
func (r *RandomImpl) Bytes(size int) ([]byte, error) {
|
||||
b := make([]byte, 32)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
@@ -34,7 +34,7 @@ func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) String(size int) (string, error) {
|
||||
func (r *RandomImpl) String(size int) (string, error) {
|
||||
bytes, err := r.Bytes(size)
|
||||
if err != nil {
|
||||
return "", types.ErrInternal
|
||||
@@ -43,6 +43,6 @@ func (r *RandomServiceImpl) String(size int) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func (r *RandomServiceImpl) UUID() (uuid.UUID, error) {
|
||||
func (r *RandomImpl) UUID() (uuid.UUID, error) {
|
||||
return uuid.NewRandom()
|
||||
}
|
||||
|
||||
@@ -9,27 +9,27 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type WorkoutService interface {
|
||||
type Workout interface {
|
||||
AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error)
|
||||
DeleteWorkout(user *User, rowId int) error
|
||||
GetWorkouts(user *User) ([]*WorkoutDto, error)
|
||||
}
|
||||
|
||||
type WorkoutServiceImpl struct {
|
||||
dbWorkout db.WorkoutDb
|
||||
randomGenerator RandomService
|
||||
clock ClockService
|
||||
mailService MailService
|
||||
serverSettings *types.ServerSettings
|
||||
type WorkoutImpl struct {
|
||||
db db.WorkoutDb
|
||||
random Random
|
||||
clock Clock
|
||||
mail Mail
|
||||
settings *types.Settings
|
||||
}
|
||||
|
||||
func NewWorkoutServiceImpl(dbWorkout db.WorkoutDb, randomGenerator RandomService, clock ClockService, mailService MailService, serverSettings *types.ServerSettings) WorkoutService {
|
||||
return WorkoutServiceImpl{
|
||||
dbWorkout: dbWorkout,
|
||||
randomGenerator: randomGenerator,
|
||||
clock: clock,
|
||||
mailService: mailService,
|
||||
serverSettings: serverSettings,
|
||||
func NewWorkoutImpl(db db.WorkoutDb, random Random, clock Clock, mail Mail, settings *types.Settings) Workout {
|
||||
return WorkoutImpl{
|
||||
db: db,
|
||||
random: random,
|
||||
clock: clock,
|
||||
mail: mail,
|
||||
settings: settings,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ var (
|
||||
ErrInputValues = errors.New("invalid input values")
|
||||
)
|
||||
|
||||
func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
||||
func (service WorkoutImpl) AddWorkout(user *User, workoutDto *WorkoutDto) (*WorkoutDto, error) {
|
||||
|
||||
if workoutDto.Date == "" || workoutDto.Type == "" || workoutDto.Sets == "" || workoutDto.Reps == "" {
|
||||
return nil, ErrInputValues
|
||||
@@ -87,7 +87,7 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
||||
|
||||
workoutInsert := db.NewWorkoutInsert(date, workoutDto.Type, sets, reps)
|
||||
|
||||
workout, err := service.dbWorkout.InsertWorkout(user.Id, workoutInsert)
|
||||
workout, err := service.db.InsertWorkout(user.Id, workoutInsert)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,20 +95,20 @@ func (service WorkoutServiceImpl) AddWorkout(user *User, workoutDto *WorkoutDto)
|
||||
return NewWorkoutDtoFromDb(workout), nil
|
||||
}
|
||||
|
||||
func (service WorkoutServiceImpl) DeleteWorkout(user *User, rowId int) error {
|
||||
func (service WorkoutImpl) DeleteWorkout(user *User, rowId int) error {
|
||||
if user == nil {
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
return service.dbWorkout.DeleteWorkout(user.Id, rowId)
|
||||
return service.db.DeleteWorkout(user.Id, rowId)
|
||||
}
|
||||
|
||||
func (service WorkoutServiceImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
||||
func (service WorkoutImpl) GetWorkouts(user *User) ([]*WorkoutDto, error) {
|
||||
if user == nil {
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
workouts, err := service.dbWorkout.GetWorkouts(user.Id)
|
||||
workouts, err := service.db.GetWorkouts(user.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"me-fit/log"
|
||||
)
|
||||
|
||||
type ServerSettings struct {
|
||||
type Settings struct {
|
||||
Port string
|
||||
PrometheusEnabled bool
|
||||
|
||||
@@ -22,7 +22,7 @@ type SmtpSettings struct {
|
||||
FromName string
|
||||
}
|
||||
|
||||
func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
||||
func NewSettingsFromEnv(env func(string) string) *Settings {
|
||||
|
||||
var smtp *SmtpSettings
|
||||
if env("SMTP_ENABLED") == "true" {
|
||||
@@ -55,7 +55,7 @@ func NewServerSettingsFromEnv(env func(string) string) *ServerSettings {
|
||||
}
|
||||
}
|
||||
|
||||
settings := &ServerSettings{
|
||||
settings := &Settings{
|
||||
Port: env("PORT"),
|
||||
PrometheusEnabled: env("PROMETHEUS_ENABLED") == "true",
|
||||
BaseUrl: env("BASE_URL"),
|
||||
|
||||
Reference in New Issue
Block a user