-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
48 lines (41 loc) · 1.5 KB
/
Copy pathcommand.go
File metadata and controls
48 lines (41 loc) · 1.5 KB
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
package main
import (
"context"
"fmt"
"github.com/josephus-git/gator/internal/database"
)
// command represents a single executable command with its name and associated handler arguments.
type command struct {
Name string
Handler []string
}
// commands holds a collection of named functions that can handle specific commands.
type commands struct {
Cmds map[string]func(*state, command) error
}
// run executes the handler function associated with the given command name.
func (c *commands) run(s *state, cmd command) error {
cmdFunc, ok := c.Cmds[cmd.Name]
if !ok {
return fmt.Errorf("available commands: login, reset, register, users, addfeed, feeds, follow, following, unfollow, agg, browse")
}
return cmdFunc(s, cmd)
}
// middlewareLoggedIn creates a middleware that ensures a user is authenticated before executing the handler.
func middlewareLoggedIn(handler func(s *state, cmd command, user database.User) error) func(*state, command) error {
return func(s *state, cmd command) error {
// check if user is available in database
if s.cfg.Current_user_name == "" {
return fmt.Errorf("authentication required: no user available")
}
user, err := s.db.GetUser(context.Background(), s.cfg.Current_user_name)
if err != nil {
return fmt.Errorf("authentication failed: error fetching user")
}
return handler(s, cmd, user)
}
}
// register adds a new command and its handler function to the commands map.
func (c *commands) register(name string, f func(*state, command) error) {
c.Cmds[name] = f
}