-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
69 lines (57 loc) · 1.48 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
// App defines our app
type app struct {
Router *mux.Router
DB *gorm.DB
}
//Initialize creates db connection and initialize mux router
func (a *app) Initialize(userr string, password string, dbname string) {
connectionString := fmt.Sprintf("%s:%s@/%s", userr, password, dbname)
var err error
a.DB, err = gorm.Open("mysql", connectionString)
// migrate db
migrateSchema(a)
if err != nil {
log.Fatal(err)
}
a.Router = mux.NewRouter()
a.initialiazeRoutes()
}
//Run starts the application
func (a *app) Run(addr string) {
// this will simply start thapplication
log.Printf("App running on:http://localhost:8080")
log.Fatal(http.ListenAndServe(addr, a.Router))
}
func respondWithError(w http.ResponseWriter, code int, err []string) {
response := ResponseBuilder{
Status: "failure",
StatusCode: code,
Errors: err,
}
responseWrite(w, response)
}
func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) {
response := ResponseBuilder{
Status: "success",
StatusCode: code,
Data: payload,
}
responseWrite(w, response)
}
func responseWrite(w http.ResponseWriter, response ResponseBuilder) {
jsonResponse, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(response.StatusCode)
w.Write(jsonResponse)
}