Skip to content

Commit 013f333

Browse files
committed
change:slogen
1 parent 526fb43 commit 013f333

File tree

8 files changed

+48
-100
lines changed

8 files changed

+48
-100
lines changed

app.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ end)
113113
router:get("/func", function(ctx)
114114
return function()
115115
ngx.header.content_type = 'text/plain; charset=utf-8'
116-
ngx.say("function called2")
116+
ngx.print("function called")
117117
end
118118
end)
119119

@@ -127,4 +127,9 @@ router:get("/events", function(ctx)
127127
return ctx.cnt
128128
end)
129129

130+
-- 12. ctx.response.body
131+
router:get("/response-body", function(ctx)
132+
ctx.response.body = "response body"
133+
end)
134+
130135
return router

index.ts

Lines changed: 9 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -73,73 +73,6 @@ function emptyDir(dir) {
7373
)
7474
}
7575

76-
const LINE =
77-
/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm
78-
79-
// Parser src into an Object
80-
function parseEnv(src) {
81-
const obj = {}
82-
83-
// Convert buffer to string
84-
let lines = src.toString()
85-
86-
// Convert line breaks to same format
87-
lines = lines.replace(/\r\n?/gm, '\n')
88-
89-
let match
90-
while ((match = LINE.exec(lines)) != null) {
91-
const key = match[1]
92-
93-
// Default undefined or null to empty string
94-
let value = match[2] || ''
95-
96-
// Remove whitespace
97-
value = value.trim()
98-
99-
// Check if double quoted
100-
const maybeQuote = value[0]
101-
102-
// Remove surrounding quotes
103-
value = value.replace(/^(['"`])([\s\S]*)\1$/gm, '$2')
104-
105-
// Expand newlines if double quoted
106-
if (maybeQuote === '"') {
107-
value = value.replace(/\\n/g, '\n')
108-
value = value.replace(/\\r/g, '\r')
109-
}
110-
111-
// Add to object
112-
obj[key] = value
113-
}
114-
115-
return obj
116-
}
117-
const envContext = parseEnv(
118-
fs.readFileSync(path.resolve(__dirname, '.env'), { encoding: 'utf8' })
119-
)
120-
121-
const normalizeArgv = (argv) => {
122-
const d = {}
123-
for (const key in argv) {
124-
d[key.replace(/-/g, '_').toUpperCase()] = argv[key]
125-
}
126-
if (d.PROD) {
127-
d.NODE_ENV = 'production'
128-
} else {
129-
d.NODE_ENV = 'development'
130-
}
131-
d.HTTPS = d.HTTPS ?? true
132-
if (d.HTTPS) {
133-
d.VITE_HTTPS = 'on'
134-
}
135-
if (d.HOST) {
136-
d.VITE_HOST = d.HOST
137-
} else {
138-
d.HOST = envContext.VITE_HOST
139-
d.VITE_HOST = d.HOST
140-
}
141-
return d
142-
}
14376

14477
async function init() {
14578
console.log()
@@ -169,7 +102,7 @@ async function init() {
169102
// all arguments are treated as booleans
170103
boolean: true
171104
})
172-
const normalizedArgv = normalizeArgv(argv)
105+
const normalizedArgv = argv
173106

174107
// if any of the feature flags is set, we would skip the feature prompts
175108
const isFeatureFlagsUsed =
@@ -309,7 +242,6 @@ async function init() {
309242
render('.')
310243

311244
// add dynamic scritps block
312-
const HOST = normalizedArgv.HOST
313245
const packageJsonPath = path.resolve(root, 'package.json')
314246
const existingPkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
315247
const updatedPkg = sortDependencies(
@@ -320,7 +252,7 @@ async function init() {
320252
}
321253
})
322254
)
323-
console.log({ argv, normalizedArgv, targetDir, projectName })
255+
// console.log({ argv, normalizedArgv, targetDir, projectName })
324256
fs.writeFileSync(packageJsonPath, JSON.stringify(updatedPkg, null, 2) + '\n', 'utf-8')
325257

326258
// An external data store for callbacks to share data
@@ -417,20 +349,22 @@ async function init() {
417349
})
418350
)
419351

420-
console.log(`\nDone. Now run:\n`)
352+
console.log(`\nDone. to start the server, run:\n`)
421353
if (root !== cwd) {
422354
const cdProjectName = path.relative(cwd, root)
423355
console.log(
424356
` ${bold(green(`cd ${cdProjectName.includes(' ') ? `"${cdProjectName}"` : cdProjectName}`))}`
425357
)
426358
}
427-
console.log(` ${bold(green(getCommand(packageManager, 'init-github')))}`)
428-
console.log(` ${bold(green(getCommand(packageManager, 'install')))}`)
359+
// console.log(` ${bold(green(getCommand(packageManager, 'init-github')))}`)
360+
// console.log(` ${bold(green(getCommand(packageManager, 'install')))}`)
429361
if (needsPrettier) {
430-
console.log(` ${bold(green(getCommand(packageManager, 'format')))}`)
362+
// console.log(` ${bold(green(getCommand(packageManager, 'format')))}`)
431363
}
432-
console.log(` ${bold(green(getCommand(packageManager, 'dev')))}`)
364+
console.log(` ${bold(green(getCommand(packageManager, 'start')))}`)
433365
console.log()
366+
console.log(`to stop the server, run:\n`)
367+
console.log(` ${bold(green(getCommand(packageManager, 'stop')))}`)
434368
}
435369

436370
init().catch((e) => {

lib/resty/router.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,11 @@ function Router:dispatch(path, method)
429429
return self:fail(ctx, err_or_okcode, errcode)
430430
elseif rawget(ctx, 'response') then
431431
local code = ctx.response.status or 200
432-
return self:echo(ctx, ctx.response.body, code)
432+
if ctx.response.body then
433+
return self:echo(ctx, ctx.response.body, code)
434+
else
435+
return self:echo(ctx, 'no response', 500)
436+
end
433437
else
434438
return self:echo(ctx, 'no response', 500)
435439
end

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-lua-resty-router-tmp",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "high performance router",
55
"type": "module",
66
"bin": {

template/lib/resty/router.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,11 @@ function Router:dispatch(path, method)
429429
return self:fail(ctx, err_or_okcode, errcode)
430430
elseif rawget(ctx, 'response') then
431431
local code = ctx.response.status or 200
432-
return self:echo(ctx, ctx.response.body, code)
432+
if ctx.response.body then
433+
return self:echo(ctx, ctx.response.body, code)
434+
else
435+
return self:echo(ctx, 'no response', 500)
436+
end
433437
else
434438
return self:echo(ctx, 'no response', 500)
435439
end

test.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_endpoint(
2525
expected_code: int = 200,
2626
expected_response: Optional[Union[str, dict, list]] = None,
2727
expected_content_type: Optional[str] = None,
28+
expected_substring: Optional[str] = None,
2829
) -> None:
2930
"""
3031
Test a single endpoint
@@ -36,6 +37,7 @@ def test_endpoint(
3637
expected_response: Expected response content. Can be:
3738
- string: for substring matching
3839
- dict/list: for complete JSON matching
40+
expected_substring: Expected substring in response content (optional)
3941
expected_content_type: Expected Content-Type (optional)
4042
"""
4143
test_stats['total'] += 1
@@ -92,13 +94,13 @@ def test_endpoint(
9294
else:
9395
actual_text = response.text
9496
if isinstance(expected_response, str):
95-
if expected_response in actual_text:
97+
if expected_response == actual_text:
9698
print(f"{GREEN}✓ Test passed{NC}")
9799
test_stats['passed'] += 1
98100
else:
99101
print(f"{RED}✗ Response content mismatch{NC}")
100-
print(f"Expected: {expected_response}")
101-
print(f"Got: {actual_text}")
102+
print(f"Expected: {repr(expected_response)}")
103+
print(f"Got: {repr(actual_text)}")
102104
test_stats['failed'] += 1
103105
else:
104106
print(f"{RED}✗ Response type mismatch{NC}")
@@ -157,7 +159,7 @@ def run_tests():
157159
# 7. Test error handling
158160
test_endpoint("/error",
159161
expected_code=500,
160-
expected_response="Test Error")
162+
expected_substring="Test Error")
161163
test_endpoint("/custom-error",
162164
expected_code=500,
163165
expected_response={"code": 400, "message": "Custom Error"},
@@ -189,6 +191,9 @@ def run_tests():
189191
test_endpoint("/add", expected_response=1)
190192
test_endpoint("/events", expected_response=1)
191193

194+
# 12. Test ctx.response.body
195+
test_endpoint("/response-body", expected_response="response body")
196+
192197
if __name__ == "__main__":
193198
try:
194199
run_tests()

utils/banner.cjs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
const s = require('gradient-string')([
1+
const slogan = 'lua-resty-router - Elegant, performant and productive router for Openresty'
2+
const colorizedslogan = require('gradient-string')([
23
{ color: '#42d392', pos: 0 },
34
{ color: '#42d392', pos: 0.1 },
45
{ color: '#647eff', pos: 1 }
5-
])('lua-resty-router - Elegant, performant and productive router for Openresty')
6+
])(slogan)
67

7-
console.log(JSON.stringify(s))
8+
9+
10+
console.log(`
11+
const defaultBanner = '${slogan}'
12+
const gradientBanner = ${JSON.stringify(colorizedslogan)}
13+
export { defaultBanner, gradientBanner }
14+
`)

utils/banners.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
const defaultBanner = 'Vue.js - The Progressive JavaScript Framework'
2-
3-
// generated by the following code:
4-
//
5-
// require('gradient-string')([
6-
// { color: '#42d392', pos: 0 },
7-
// { color: '#42d392', pos: 0.1 },
8-
// { color: '#647eff', pos: 1 }
9-
// ])('Vue.js - The Progressive JavaScript Framework'))
10-
//
11-
// Use the output directly here to keep the bundle small.
12-
const gradientBanner =
13-
"\u001b[38;2;66;211;146ml\u001b[39m\u001b[38;2;66;211;146mu\u001b[39m\u001b[38;2;66;211;146ma\u001b[39m\u001b[38;2;66;211;146m-\u001b[39m\u001b[38;2;66;211;146mr\u001b[39m\u001b[38;2;66;211;146me\u001b[39m\u001b[38;2;66;211;146ms\u001b[39m\u001b[38;2;67;209;148mt\u001b[39m\u001b[38;2;67;208;150my\u001b[39m\u001b[38;2;68;206;152m-\u001b[39m\u001b[38;2;68;205;154mr\u001b[39m\u001b[38;2;69;203;156mo\u001b[39m\u001b[38;2;70;202;158mu\u001b[39m\u001b[38;2;70;200;160mt\u001b[39m\u001b[38;2;71;199;162me\u001b[39m\u001b[38;2;71;197;164mr\u001b[39m \u001b[38;2;72;196;165m-\u001b[39m \u001b[38;2;73;194;167mH\u001b[39m\u001b[38;2;73;193;169mi\u001b[39m\u001b[38;2;74;191;171mg\u001b[39m\u001b[38;2;75;190;173mh\u001b[39m \u001b[38;2;75;188;175mp\u001b[39m\u001b[38;2;76;187;177me\u001b[39m\u001b[38;2;76;185;179mr\u001b[39m\u001b[38;2;77;184;181mf\u001b[39m\u001b[38;2;78;182;183mo\u001b[39m\u001b[38;2;78;181;185mr\u001b[39m\u001b[38;2;79;179;187mm\u001b[39m\u001b[38;2;79;178;189ma\u001b[39m\u001b[38;2;80;176;191mn\u001b[39m\u001b[38;2;81;175;193mc\u001b[39m\u001b[38;2;81;173;195me\u001b[39m \u001b[38;2;82;172;197ma\u001b[39m\u001b[38;2;82;170;199mn\u001b[39m\u001b[38;2;83;169;201md\u001b[39m \u001b[38;2;84;167;202mp\u001b[39m\u001b[38;2;84;165;204mr\u001b[39m\u001b[38;2;85;164;206mo\u001b[39m\u001b[38;2;85;162;208md\u001b[39m\u001b[38;2;86;161;210mu\u001b[39m\u001b[38;2;87;159;212mc\u001b[39m\u001b[38;2;87;158;214mt\u001b[39m\u001b[38;2;88;156;216mi\u001b[39m\u001b[38;2;88;155;218mv\u001b[39m\u001b[38;2;89;153;220me\u001b[39m \u001b[38;2;90;152;222mr\u001b[39m\u001b[38;2;90;150;224mo\u001b[39m\u001b[38;2;91;149;226mu\u001b[39m\u001b[38;2;92;147;228mt\u001b[39m\u001b[38;2;92;146;230me\u001b[39m\u001b[38;2;93;144;232mr\u001b[39m \u001b[38;2;93;143;234mf\u001b[39m\u001b[38;2;94;141;236mo\u001b[39m\u001b[38;2;95;140;237mr\u001b[39m \u001b[38;2;95;138;239mO\u001b[39m\u001b[38;2;96;137;241mp\u001b[39m\u001b[38;2;96;135;243me\u001b[39m\u001b[38;2;97;134;245mn\u001b[39m\u001b[38;2;98;132;247mr\u001b[39m\u001b[38;2;98;131;249me\u001b[39m\u001b[38;2;99;129;251ms\u001b[39m\u001b[38;2;99;128;253mt\u001b[39m\u001b[38;2;100;126;255my\u001b[39m"
14-
export { defaultBanner, gradientBanner }
1+
const defaultBanner = 'lua-resty-router - Elegant, performant and productive router for Openresty'
2+
const gradientBanner = "\u001b[38;2;66;211;146ml\u001b[39m\u001b[38;2;66;211;146mu\u001b[39m\u001b[38;2;66;211;146ma\u001b[39m\u001b[38;2;66;211;146m-\u001b[39m\u001b[38;2;66;211;146mr\u001b[39m\u001b[38;2;66;211;146me\u001b[39m\u001b[38;2;66;211;146ms\u001b[39m\u001b[38;2;66;211;146mt\u001b[39m\u001b[38;2;67;210;148my\u001b[39m\u001b[38;2;67;208;150m-\u001b[39m\u001b[38;2;68;207;152mr\u001b[39m\u001b[38;2;68;205;154mo\u001b[39m\u001b[38;2;69;204;155mu\u001b[39m\u001b[38;2;70;202;157mt\u001b[39m\u001b[38;2;70;201;159me\u001b[39m\u001b[38;2;71;199;161mr\u001b[39m \u001b[38;2;71;198;163m-\u001b[39m \u001b[38;2;72;196;165mE\u001b[39m\u001b[38;2;72;195;167ml\u001b[39m\u001b[38;2;73;193;169me\u001b[39m\u001b[38;2;74;192;170mg\u001b[39m\u001b[38;2;74;190;172ma\u001b[39m\u001b[38;2;75;189;174mn\u001b[39m\u001b[38;2;75;188;176mt\u001b[39m\u001b[38;2;76;186;178m,\u001b[39m \u001b[38;2;77;185;180mp\u001b[39m\u001b[38;2;77;183;182me\u001b[39m\u001b[38;2;78;182;184mr\u001b[39m\u001b[38;2;78;180;185mf\u001b[39m\u001b[38;2;79;179;187mo\u001b[39m\u001b[38;2;79;177;189mr\u001b[39m\u001b[38;2;80;176;191mm\u001b[39m\u001b[38;2;81;174;193ma\u001b[39m\u001b[38;2;81;173;195mn\u001b[39m\u001b[38;2;82;171;197mt\u001b[39m \u001b[38;2;82;170;199ma\u001b[39m\u001b[38;2;83;169;201mn\u001b[39m\u001b[38;2;84;167;202md\u001b[39m \u001b[38;2;84;166;204mp\u001b[39m\u001b[38;2;85;164;206mr\u001b[39m\u001b[38;2;85;163;208mo\u001b[39m\u001b[38;2;86;161;210md\u001b[39m\u001b[38;2;87;160;212mu\u001b[39m\u001b[38;2;87;158;214mc\u001b[39m\u001b[38;2;88;157;216mt\u001b[39m\u001b[38;2;88;155;217mi\u001b[39m\u001b[38;2;89;154;219mv\u001b[39m\u001b[38;2;89;152;221me\u001b[39m \u001b[38;2;90;151;223mr\u001b[39m\u001b[38;2;91;149;225mo\u001b[39m\u001b[38;2;91;148;227mu\u001b[39m\u001b[38;2;92;147;229mt\u001b[39m\u001b[38;2;92;145;231me\u001b[39m\u001b[38;2;93;144;232mr\u001b[39m \u001b[38;2;94;142;234mf\u001b[39m\u001b[38;2;94;141;236mo\u001b[39m\u001b[38;2;95;139;238mr\u001b[39m \u001b[38;2;95;138;240mO\u001b[39m\u001b[38;2;96;136;242mp\u001b[39m\u001b[38;2;96;135;244me\u001b[39m\u001b[38;2;97;133;246mn\u001b[39m\u001b[38;2;98;132;247mr\u001b[39m\u001b[38;2;98;130;249me\u001b[39m\u001b[38;2;99;129;251ms\u001b[39m\u001b[38;2;99;127;253mt\u001b[39m\u001b[38;2;100;126;255my\u001b[39m"
3+
export { defaultBanner, gradientBanner }

0 commit comments

Comments
 (0)