-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (87 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"github.com/Rodabaugh/gator/internal/config"
"github.com/Rodabaugh/gator/internal/database"
_ "github.com/lib/pq"
)
type state struct {
db *database.Queries
config *config.Config
}
func main() {
// Initialize config
cfg, err := config.Read()
if err != nil {
log.Fatalf("error reading config: %v", err)
}
// Connect to the database
dbURL := cfg.DbURL
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatalf("Unable to connect to the database: %v", err)
}
dbQueries := database.New(db)
appState := &state{
db: dbQueries,
config: cfg,
}
if len(os.Args) < 2 {
log.Fatalf("not enough arguments provided")
}
// Extract command name and arguments
commandName := os.Args[1]
commandArgs := os.Args[2:]
// Create command instance
cmd := command{
name: commandName,
arguments: commandArgs,
}
// Get the commands map
cmds := &commands{handlers: make(map[string]func(*state, command) error)}
// Register commands
cmds.register("login", handlerLogin)
cmds.register("register", handlerRegister)
cmds.register("reset", handlerReset)
cmds.register("users", handlerListUsers)
cmds.register("agg", handlerAgg)
cmds.register("addfeed", middlewareLoggedIn(handlerAddFeed))
cmds.register("feeds", handlerFeeds)
cmds.register("follow", middlewareLoggedIn(handlerFollow))
cmds.register("following", middlewareLoggedIn(handlerListFeedFollows))
cmds.register("unfollow", middlewareLoggedIn(handlerUnfollow))
cmds.register("browse", middlewareLoggedIn(handlerBrowse))
cmds.register("help", handlerHelp)
// Look up and run the handler
if err := cmds.run(appState, cmd); err != nil {
log.Fatal(err)
}
}
func middlewareLoggedIn(handler func(s *state, cmd command, user database.User) error) func(*state, command) error {
return func(s *state, cmd command) error {
user, err := s.db.GetUser(context.Background(), s.config.CurrentUserName)
if err != nil {
return fmt.Errorf("unable to get user: %w", err)
}
return handler(s, cmd, user)
}
}
func handlerHelp(s *state, cmd command) error {
fmt.Println("Available Commands")
fmt.Println("* register")
fmt.Println("* login")
fmt.Println("* users")
fmt.Println("* addfeed")
fmt.Println("* feeds")
fmt.Println("* follow")
fmt.Println("* following")
fmt.Println("* unfollow")
fmt.Println("* agg")
fmt.Println("* browse")
fmt.Println("* help")
return nil
}