-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
143 lines (126 loc) · 3.51 KB
/
main.go
File metadata and controls
143 lines (126 loc) · 3.51 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
func main() {
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
// Set a timeout value on the request context (ctx), that will signal
// through ctx.Done() that the request has timed out and further
// processing should be stopped.
r.Use(middleware.Timeout(60 * time.Second))
// no push notifications! that means that we need to either send sms or something of the sort to the user dabout the state of
// his request
// create new connection
conn, err := NewConnection()
if err != nil {
panic(err)
}
workDir, _ := os.Getwd()
filesDir := filepath.Join(workDir, "/frontend/dist")
FileServer(r, "/", http.Dir(filesDir))
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
sessionId := r.FormValue("sessionId")
phoneNumber := r.FormValue("phoneNumber")[1:]
networkCode := r.FormValue("networkCode")
text := r.FormValue("text")
// check if the user has a history, if not create one
_, err := conn.memory.Get(phoneNumber)
if err != nil {
// if error, create a new one
txHistory := TransactionHistory{}
conn.memory.Write(phoneNumber, txHistory)
}
fmt.Println(sessionId, phoneNumber, networkCode, text)
if text == "" {
w.Write([]byte(`CON
Welcome to crypto USSD Portal, what would you like to do?
1. Send Money
2. Withdraw
3. Buy airtime
4. Pay goods
5. Pay bill
6. Get a loan PRWD by MKR
7. My Account
`))
return
}
textArray := strings.Split(text, "*")
switch textArray[0] {
case "1":
// Send Money
resp, err := conn.SendMoney(textArray, sessionId, phoneNumber, networkCode, text)
if err != nil {
// handle it
w.Write([]byte(err.Error()))
}
w.Write([]byte(resp))
case "2":
// Withdraw Cash
resp, err := conn.Withdraw(textArray, sessionId, phoneNumber, networkCode, text)
if err != nil {
// handle it
w.Write([]byte(err.Error()))
}
w.Write([]byte(resp))
case "3":
w.Write([]byte("END Not Implemented, sorry!"))
case "4":
w.Write([]byte("END Not Implemented, sorry!"))
case "5":
w.Write([]byte("END Not Implemented, sorry!"))
case "6":
resp, err := conn.MakerDao(textArray, sessionId, phoneNumber, networkCode, text)
if err != nil {
// handle it
w.Write([]byte(err.Error()))
}
w.Write([]byte(resp))
case "7":
// My Account
resp, err := conn.MyAccount(textArray, sessionId, phoneNumber, networkCode, text)
if err != nil {
// handle it
w.Write([]byte(err.Error()))
}
w.Write([]byte(resp))
case "8":
w.Write([]byte("END Not Implemented, sorry!"))
default:
w.Write([]byte("END Not Implemented, sorry!"))
}
})
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
fmt.Println("Listening on port", port)
http.ListenAndServe(":"+port, r)
}
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}