-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (48 loc) · 1.51 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
package main
import (
"fmt"
"github.com/TRileySchwarz/go-database/db"
"github.com/TRileySchwarz/go-database/routes"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"net/http"
"os"
)
func main() {
// Load the environment variables, this is where things like api keys should be stored.
// Can also store constants shared by multiple services
err := godotenv.Load()
if err != nil {
panic(errors.Wrap(err, "Could not load .env file"))
}
apiPort := os.Getenv("API_PORT")
// Initialize the database and create corresponding rows
err = db.InitDatabase()
if err != nil {
panic(errors.Wrap(err, "There was an error initializing the database connection"))
}
fmt.Println("\nConnection to the database successful")
// Defer closing the database when the program exits
defer func() {
err = db.DataBase.Close()
if err != nil {
fmt.Printf("\nError closing the db connection: %v", err)
}
fmt.Println("The database connection has been closed")
}()
// Initialize the API and prepare to handle requests
fmt.Println("Starting the API Service on port: " + apiPort)
err = http.ListenAndServe(":"+apiPort, handler())
if err != nil {
panic(errors.Wrap(err, "Could not listen and serve on port: "+apiPort))
}
}
// http handler we will be using to route API calls
func handler() http.Handler {
r := http.NewServeMux()
// Assign the router handlers for this particular API
r.HandleFunc("/signup", routes.HandleSignUp)
r.HandleFunc("/login", routes.HandleLogin)
r.HandleFunc("/users", routes.HandleUsers)
return r
}