-
Notifications
You must be signed in to change notification settings - Fork 0
/
mian.go
79 lines (67 loc) · 2.11 KB
/
mian.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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/20ritiksingh/RSSagg/internal/database"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"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 not found in env")
}
dbURL := os.Getenv("DB_URL")
if dbURL == "" {
log.Fatal("port not found in env")
}
conn, err := sql.Open("postgres", dbURL)
if err != nil {
log.Fatal("db not connected", err)
}
db := database.New(conn)
apiCfg := apiConfig{
DB: db,
}
go startScraping(db, 10, 1)
r := chi.NewRouter()
r.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{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
v1Router := chi.NewRouter()
v1Router.Get("/heathz", handlerReadiness)
v1Router.Get("/err", 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.Get("/feed_follow", apiCfg.middlewareAuth(apiCfg.handlerGetFeedFollows))
v1Router.Post("/feed_follow", apiCfg.middlewareAuth(apiCfg.handlerCreateFeedFollow))
v1Router.Delete("/feed_follow/{feedFollowID}", apiCfg.middlewareAuth(apiCfg.handlerDeleteFeedFollow))
v1Router.Get("/posts", apiCfg.middlewareAuth(apiCfg.handlerGetPosts))
r.Mount("/v1", v1Router)
srv := &http.Server{
Handler: r,
Addr: ":" + port,
}
log.Println("server starting on ", port)
err = srv.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}