-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (95 loc) · 2.94 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
107
108
109
110
111
112
113
114
115
116
117
package main
// Import Postgres driver w/ side effects
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/ajpotts01/go-blog-aggregator/api"
"github.com/ajpotts01/go-blog-aggregator/internal/database"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func getApiConfig(dbConnStr string) (*api.ApiConfig, error) {
db, err := sql.Open("postgres", dbConnStr)
if err != nil {
return &api.ApiConfig{}, err
}
dbq := database.New(db)
return &api.ApiConfig{
DbConn: dbq,
MaxFeedsProcessed: 5,
}, nil
}
func getApiRouterV1(config *api.ApiConfig) *chi.Mux {
const errEndpoint = "/err"
const readyEndpoint = "/readiness"
const usersEndpoint = "/users"
const feedsEndpoint = "/feeds"
const followsEndpoint = "/follows"
const singleFollowEndpoint = "/follows/{id}"
const postsEndpoint = "/posts"
apiRouter := chi.NewRouter()
apiRouter.Get(readyEndpoint, api.Ready)
apiRouter.Get(errEndpoint, api.Err)
apiRouter.Post(usersEndpoint, config.CreateUser)
apiRouter.Get(usersEndpoint, config.AuthMiddleware(config.GetUser))
apiRouter.Post(feedsEndpoint, config.AuthMiddleware(config.CreateFeed))
apiRouter.Get(feedsEndpoint, config.GetFeeds)
apiRouter.Post(followsEndpoint, config.AuthMiddleware(config.FollowFeed))
apiRouter.Get(followsEndpoint, config.AuthMiddleware(config.GetFollows))
apiRouter.Delete(singleFollowEndpoint, config.AuthMiddleware(config.UnfollowFeed))
apiRouter.Get(postsEndpoint, config.AuthMiddleware(config.GetPostsForUser))
return apiRouter
}
func main() {
godotenv.Load()
port := os.Getenv("PORT")
dbConnStr := os.Getenv("PG_CONN")
// Database
apiConfig, err := getApiConfig(dbConnStr)
fmt.Printf("API config: %v", apiConfig)
if err != nil {
log.Fatalf("Error setting up database: %v", err)
}
// App router
appRouter := chi.NewRouter()
// CORS
corsOptions := cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET, POST, OPTIONS, PUT, DELETE"},
AllowedHeaders: []string{"*"},
}
appRouter.Use(cors.Handler(corsOptions))
appRouter.Mount("/v1", getApiRouterV1(apiConfig))
server := &http.Server{
Addr: ":" + port,
Handler: appRouter,
}
// url := "https://blog.boot.dev/index.xml"
// altUrl := "https://wagslane.dev/index.xml"
// log.Printf("URL: %v", url)
// log.Printf("Alt URL: %v", url)
// log.Printf("Using alt URL")
//feed, err := apiConfig.FetchFeed(altUrl)
// if err != nil {
// log.Printf("Error getting feed: %v", err)
// }
// for _, c := range feed.Channels {
// log.Printf("Channel Link: %v", c.Link)
// //log.Printf("Atom: %v", c.AtomLink)
// log.Printf("Items:")
// for _, i := range c.Items {
// log.Printf("Title: %v", i.Title)
// log.Printf("Desc: %v", i.Description)
// log.Printf("Publish Date: %v", i.PubDate)
// log.Printf("Link: %v", i.Link)
// }
// }
go apiConfig.FetchLoop()
log.Printf("Now serving on port: %v", port)
log.Fatal(server.ListenAndServe())
}