-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathido.go
195 lines (157 loc) · 4.14 KB
/
ido.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*interkonekto serves a RESTful API to define and translate words in the Ido language. */
package main
import (
"database/sql"
"encoding/json"
"log"
"net/http"
"os"
"strings"
_ "github.com/lib/pq"
)
var db *sql.DB
var datumi []string
func main() {
db = DB()
defer db.Close()
// http.HandleFunc("/favicon.ico", nil)
http.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func DB() *sql.DB {
connStr := "user=postgres password=" + os.Getenv("DB_PASSWORD") + " host=35.226.129.4 dbname=postgres"
db, err := sql.Open("postgres", connStr)
panicOnErr(err)
rows, err := db.Query("SELECT * FROM ido WHERE io = 'lingu.o'")
panicOnErr(err)
datumi, err = rows.Columns()
panicOnErr(err)
return db
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, PUT, OPTIONS")
w.Header().Set("Access-Control-Max-Age", "86400")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
rendimento := json.NewEncoder(w)
path := strings.Split(r.URL.Path, "/") // trouble characters: %#
switch path[1] {
case "":
rendimento.Encode("https://gitlab.com/hall/interkonekto")
return
case "*":
rendimento.Encode(datumi)
return
}
demando := path[1]
var datumo string
var linguo string
splitQuery := strings.Split(demando, ":")
if len(splitQuery) == 1 {
linguo = "io" // search with Ido by default
} else {
linguo = splitQuery[0]
demando = splitQuery[1]
}
if !validColumn(linguo, datumi) {
w.WriteHeader(http.StatusBadRequest) //400
return
}
// return all data (for matching queries) if no specific set is requested
if len(path) < 3 || path[2] == "" {
datumo = "*"
} else {
datumo = path[2]
}
if !validColumn(datumo, datumi) {
w.WriteHeader(http.StatusBadRequest) //400
return
}
switch r.Method {
case "GET":
rows, err := db.Query("SELECT "+datumo+" FROM ido WHERE replace("+linguo+",'.','') = ?", demando)
panicOnErr(err)
defer rows.Close()
cols, err := rows.Columns()
panicOnErr(err)
colNum := len(cols)
dest := make([]interface{}, colNum)
raw := make([][]byte, colNum)
result := make(map[string]interface{}, colNum)
for i := range raw {
dest[i] = &raw[i]
}
for rows.Next() {
err = rows.Scan(dest...)
panicOnErr(err)
for i, r := range raw {
if r == nil {
result[cols[i]] = ""
} else {
entries := strings.Split(string(r), "\n")
if len(entries) > 1 {
result[cols[i]] = strings.Split(string(r), "\n")
} else {
result[cols[i]] = string(r)
}
}
}
master := make(map[string]map[string]interface{}, colNum)
master[demando] = result
rendimento.Encode(master)
}
err = rows.Err()
panicOnErr(err)
//w.WriteHeader(http.StatusOK) //200
//w.WriteHeader(http.StatusNotFound) //404
//w.WriteHeader(http.StatusBadRequest) //400
case "PUT":
if datumo == "*" {
return
}
err := r.ParseForm()
panicOnErr(err)
//nova := r.PostFormValue("nova")
//if (len(nova) < 1) { return }
//statement, err := db.Prepare("UPDATE ido SET "+data+" = ?")
//if err != nil {
//log.Fatal(err)
//}
//statement.Exec(nova)
// rendimento.Encode("UPDATE ido SET '" + datumo + "' = '" + nova + "' WHERE io = " + demando + ";")
case "POST":
//statement, _ := db.Prepare("UPDATE ido SET en='?' WHERE io='?'")
//statement.Exec("en", query)
case "DELETE": //delete
//statement, _ := db.Prepare("DELETE FROM ido WHERE io='?'")
//statement.Exec("en", query)
//default:
//json.NewEncoder(w).Encode(query)
//json.NewEncoder(w).Encode(request)
//json.NewEncoder(w).Encode(result)
// log.Fatal("unknown request: ", request)
}
}
// validColumn checks if column requests are valid (to prevent SQL injections).
func validColumn(datumo string, datumi []string) bool {
var found bool
for _, c := range strings.Split(datumo, ",") {
found = false
for _, b := range datumi {
if b == c || c == "*" {
found = true
continue
}
}
if !found {
return false
}
}
return true
}
func panicOnErr(err error) {
if err != nil {
panic(err.Error())
}
}