-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
76 lines (61 loc) · 1.89 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
72
73
74
75
76
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"net/http"
"os"
_ "github.com/mattn/go-sqlite3"
)
// Application is the base struct for the whole web API service.
//
// All the API endpoints are attached to this struct as methods, this allows us
// to reference additional resources like the database, cache layer (if there
// is one), and other middlewares.
type Application struct {
// db is the connection with the SQLite database.
db *sql.DB
// host is the name or IP address for the web server.
host string
// port is the port number where the web server is running.
port string
// database is the full path to the SQLite database file.
database string
// migration is an SQL file to initialize the database.
migration string
}
// Response represents the data to respond to XHR requests.
type Response struct {
Ok bool `json:"ok"`
Error string `json:"error,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// Initialize creates the database tables and inserts initial data.
func (app *Application) Initialize() {
var err error
var db *sql.DB
if db, err = sql.Open("sqlite3", app.database); err != nil {
router.Logger.Println("sql.Open", err)
os.Exit(1)
}
app.db = db
// load SQL migration file, if available.
if out, err2 := ioutil.ReadFile(app.migration); err2 == nil {
if _, err = db.Exec(string(out)); err != nil {
router.Logger.Println("db.Init", err)
os.Exit(1)
}
}
}
// write writes a JSON encoded object with a successful message.
func write(w http.ResponseWriter, r *http.Request, v interface{}) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(v); err != nil {
router.Logger.Println("json.Encode", err)
return
}
}
// fail writes a JSON encoded object with an error message.
func fail(w http.ResponseWriter, r *http.Request, err error) {
write(w, r, Response{Error: err.Error()})
}