-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (88 loc) · 1.79 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"database/sql"
"log"
"net/http"
"os"
"time"
"github.com/bsach64/blogAggregator/internal/database"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
type apiConfig struct {
DB *database.Queries
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Could not get environment variables..")
}
port := os.Getenv("PORT")
dbURL := os.Getenv("DB")
db, err := sql.Open("postgres", dbURL)
dbc := database.New(db)
api := apiConfig{
DB: dbc,
}
go startScraping(dbc, 10, time.Minute)
router := chi.NewRouter()
handler := cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
})
router.Use(handler)
routerV1 := chi.NewRouter()
routerV1.Get(
"/readiness",
handleReadiness,
)
routerV1.Get(
"/err",
handleErr,
)
routerV1.Post(
"/users",
api.handleCreateUsers,
)
routerV1.Get(
"/users",
api.middlewareAuth(api.handleGetUserFromApi),
)
routerV1.Post(
"/feeds",
api.middlewareAuth(api.handleCreateFeed),
)
routerV1.Get(
"/all_feeds",
api.handleGetAllFeeds,
)
routerV1.Post(
"/feed_follows",
api.middlewareAuth(api.handleFeedFollow),
)
routerV1.Delete(
"/feed_follows/{feedfollowID}",
api.middlewareAuth(api.handleDeleteFeedFollow),
)
routerV1.Get(
"/feed_follows",
api.middlewareAuth(api.handleGetFeedFollows),
)
routerV1.Get(
"/posts",
api.middlewareAuth(api.handleGetPosts),
)
router.Mount("/v1", routerV1)
server := &http.Server{
Addr: ":" + port,
Handler: router,
}
err = server.ListenAndServe()
log.Print(err)
}