generated from zerefwayne/go-psql-rest-docker-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
40 lines (27 loc) · 908 Bytes
/
server.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
package main
import (
"context"
"github.com/zerefwayne/go-psql-rest-jwt-docker-boilerplate/config"
"github.com/zerefwayne/go-psql-rest-jwt-docker-boilerplate/routes"
"log"
"net/http"
"github.com/rs/cors"
)
func main() {
log.Println("server | initializing")
// Database setup
config.ConnectDB()
// Close the database connection once the main function is finished
defer config.DB.Close(context.Background())
// Calls ping method
config.PingDB()
// Creates a new Mux Router
r := routes.NewRouter()
// This is used to remove CORS that arise when request comes from the same server's another port
handler := cors.AllowAll().Handler(r)
// http ListenAndServe is used to listen to requests on 5000 and redirecting them to handler
if err := http.ListenAndServe(":5000", handler); err != nil {
// In case there's an error, server is closed and error is logged.
log.Fatal(err)
}
}