-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
84 lines (66 loc) · 2.27 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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/cors"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
"github.com/muhtutorials/rss/db"
)
type apiConfig struct {
DB *db.Queries
}
func main() {
godotenv.Load()
port := os.Getenv("PORT")
if port == "" {
log.Fatal("Port was not found in the environment")
}
dbURL := os.Getenv("DB_URL")
if dbURL == "" {
log.Fatal("DB URL was not found in the environment")
}
conn, err := sql.Open("mysql", dbURL)
if err != nil {
log.Fatal("Couldn't connect to the database")
}
dbConn := db.New(conn)
apiConfig := apiConfig{DB: dbConn}
go startScraping(dbConn, 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{"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("/healthz", handlerReadiness)
v1Router.Get("/error", handlerError)
v1Router.Post("/users", apiConfig.handlerCreateUser)
v1Router.Get("/users", apiConfig.middlewareAuth(apiConfig.handlerGetUser))
v1Router.Post("/feeds", apiConfig.middlewareAuth(apiConfig.handlerCreateFeed))
v1Router.Get("/feeds", apiConfig.handlerGetFeeds)
v1Router.Post("/feed-follows", apiConfig.middlewareAuth(apiConfig.handlerCreateFeedFollow))
v1Router.Get("/feed-follows", apiConfig.middlewareAuth(apiConfig.handlerGetUserFeedFollows))
v1Router.Delete("/feed-follows/{feedFollowID}", apiConfig.middlewareAuth(apiConfig.handlerDeleteFeedFollow))
v1Router.Post("/posts", apiConfig.middlewareAuth(apiConfig.handlerGetPostsForUser))
router.Mount("/v1", v1Router)
server := &http.Server{
Handler: router,
Addr: ":" + port,
}
log.Printf("Server is starting on port %v:", port)
err = server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}