-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (54 loc) · 1.71 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
package main
import (
"database/sql"
"github/alexisagos/rssagg/internal/database"
"log"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
_ "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 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.handlerFeedsCreate))
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.handlerFeedFollowsCreate))
mux.HandleFunc("DELETE /v1/feed_follows/{feedFollowID}", apiCfg.middlewareAuth(apiCfg.handlerFeedFollowsDelete))
mux.HandleFunc("GET /v1/posts", apiCfg.middlewareAuth(apiCfg.handlerPostsGet))
mux.HandleFunc("GET /v1/healthz", handlerReadiness)
mux.HandleFunc("GET /v1/err", handlerError)
corsMux := corsMiddleware(mux)
srv := &http.Server{
Addr: ":" + port,
Handler: corsMux,
}
const collectionConcurrency = 10
const collectionInterval = time.Minute
go startScraping(dbQueries, collectionConcurrency, collectionInterval)
log.Printf("Serving on port: %s\n", port)
log.Fatal(srv.ListenAndServe())
}