-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (56 loc) · 1.28 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
package main
import (
"database/sql"
"log"
"os"
_ "github.com/lib/pq"
"github.com/nudopnu/gator/internal/config"
"github.com/nudopnu/gator/internal/database"
)
type state struct {
db *database.Queries
cfg *config.Config
}
func main() {
cfg, err := config.Read()
if err != nil {
log.Fatal(err)
return
}
dbURL := cfg.DBURL
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
s := state{
db: dbQueries,
cfg: &cfg,
}
cmds := commands{
handler: map[string]func(*state, command) error{},
}
cmds.register("login", handlerLogin)
cmds.register("register", handlerRegister)
cmds.register("reset", handlerReset)
cmds.register("users", handlerUsers)
cmds.register("agg", handlerAgg)
cmds.register("feeds", handlerFeeds)
cmds.register("browse", handlerBrowse)
cmds.register("addfeed", middlewareLoggedIn(handlerAddFeed))
cmds.register("follow", middlewareLoggedIn(handlerFollow))
cmds.register("unfollow", middlewareLoggedIn(handlerUnfollow))
cmds.register("following", middlewareLoggedIn(handlerFollowing))
if len(os.Args) < 2 {
log.Fatal("no command provided")
}
cmd := command{
name: os.Args[1],
args: os.Args[2:],
}
// Run the actual command
err = cmds.run(&s, cmd)
if err != nil {
log.Fatal(err)
}
}