Compare commits

...

5 Commits

Author SHA1 Message Date
7d6a521f12 feat: call admin ensure function 2025-05-18 20:16:26 +02:00
24a9c8a9ac feat: ensure admin user 2025-05-18 20:16:18 +02:00
e2a55588df feat: find user by email function 2025-05-18 20:16:10 +02:00
e7354df10e feat: phone number is not required 2025-05-18 20:15:58 +02:00
66b7df32f3 feat: added godotenv 2025-05-18 20:15:39 +02:00
7 changed files with 74 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import (
"gitea.local/admin/hspguard/cmd/hspguard/api"
"gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/user"
"github.com/jackc/pgx/v5"
)
@ -21,6 +22,8 @@ func main() {
repo := repository.New(conn)
user.EnsureAdminUser(ctx, repo)
server := api.NewAPIServer(":3000", repo)
if err := server.Run(); err != nil {
log.Fatalln("ERR: Failed to start server:", err)

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx v3.6.2+incompatible // indirect
github.com/jackc/pgx/v5 v5.7.5 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/text v0.25.0 // indirect

2
go.sum
View File

@ -11,6 +11,8 @@ github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yI
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jackc/pgx/v5 v5.7.5 h1:JHGfMnQY+IEtGM63d+NGMjoRpysB2JBwDr5fsngwmJs=
github.com/jackc/pgx/v5 v5.7.5/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

View File

@ -45,6 +45,27 @@ func (q *Queries) FindAllUsers(ctx context.Context) ([]User, error) {
return items, nil
}
const findUserEmail = `-- name: FindUserEmail :one
SELECT id, email, full_name, password_hash, is_admin, created_at, updated_at, last_login, phone_number FROM users WHERE email = $1 LIMIT 1
`
func (q *Queries) FindUserEmail(ctx context.Context, email string) (User, error) {
row := q.db.QueryRow(ctx, findUserEmail, email)
var i User
err := row.Scan(
&i.ID,
&i.Email,
&i.FullName,
&i.PasswordHash,
&i.IsAdmin,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastLogin,
&i.PhoneNumber,
)
return i, err
}
const insertUser = `-- name: InsertUser :one
INSERT INTO users (
email, full_name, password_hash, is_admin

44
internal/user/admin.go Normal file
View File

@ -0,0 +1,44 @@
package user
import (
"context"
"log"
"os"
"gitea.local/admin/hspguard/internal/repository"
"github.com/google/uuid"
)
func EnsureAdminUser(ctx context.Context, repo *repository.Queries) {
adminName := os.Getenv("ADMIN_NAME")
if adminName == "" {
adminName = "admin"
}
adminEmail := os.Getenv("ADMIN_EMAIL")
adminPassword := os.Getenv("ADMIN_PASSWORD")
if adminEmail == "" {
log.Fatalln("ERR: ADMIN_EMAIL env variable is required")
}
_, err := repo.FindUserEmail(ctx, adminEmail)
if err != nil {
if adminPassword == "" {
log.Fatalln("ERR: ADMIN_PASSWORD env variable is required")
}
if _, err := createAdmin(ctx, adminName, adminEmail, adminPassword, repo); err != nil {
log.Fatalln("ERR: Failed to create admin account:", err)
}
}
}
func createAdmin(ctx context.Context, name, email, password string, repo *repository.Queries) (uuid.UUID, error) {
return repo.InsertUser(ctx, repository.InsertUserParams{
FullName: name,
Email: email,
PasswordHash: password,
IsAdmin: true,
})
}

View File

@ -1,7 +1,7 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE users
ADD phone_number TEXT NOT NULL;
ADD phone_number TEXT DEFAULT '';
-- +goose StatementEnd
-- +goose Down

View File

@ -9,3 +9,5 @@ INSERT INTO users (
)
RETURNING id;
-- name: FindUserEmail :one
SELECT * FROM users WHERE email = $1 LIMIT 1;