-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (85 loc) · 2.17 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"log"
"net/http"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"github.com/mspiewak/renter/db"
)
// App keeps application dependencies
type App struct {
DB *sqlx.DB
billRepository *db.BillRepository
tenantRepository *db.TenantRepository
paymentRepository *db.PaymentRepository
}
func main() {
var app App
for {
log.Println("connecting")
if err := app.Initialize(); err != nil {
log.Println(err)
time.Sleep(time.Second * 10)
continue
}
break
}
defer app.DB.Close()
r := mux.NewRouter()
r.Handle("/tenant", errorHandler(app.getTenantsHandler)).Methods(http.MethodGet)
r.Handle("/tenant/{id:[0-9a-z]+}/bill", errorHandler(app.getTenantBills)).Methods(http.MethodGet)
r.Handle("/tenant/{id:[0-9a-z]+}/payment", errorHandler(app.getTenantPayments)).Methods(http.MethodGet)
r.Handle("/bill", errorHandler(app.getBills)).Methods(http.MethodGet)
r.Handle("/bill", errorHandler(app.postBill)).Methods(http.MethodPost)
r.Handle("/bill", errorHandler(app.optionsBills)).Methods(http.MethodOptions)
r.Handle("/stats/income", errorHandler(app.getIncome)).Methods(http.MethodGet)
r.Handle("/cron/rent", errorHandler(app.postRent)).Methods(http.MethodPost)
log.Println("listening")
log.Fatal(http.ListenAndServe(":8090", commonHeaders(r)))
}
// Initialize the application
func (a *App) Initialize() error {
dbc, err := sqlx.Connect("mysql", "root:Jg2FXug3rg@tcp(db:3306)/renter?parseTime=true")
if err != nil {
return fmt.Errorf("cannot connect to db server: %v", err)
}
a.DB = dbc
a.billRepository = db.NewBillRepository(dbc)
a.tenantRepository = db.NewTenantRepository(dbc)
a.paymentRepository = db.NewPaymentRepository(dbc)
return nil
}
func getRealTenantID(hash string) int {
switch hash {
case "i4lrehq":
return 1
case "ggl0qk8":
return 2
case "4duspw0":
return 3
case "wm4yk48":
return 4
case "j2i9kzr":
return 5
case "hfb8yf5":
return 6
case "jaf8eg3":
return 7
case "kds9bd7":
return 8
case "bgx3n81":
return 9
case "pw8bvo9":
return 10
case "kc8dn4j":
return 11
case "fgb2g6":
return 12
case "qfd9d7":
return 13
}
return 0
}