-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (77 loc) · 2.55 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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/Jhnvlglmlbrt/rss-aggregator/api"
"github.com/Jhnvlglmlbrt/rss-aggregator/internal/database"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func init() {
if err := godotenv.Load(".env"); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
}
func main() {
// feed, err := utils.UrlToFeed("https://blog.boot.dev/index.xml")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(feed)
portStr, exists := os.LookupEnv("PORT")
if !exists {
log.Fatal("PORT not found in .env file")
}
dbURL, exists := os.LookupEnv("DB_URL")
if !exists {
log.Fatal("dbURL not found in .env file")
}
conn, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal("Can't connect to database ", err)
}
db := database.New(conn)
apiCfg := api.ApiConfig{
DB: db,
}
go api.StartScraping(db, 10, time.Minute)
router := chi.NewRouter()
router.Use(cors.Handler(cors.Options{
// AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts
AllowedOrigins: []string{"https://*", "http://*"},
// AllowOriginFunc: func(r *http.Request, origin string) bool { return true },
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by anjy of major browsers
}))
v1Router := chi.NewRouter()
v1Router.Get("/ready", api.HandlerReadiness)
v1Router.Get("/err", api.HandlerErr)
v1Router.Post("/users", apiCfg.HandlerCreateUser)
v1Router.Get("/users", apiCfg.MiddlewareAuth(apiCfg.HandlerGetUser))
v1Router.Post("/feeds", apiCfg.MiddlewareAuth(apiCfg.HandlerCreateFeed))
v1Router.Get("/feeds", (apiCfg.HandlerGetFeeds))
v1Router.Post("/feed_follows", apiCfg.MiddlewareAuth(apiCfg.HandlerCreateFeedFollow))
v1Router.Get("/feed_follows", apiCfg.MiddlewareAuth(apiCfg.HandlerGetFeedFollows))
v1Router.Delete("/feed_follows/{feedFollowID}", apiCfg.MiddlewareAuth(apiCfg.HandlerDeleteFeedFollow))
v1Router.Get("/posts", apiCfg.MiddlewareAuth(apiCfg.HandlerGetPostsForUser))
router.Mount("/v1", v1Router)
srv := &http.Server{
Handler: router,
Addr: ":" + portStr,
}
log.Printf("Server starting on port %v", portStr)
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
fmt.Println("Port: ", portStr)
}