-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (53 loc) · 1.53 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
package main
import (
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
"github.com/riju-stone/go-rss/internal/database"
log "github.com/riju-stone/go-rss/logging"
"github.com/riju-stone/go-rss/routes/v1"
"github.com/riju-stone/go-rss/utils"
)
func main() {
// Load env variables
godotenv.Load(".env")
port := os.Getenv("PORT")
if port == "" {
log.Error("Could not locate PORT config in environment")
}
// Connect database
dbConn := utils.ConnectDB()
defer dbConn.Close()
// Initializing sqlc db conection
dbQueries := database.New(dbConn)
// Creating a new router
router := chi.NewRouter()
// Adding cors config to the global router
router.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"https://*", "http://*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"Link"},
AllowCredentials: false,
MaxAge: 300,
}))
// Adding custom logger as middleware
router.Use(log.LogMiddleware)
// Adding & Mounting Sub-routes
v1Router := routes.InitV1Routes(dbQueries)
router.Mount("/v1", v1Router)
log.Debug("V1 Routes Mounted")
// Initializing a new HTTP server using the declared router
server := &http.Server{
Handler: router,
Addr: ":" + port,
}
// Executing the server to listen to a particular port
log.Info("Server Listening on Port: %v", port)
err := server.ListenAndServe()
if err != nil {
log.Panic("Failed to initialize server: %v", err)
}
}