src/run.go (view raw)
1package src
2
3import (
4 "log"
5 "net/http"
6 "os"
7
8 "github.com/birabittoh/go-lift/src/api"
9 "github.com/birabittoh/go-lift/src/database"
10 "github.com/joho/godotenv"
11)
12
13func getEnv(key, def string) string {
14 if value, exists := os.LookupEnv(key); exists {
15 return value
16 }
17 return def
18}
19
20func Run() (err error) {
21 godotenv.Load()
22
23 db, err := database.InitializeDB()
24 if err != nil {
25 return
26 }
27
28 listenAddress := getEnv("APP_LISTEN_ADDRESS", ":3000")
29
30 mux := api.GetServeMux(db)
31
32 log.Println("Example running at", listenAddress)
33 err = http.ListenAndServe(listenAddress, mux)
34 return
35}