-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
162 lines (140 loc) · 3.23 KB
/
Copy pathserver.go
File metadata and controls
162 lines (140 loc) · 3.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"database/sql"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
)
type QuotationResult struct {
Bid string `json:"bid"`
}
type Quotation struct {
Usdbrl QuotationResult `json:"usdbrl"`
}
func main() {
http.HandleFunc("/quotation", quotationHandler)
http.ListenAndServe(":8080", nil)
}
func quotationHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
if r.URL.Path != "/quotation" {
w.WriteHeader(http.StatusNotFound)
return
}
srcParam := r.URL.Query().Get("src")
dstParam := r.URL.Query().Get("dst")
if srcParam == "" || dstParam == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
quotation, err := requestQuotation(srcParam, dstParam)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(quotation.Usdbrl)
}
func requestQuotation(currencySrc, currencyDst string) (*Quotation, error) {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://economia.awesomeapi.com.br/json/last/"+currencySrc+"-"+currencyDst, nil)
if err != nil {
return nil, err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, error := ioutil.ReadAll(res.Body)
if error != nil {
return nil, error
}
var q Quotation
error = json.Unmarshal(body, &q)
if error != nil {
return nil, error
}
err = saveToDataBase(context.Background(), &q)
if error != nil {
return nil, error
}
return &q, nil
}
func saveToDataBase(ctx context.Context, quotation *Quotation) error {
os.Remove("sqlite-database.db")
file, err := os.Create("sqlite-database.db")
if err != nil {
panic(err)
}
file.Close()
db, err := sql.Open("sqlite3", "./sqlite-database.db")
if err != nil {
panic(err)
}
defer db.Close()
createTable(db)
select {
case <-time.After(10 * time.Millisecond):
err = insertQuotation(db, quotation.Usdbrl.Bid)
if err != nil {
return err
}
log.Println("Insert Quotation successfully executed")
case <-ctx.Done():
log.Println("Failed to insertation Quotation")
}
displayQuotation(db)
return nil
}
func createTable(db *sql.DB) {
createTable := `CREATE TABLE quotation (
"id" TEXT NOT NULL PRIMARY KEY,
"code" TEXT,
"codein" TEXT,
"bid" TEXT
);`
stmt, err := db.Prepare(createTable)
if err != nil {
panic(err)
}
stmt.Exec()
}
func insertQuotation(db *sql.DB, bid string) error {
stmt, err := db.Prepare(`INSERT INTO quotation(id, code, codein, bid) values(?, ?, ?, ?)`)
if err != nil {
panic(err)
}
defer stmt.Close()
_, err = stmt.Exec(uuid.New(), "USB", "BRL", bid)
if err != nil {
return err
}
return nil
}
func displayQuotation(db *sql.DB) {
row, err := db.Query("SELECT * FROM quotation ORDER BY bid")
if err != nil {
panic(err)
}
defer row.Close()
for row.Next() {
var id string
var code string
var codein string
var bid string
row.Scan(&id, &code, &codein, &bid)
log.Println(id, " ", code, " ", codein, " ", bid)
}
}