-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
71 lines (58 loc) · 1.76 KB
/
app.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
66
67
68
69
70
71
// Package main consists of the application, namely the rest service
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
"log"
"net/http"
)
// Type Application comprises a reference to the router as well as the database that is used to persist data
type Application struct {
Router *mux.Router
DB *sql.DB
}
// Initialize the database connection
func (app *Application) Initialize(user, password, dbname string, port int) {
connectionString :=
fmt.Sprintf("port=%d user=%s password=%s dbname=%s sslmode=disable", port, user, password, dbname)
var err error
app.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
app.Router = mux.NewRouter()
app.initializeRoutes()
}
// Initialize the REST handler
func (app *Application) initializeRoutes() {
app.Router.HandleFunc("/footballer", app.getFootballers).Methods("GET")
}
// Run the application
func (app *Application) Run(addr string) {
log.Fatal(http.ListenAndServe(addr, app.Router))
}
func main() {
app := Application{}
app.Initialize("postgres", "admin", "postgres", 5432)
app.Run(":8090")
}
func (app *Application) getFootballers(writer http.ResponseWriter, reader *http.Request) {
var players, err = getFootballers(app.DB)
if err != nil {
respondWithError(writer, http.StatusInternalServerError, err.Error())
return
}
respondWithJSON(writer, http.StatusOK, players)
}
func respondWithError(w http.ResponseWriter, code int, message string) {
respondWithJSON(w, code, map[string]string{"error": message})
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}