-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (75 loc) · 2.43 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
package main
import (
"context"
"log"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/jackc/pgx/v5"
"github.com/joho/godotenv"
internal "github.com/sakthi-lucia0567/rssagg/internal/database"
_ "github.com/lib/pq"
)
type apiConfig struct {
DB *internal.Queries
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
port := os.Getenv("PORT")
if port == "" {
log.Fatal("Port is not fount in the environment")
}
dbUrl := os.Getenv("DB_URL")
if dbUrl == "" {
log.Fatal("DB_URL is not found in the environment")
}
// conn, err := sql.Open("postgres", dbUrl)
conn, err := pgx.Connect(context.Background(), dbUrl)
if err != nil {
log.Fatal("Can't connect to database:", err)
}
apiCfg := apiConfig{
DB: internal.New(conn),
}
router := chi.NewRouter()
server := &http.Server{
Handler: router,
Addr: ":" + port,
}
v1Router := chi.NewRouter()
v1Router.Get("/healthz", handleReadiness)
v1Router.Get("/err", handleError)
v1Router.Post("/users", apiCfg.handleCreateUser)
v1Router.Get("/users", apiCfg.middlewareAuth(apiCfg.handleGetUser))
v1Router.Post("/feed", apiCfg.middlewareAuth(apiCfg.handleCreateFeed))
v1Router.Get("/feed", apiCfg.handleGetFeeds)
v1Router.Post("/feed_follow", apiCfg.middlewareAuth(apiCfg.handleCreateFeedFollow))
v1Router.Get("/feed_follow", apiCfg.middlewareAuth(apiCfg.handleGetFeedFollow))
v1Router.Delete("/feed_follow/{feedFollowId}", apiCfg.middlewareAuth(apiCfg.handleDeleteFeedFollow))
router.Mount("/v1", v1Router)
log.Printf("Server starting on port %v", port)
error := server.ListenAndServe()
if err != nil {
log.Fatal(error)
}
router.Use(middleware.RouteHeaders().
Route("Origin", "https://app.skyweaver.net", cors.Handler(cors.Options{
AllowedOrigins: []string{"https://api.skyweaver.net", "http://*", "https://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: false, // <----------<<< allow credentials
MaxAge: 300,
})).
Route("Origin", "*", cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Content-Type"},
AllowCredentials: false, // <----------<<< do not allow credentials
})).
Handler)
}