diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-10-27 01:38:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-10-27 01:53:05 +0900 |
| commit | af87dd1de0c1dd0e6284170aef807224d5c8ccb5 (patch) | |
| tree | 50434d908ad3ee5a1ccd9414f8a13c0a9dee05f5 /backend/main.go | |
| parent | 469e469dd3f77bc8cd22bbcd87017eb69569ac23 (diff) | |
| download | feedaka-af87dd1de0c1dd0e6284170aef807224d5c8ccb5.tar.gz feedaka-af87dd1de0c1dd0e6284170aef807224d5c8ccb5.tar.zst feedaka-af87dd1de0c1dd0e6284170aef807224d5c8ccb5.zip | |
feat(backend): create-user command
Diffstat (limited to 'backend/main.go')
| -rw-r--r-- | backend/main.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/backend/main.go b/backend/main.go index 3bbe2f7..117cfb9 100644 --- a/backend/main.go +++ b/backend/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "context" "database/sql" "embed" @@ -10,6 +11,7 @@ import ( "net/http" "os" "os/signal" + "strings" "syscall" "time" @@ -23,6 +25,7 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/mmcdole/gofeed" "github.com/vektah/gqlparser/v2/ast" + "golang.org/x/crypto/bcrypt" "undef.ninja/x/feedaka/db" "undef.ninja/x/feedaka/graphql" @@ -129,6 +132,53 @@ func fetchAllFeeds(ctx context.Context) error { return result.ErrorOrNil() } +func runCreateUser(database *sql.DB) { + queries := db.New(database) + reader := bufio.NewReader(os.Stdin) + + // Read username + fmt.Print("Enter username: ") + username, err := reader.ReadString('\n') + if err != nil { + log.Fatalf("Failed to read username: %v", err) + } + username = strings.TrimSpace(username) + if username == "" { + log.Fatal("Username cannot be empty") + } + + // Read password + fmt.Print("Enter password: ") + password, err := reader.ReadString('\n') + if err != nil { + log.Fatalf("Failed to read password: %v", err) + } + password = strings.TrimSpace(password) + + // Validate password length + if len(password) < 15 { + log.Fatalf("Password must be at least 15 characters long (got %d characters)", len(password)) + } + + // Hash password + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + log.Fatalf("Failed to hash password: %v", err) + } + + // Create user + ctx := context.Background() + user, err := queries.CreateUser(ctx, db.CreateUserParams{ + Username: username, + PasswordHash: string(hashedPassword), + }) + if err != nil { + log.Fatalf("Failed to create user: %v", err) + } + + log.Printf("User created successfully: ID=%d, Username=%s", user.ID, user.Username) +} + func scheduled(ctx context.Context, d time.Duration, fn func()) { ticker := time.NewTicker(d) go func() { @@ -146,6 +196,7 @@ func scheduled(ctx context.Context, d time.Duration, fn func()) { func main() { // Parse command line flags var migrate = flag.Bool("migrate", false, "Run database migrations") + var createUser = flag.Bool("create-user", false, "Create a new user") flag.Parse() port := os.Getenv("FEEDAKA_PORT") @@ -171,6 +222,12 @@ func main() { return } + // Create user mode + if *createUser { + runCreateUser(database) + return + } + err = db.ValidateSchemaVersion(database) if err != nil { log.Fatal(err) |
