-
Notifications
You must be signed in to change notification settings - Fork 0
/
appModel.go
85 lines (65 loc) · 1.58 KB
/
appModel.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package models
import (
"database/sql"
"fmt"
"log"
"net/http"
_ "github.com/lib/pq"
)
type App struct {
DB *sql.DB
Router *http.Server
}
var (
host = "localhost"
port = 5433
user = "postgres"
password = "password"
)
func (app *App) Initialize() error {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s sslmode=disable",
host, port, user, password)
var err error
app.DB, err = sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
err = app.DB.Ping()
if err != nil {
panic(err)
}
fmt.Println("DB CONNECTED")
return err
}
func (app *App) CheckDB() bool {
var (
err error
exists bool
)
err = app.DB.QueryRow("SELECT EXISTS(SELECT * FROM information_schema.tables WHERE table_schema = $1 AND table_name = $2);", "public", "words").Scan(&exists)
if err != nil {
log.Fatal(err)
}
return exists
}
// func (app *App) Run(port string) {
// log.Println("Server listening on port", port)
// log.Fatal(http.ListenAndServe(port, app.Router))
// }
// 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)
// }
// func createRouter(app *App) *mux.Router {
// r := mux.NewRouter()
// r.HandleFunc("/", healthCheck)
// http.Handle("/", r)
// r.HandleFunc("/api/words", app.getWords).Methods("GET")
// return r
// }