Skip to content

Commit 14facf1

Browse files
committed
Polish the lib api and add comments
1 parent 1cdbdbc commit 14facf1

1 file changed

Lines changed: 48 additions & 23 deletions

File tree

sqliteadmin.go

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ type Handler struct {
1515
db *sql.DB
1616
username string
1717
password string
18+
logger Logger
1819
}
1920

20-
type Query string
21+
type Command string
2122

2223
type Filter struct {
2324
Column string `json:"column"`
@@ -62,11 +63,11 @@ const (
6263
)
6364

6465
const (
65-
Ping Query = "Ping"
66-
ListTables Query = "ListTables"
67-
GetTable Query = "GetTable"
68-
DeleteRows Query = "DeleteRows"
69-
UpdateRow Query = "UpdateRow"
66+
Ping Command = "Ping"
67+
ListTables Command = "ListTables"
68+
GetTable Command = "GetTable"
69+
DeleteRows Command = "DeleteRows"
70+
UpdateRow Command = "UpdateRow"
7071
)
7172

7273
const pathPrefixPlaceholder = "%%__path_prefix__%%"
@@ -76,21 +77,47 @@ const DefaultOffset = 0
7677

7778
var indexHtml embed.FS
7879

79-
func NewHandler(db *sql.DB, username, password string) *Handler {
80+
type Logger interface {
81+
Info(format string, args ...interface{})
82+
Error(format string, args ...interface{})
83+
Debug(format string, args ...interface{})
84+
}
85+
86+
type LogLevel string
87+
88+
const (
89+
LogLevelInfo LogLevel = "info"
90+
LogLevelDebug LogLevel = "debug"
91+
)
92+
93+
type Config struct {
94+
Db *sql.DB
95+
Username string
96+
Password string
97+
Logger Logger
98+
}
99+
100+
// Creates a new HTTP handler which has a HandlePost method
101+
// that can be used to handle requests from https://sqliteadmin.dev.
102+
func NewHandler(c Config) *Handler {
80103
return &Handler{
81-
db: db,
82-
username: username,
83-
password: password,
104+
db: c.Db,
105+
username: c.Username,
106+
password: c.Password,
107+
logger: c.Logger,
84108
}
85109
}
86110

87-
type QueryRequest struct {
88-
Query Query `json:"query"`
89-
Params map[string]interface{} `json:"params"`
111+
type CommandRequest struct {
112+
Command Command `json:"command"`
113+
Params map[string]interface{} `json:"params"`
90114
}
91115

92-
func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
116+
// Handles the incoming HTTP POST request. This is responsible for handling
117+
// all the supported operations from https://sqliteadmin.dev
118+
func (h *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
93119
// Check for auth header that contains username and password
120+
w.Header().Set("Content-Type", "application/json")
94121
if h.username != "" && h.password != "" {
95122
authHeader := r.Header.Get("Authorization")
96123
if h.username+":"+h.password != authHeader {
@@ -99,33 +126,31 @@ func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) {
99126
}
100127
}
101128

102-
w.Header().Set("Content-Type", "application/json")
103-
// w.Header().Set("Access-Control-Allow-Origin", "*")
104-
var qr QueryRequest
105-
err := json.NewDecoder(r.Body).Decode(&qr)
129+
var cr CommandRequest
130+
err := json.NewDecoder(r.Body).Decode(&cr)
106131
if err != nil {
107132
w.WriteHeader(http.StatusBadRequest)
108133
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid Request Body"})
109134
return
110135
}
111136

112-
switch qr.Query {
137+
switch cr.Command {
113138
case Ping:
114139
h.ping(w)
115140
return
116141
case ListTables:
117142
h.listTables(w)
118143
return
119144
case GetTable:
120-
h.getTable(w, qr.Params)
145+
h.getTable(w, cr.Params)
121146
return
122147
case DeleteRows:
123-
h.deleteRows(w, qr.Params)
148+
h.deleteRows(w, cr.Params)
124149
return
125150
case UpdateRow:
126-
h.updateRow(w, qr.Params)
151+
h.updateRow(w, cr.Params)
127152
return
128153
default:
129-
http.Error(w, "Invalid query", http.StatusBadRequest)
154+
http.Error(w, "Invalid command", http.StatusBadRequest)
130155
}
131156
}

0 commit comments

Comments
 (0)