-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (43 loc) · 958 Bytes
/
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
package main
import (
"Chi-hero-API-with-GOPG/db"
"Chi-hero-API-with-GOPG/routes"
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-pg/pg/v10"
"log"
"net/http"
"os"
)
func main() {
pgdb, err := StartDB()
router := SetupRoutes(pgdb)
port := FetchPortNumber()
StartHTTPServer(err, port, router)
}
func StartDB() (*pg.DB, error) {
pgdb, err := db.StartDB()
if err != nil {
log.Printf("error: %v", err)
panic("error starting the database")
}
return pgdb, err
}
func SetupRoutes(pgdb *pg.DB) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.Logger, middleware.WithValue("DB", pgdb))
routes.SetupHeroesRoute(r)
return r
}
func FetchPortNumber() string {
port := os.Getenv("PORT")
return port
}
func StartHTTPServer(err error, port string, router *chi.Mux) {
err = http.ListenAndServe(fmt.Sprintf(":%s", port), router)
if err != nil {
log.Printf("error from router %v\n", err)
return
}
}