-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (68 loc) · 2.28 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
"github.com/itishrishikesh/gofeed/controller"
"github.com/itishrishikesh/gofeed/internal/database"
"github.com/itishrishikesh/gofeed/middleware"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
godotenv.Load()
portNumber := os.Getenv("PORT")
if portNumber == "" {
log.Fatal("E#1OLYW6 - PORT is not found in the env variables")
}
dbUrl := os.Getenv("GO_FEED_DB_URL")
if dbUrl == "" {
log.Fatal("E#1ORGYA - DB_URL is not found in the env variables")
}
conn, err := sql.Open("postgres", dbUrl)
if err != nil {
log.Fatal("E#1ORHEU - Failed to open a connection to db")
}
db := database.New(conn)
apiCfg := middleware.ApiConfig{
DB: db,
}
// go utils.StartScraping(db, 10, time.Minute)
router := chi.NewRouter()
log.Println("I#1OLYVV - Server starting on port number", portNumber)
router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
config := controller.ApiConfig{
DB: db,
}
v1Router := chi.NewRouter()
v1Router.Get("/healthz", controller.HealthCheckHandler)
v1Router.Get("/err", controller.ErrorHandler)
v1Router.Post("/users", config.CreateUserHandler)
v1Router.Get("/users", apiCfg.AuthMiddleware(config.GetUserHandler))
v1Router.Post("/feeds", apiCfg.AuthMiddleware(config.CreateFeedHandler))
v1Router.Get("/feeds", config.GetFeedsHandler)
v1Router.Post("/feedfollow", apiCfg.AuthMiddleware(config.CreateFeedFollowHandler))
v1Router.Get("/feedfollow", apiCfg.AuthMiddleware(config.GetFeedFollowsHandler))
v1Router.Delete("/feedfollow/{feedFollowId}", apiCfg.AuthMiddleware(config.DeleteFeedHandler))
v1Router.Get("/posts", apiCfg.AuthMiddleware(config.GetPostsForUserHandler))
v1Router.Post("/token", config.TokenHandler)
router.Mount("/v1", v1Router)
server := &http.Server{
Handler: router,
Addr: ":" + portNumber,
}
err = server.ListenAndServe()
if err != nil {
log.Fatalln("E#1OLYWJ - Server failed to launch. E:", err)
}
}