-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsqliteadmin.go
More file actions
173 lines (141 loc) · 3.76 KB
/
sqliteadmin.go
File metadata and controls
173 lines (141 loc) · 3.76 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
163
164
165
166
167
168
169
170
171
172
173
// Package sqliteadmin allows you to view and manage your SQLite database by
// exposing an HTTP handler that you can easily integrate into any Go web
// framework.
package sqliteadmin
import (
"database/sql"
"encoding/json"
"net/http"
)
type Admin struct {
db *sql.DB
username string
password string
logger Logger
}
type Command string
type Filter struct {
Column string `json:"column"`
Operator Operator `json:"operator"`
Value string `json:"value"`
}
type Condition struct {
Cases []Case `json:"cases" mapstructure:"cases"`
LogicalOperator LogicalOperator `json:"logicalOperator" mapstructure:"logicalOperator"`
}
type Case interface {
ConditionCaseType() string
}
func (c Condition) ConditionCaseType() string {
return "condition"
}
func (f Filter) ConditionCaseType() string {
return "filter"
}
type LogicalOperator string
const (
LogicalOperatorAnd LogicalOperator = "and"
LogicalOperatorOr LogicalOperator = "or"
)
type Operator string
const (
OperatorEquals Operator = "eq"
OperatorLike Operator = "like"
OperatorNotEquals Operator = "neq"
OperatorLessThan Operator = "lt"
OperatorLessThanOrEquals Operator = "lte"
OperatorGreaterThan Operator = "gt"
OperatorGreaterThanOrEquals Operator = "gte"
OperatorIsNull Operator = "null"
OperatorIsNotNull Operator = "notnull"
)
const (
Ping Command = "Ping"
ListTables Command = "ListTables"
GetTable Command = "GetTable"
DeleteRows Command = "DeleteRows"
UpdateRow Command = "UpdateRow"
)
const pathPrefixPlaceholder = "%%__path_prefix__%%"
const (
DefaultLimit = 100
DefaultOffset = 0
)
type Logger interface {
Info(format string, args ...interface{})
Error(format string, args ...interface{})
Debug(format string, args ...interface{})
}
type LogLevel string
const (
LogLevelInfo LogLevel = "info"
LogLevelDebug LogLevel = "debug"
)
type Config struct {
DB *sql.DB
Username string
Password string
Logger Logger
}
// Returns a *Admin which has a HandlePost method that can be used to handle
// requests from https://sqliteadmin.dev.
func New(c Config) *Admin {
h := &Admin{
db: c.DB,
username: c.Username,
password: c.Password,
logger: c.Logger,
}
if h.logger == nil {
h.logger = &defaultLogger{}
}
return h
}
type CommandRequest struct {
Command Command `json:"command"`
Params map[string]interface{} `json:"params"`
}
// Handles the incoming HTTP POST request. This is responsible for handling
// all the supported operations from https://sqliteadmin.dev
func (a *Admin) HandlePost(w http.ResponseWriter, r *http.Request) {
// Check for auth header that contains username and password
w.Header().Set("Content-Type", "application/json")
if a.username != "" && a.password != "" {
authHeader := r.Header.Get("Authorization")
if a.username+":"+a.password != authHeader {
writeError(w, apiErrUnauthorized())
return
}
}
var cr CommandRequest
err := json.NewDecoder(r.Body).Decode(&cr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid Request Body"})
return
}
switch cr.Command {
case Ping:
a.ping(w)
return
case ListTables:
a.listTables(w)
return
case GetTable:
a.getTable(w, cr.Params)
return
case DeleteRows:
a.deleteRows(w, cr.Params)
return
case UpdateRow:
a.updateRow(w, cr.Params)
return
default:
http.Error(w, "Invalid command", http.StatusBadRequest)
}
}
var _ Logger = &defaultLogger{}
type defaultLogger struct{}
func (l *defaultLogger) Info(format string, args ...interface{}) {}
func (l *defaultLogger) Error(format string, args ...interface{}) {}
func (l *defaultLogger) Debug(format string, args ...interface{}) {}