-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (41 loc) · 1.24 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
package main
import (
"blogator/api"
"blogator/internal/database"
"blogator/scraping"
"database/sql"
_ "github.com/lib/pq"
)
import (
"fmt"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
db, err := sql.Open("postgres", os.Getenv("DB_URL"))
dbQueries := database.New(db)
config := &api.Config{
DB: dbQueries,
FeedFetchConcurrency: 3,
FeedFetchIntervalSecond: 5,
}
go scraping.ScheduledFetchPosts(config)
mux := http.NewServeMux()
mux.HandleFunc("GET /v1/healthz", config.HealthCheck)
mux.HandleFunc("GET /v1/user", config.RequireAuth(config.GetUser))
mux.HandleFunc("POST /v1/users", config.CreateUser)
mux.HandleFunc("GET /v1/posts", config.RequireAuth(config.GetPosts))
mux.HandleFunc("GET /v1/feeds", config.GetFeeds)
mux.HandleFunc("POST /v1/feeds", config.RequireAuth(config.CreateFeed))
mux.HandleFunc("POST /v1/feeds/{feedId}/follow", config.RequireAuth(config.FollowFeed))
mux.HandleFunc("DELETE /v1/feeds/{feedId}/follow", config.RequireAuth(config.UnfollowFeed))
addr := fmt.Sprintf(":%s", os.Getenv("PORT"))
fmt.Println("Server running on", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}