-
Notifications
You must be signed in to change notification settings - Fork 117
/
config.example.hcl
144 lines (125 loc) · 3.38 KB
/
config.example.hcl
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
// create a macro/endpoint called "_boot",
// this macro is private "used within other macros"
// because it starts with "_".
_boot {
// the query we want to execute
exec = <<SQL
CREATE TABLE IF NOT EXISTS datax (
ID INT PRIMARY KEY,
data TEXT DEFAULT NULL
);
SQL
}
addpost {
include = ["_boot"]
methods = ["POST"]
// validators {
// title_is_empty = "$input.title && $input.title.trim().length > 0"
// content_is_empty = "$input.content"
// }
bind {
data = <<JS
JSON.stringify({
"title": $input.title,
"content": $input.content
})
JS
}
exec = <<SQL
INSERT INTO datax(ID, data) VALUES(default, :data) RETURNING id, data;
SQL
}
// adduser macro/endpoint, just hit `/adduser` with
// a `?user_name=&user_email=` or json `POST` request
// with the same fields.
adduser {
validators {
user_name_is_empty = "$input.user_name && $input.user_name.trim().length > 0"
user_email_is_empty = "$input.user_email && $input.user_email.trim().length > 0"
user_password_is_not_ok = "$input.user_password && $input.user_password.trim().length > 5"
}
bind {
name = "$input.user_name"
email = "$input.user_email"
password = "$input.user_password"
}
methods = ["POST"]
authorizer = <<JS
(function(){
log("use this for debugging")
token = $input.http_authorization
response = fetch("http://requestbin.fullcontact.com/zxpjigzx", {
headers: {
"Authorization": token
}
})
if ( response.statusCode != 200 ) {
return false
}
return true
})()
JS
// include some macros we declared before
include = ["_boot"]
exec = <<SQL
INSERT INTO users(name, email, password, time) VALUES(:name, :email, :password, UNIX_TIMESTAMP());
SELECT * FROM users WHERE id = LAST_INSERT_ID();
SQL
}
// list all databases, and run a transformer function
databases {
// include = ["_boot"]
exec = "SHOW DATABASES"
transformer = <<JS
(function(){
// $result
$new = [];
for ( i in $result ) {
$new.push($result[i].Database)
}
return $new
})()
JS
}
// list all tables from all databases
tables {
exec = "SELECT `table_name` as `table`, `table_schema` as `database` FROM INFORMATION_SCHEMA.tables"
transformer = <<SQL
(function(){
$ret = []
for ( i in $result ){
$ret.push({
table: $result[i].table,
database: $result[i].database,
})
}
return $ret
})()
SQL
}
data {
bind {
limit = 2
field = "'id'"
}
exec = "SELECT id FROM data limit 5"
}
// a macro that aggregates `databases` macro and `tables` macro into one macro
databases_tables {
aggregate = ["databases", "tables"]
}
_sqlite_tables {
exec = <<SQL
SELECT
name
FROM
sqlite_master
WHERE
type ='table' AND
name NOT LIKE 'sqlite_%';
SQL
cron = "* * * * *"
trigger {
webhook = "https://en09y7gttbxyos.x.pipedream.net"
}
}