-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (111 loc) · 2.65 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
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
package main
import (
"database/sql"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
)
var (
flagDriver = flag.String("driver", "mysql", "driver to use")
databases = map[string]*DbInfo{}
)
type DbInfo struct {
Db *sql.DB
User string
Password string
}
type QueryResult struct {
Columns []string `json:"columns,omitempty"`
Data [][]*string `json:"data,omitempty"`
Infos map[string]int64 `json:"infos,omitempty"`
Error string `json:"error,omitempty"`
}
func WriteToJSON(w http.ResponseWriter, s int, v interface{}) error {
d, e := json.Marshal(v)
if e != nil {
return e
}
log.Printf("%s", d)
w.Header().Set("content-type", "application/json; charset=utf-8")
w.WriteHeader(s)
_, e = w.Write(d)
if e != nil {
return e
}
return nil
}
func open(dbn string, info *DbInfo) (*sql.DB, int, error) {
for k, v := range databases {
if k == dbn && v.User == info.User && v.Password == info.Password {
log.Printf("Used cache for database %q", dbn)
return v.Db, http.StatusOK, nil
}
}
db, e := sql.Open(*flagDriver, info.User+":"+info.Password+"@/"+dbn)
info.Db = db
if e != nil {
return nil, http.StatusInternalServerError, e
}
e = info.Db.Ping()
if e != nil {
return nil, http.StatusBadRequest, e
}
databases[dbn] = info
log.Printf("Stored database %q", dbn)
return db, http.StatusOK, nil
}
func HandlePing(w http.ResponseWriter, r *http.Request, info *DbInfo) {
n := time.Now()
dbn := mux.Vars(r)["db"]
log.Printf("[DATABASE] %q\n", dbn)
db, s, e := open(dbn, info)
if e != nil {
log.Println(e)
w.WriteHeader(s)
fmt.Fprintln(w, e)
return
}
e = db.Ping()
if e != nil {
log.Println(e)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, e)
return
}
fmt.Fprintf(w, "ping achieved")
log.Printf("(Ping done in %v)\n", time.Now().Sub(n))
}
func auth(fn func(http.ResponseWriter, *http.Request, *DbInfo)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok {
log.Println("Bad User:Password")
e := WriteToJSON(w, http.StatusUnauthorized, QueryResult{
Error: "Bad user password",
})
if e != nil {
log.Println(e)
}
return
}
info := DbInfo{
User: u,
Password: p,
}
fn(w, r, &info)
}
}
func main() {
flag.Parse()
databases = make(map[string]*DbInfo)
m := mux.NewRouter()
m.HandleFunc("/query/{db}/{query}", auth(HandleQuery)).Methods("POST")
m.HandleFunc("/exec/{db}/{query}", auth(HandleExec)).Methods("POST")
m.HandleFunc("/ping/{db}", auth(HandlePing)).Methods("POST")
log.Fatal(http.ListenAndServe(":6033", m))
}