-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (53 loc) · 1.7 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
"github.com/bootdotdev/blog-agg-solution-snapshot-v0/internal/database"
_ "github.com/lib/pq"
)
type apiConfig struct {
DB *database.Queries
}
func main() {
godotenv.Load(".env")
port := os.Getenv("PORT")
if port == "" {
log.Fatal("PORT environment variable is not set")
}
dbURL := os.Getenv("DATABASE_URL")
if dbURL == "" {
log.Fatal("DATABASE_URL environment variable is not set")
}
db, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
apiCfg := apiConfig{
DB: dbQueries,
}
mux := http.NewServeMux()
mux.HandleFunc("POST /v1/users", apiCfg.handlerUsersCreate)
mux.HandleFunc("GET /v1/users", apiCfg.middlewareAuth(apiCfg.handlerUsersGet))
mux.HandleFunc("POST /v1/feeds", apiCfg.middlewareAuth(apiCfg.handlerFeedCreate))
mux.HandleFunc("GET /v1/feeds", apiCfg.handlerFeedsGet)
mux.HandleFunc("GET /v1/feed_follows", apiCfg.middlewareAuth(apiCfg.handlerFeedFollowsGet))
mux.HandleFunc("POST /v1/feed_follows", apiCfg.middlewareAuth(apiCfg.handlerFeedFollowCreate))
mux.HandleFunc("DELETE /v1/feed_follows/{feedFollowID}", apiCfg.middlewareAuth(apiCfg.handlerFeedFollowDelete))
mux.HandleFunc("GET /v1/posts", apiCfg.middlewareAuth(apiCfg.handlerPostsGet))
mux.HandleFunc("GET /v1/healthz", handlerReadiness)
mux.HandleFunc("GET /v1/err", handlerErr)
srv := &http.Server{
Addr: ":" + port,
Handler: mux,
}
const collectionConcurrency = 10
const collectionInterval = time.Minute
go startScraping(dbQueries, collectionConcurrency, collectionInterval)
log.Printf("Serving on port: %s\n", port)
log.Fatal(srv.ListenAndServe())
}