Skip to content

Commit

Permalink
improve static file serve
Browse files Browse the repository at this point in the history
  • Loading branch information
taojy123 committed Dec 24, 2019
1 parent 92b6e38 commit cc122ad
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
8 changes: 8 additions & 0 deletions example/example.v
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ fn main() {

}

// http://127.0.0.1:8012
// http://127.0.0.1:8012/test1?name=hello
// http://127.0.0.1:8012/test2
// http://127.0.0.1:8012/test3
// http://127.0.0.1:8012/test4
// http://127.0.0.1:8012/test5
// http://127.0.0.1:8012/test6

2 changes: 1 addition & 1 deletion example/static/demo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// test js file

console.log('test valval')
alert('gogogo')

Binary file added example/static/valval_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion example/template/test4.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<html>
<head>
<title>valval example test4</title>
<script src="/static/demo.js"></script>
</head>
<body>
<form action='' method="post">
Expand Down
32 changes: 4 additions & 28 deletions example/template/test5.html
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
<html>
<head>
<title>valval template power by vue</title>
<title>valval</title>
<script src="/static/demo.js"></script>
</head>
<body>
<!-- created by valval -->
<div id="valapp">

<h1>{{ data.name }}</h1>
<h2>{{ data.age }}</h2>
<h3 v-if="data.sex">male</h3>
<h3 v-else>female</h3>

</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.0"></script>
<script>
vue = new Vue({
el: '#valapp',
data: function() {
return {
data: {
name: 'Lucy',
age: 13,
sex: false,
}
}
}
})
</script>
<body>
<h1>Hello world</h1>
</body>

</html>
1 change: 1 addition & 0 deletions example/template/test6.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<title>valval template power by vue</title>
</head>
<body>
<img src="/static/valval_logo.png" alt="logo">
<h1>{{ user.name }}</h1>
<h2>{{ user.age }}</h2>
<el-tag v-if="user.sex">male</el-tag>
Expand Down
54 changes: 35 additions & 19 deletions valval.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
json
os
time
strings
)

const (
VERSION = '0.1.0'
VERSION = '0.1.1'
V_VERSION = '0.1.23'
HTTP_404 = 'HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\n\r\n404 Not Found'
HTTP_413 = 'HTTP/1.1 413 Request Entity Too Large\r\nContent-Type: text/plain\r\n\r\n413 Request Entity Too Large'
HTTP_500 = 'HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\n500 Internal Server Error'
Expand Down Expand Up @@ -65,7 +67,7 @@ pub fn (res mut Response) set_header(key string, value string) {
}

fn (res Response) header_text() string {
// res.header_text() => '// Content-Encoding: UTF-8\r\nContent-Length: 138'
// res.header_text() => '// Content-Encoding: UTF-8\r\nContent-Length: 138\r\n'
mut text := ''
keys := res.headers.keys()
for key in keys {
Expand Down Expand Up @@ -217,14 +219,15 @@ struct Server {
pub fn (server Server) run() {
app := server.app
println('${app.name} running on http://$server.address:$server.port ...')
println('working in: ${os.getwd()}')
println('OS: ${os.user_os()}, Debug: ${app.debug}')
println('Version: $VERSION')
println('Working in: ${os.getwd()}')
println('Server OS: ${os.user_os()}, Debug: ${app.debug}')
println('Version: $VERSION, support v version: $V_VERSION')

// listener := net.listen(server.port) or { panic('failed to listen') }
for {
listener := net.listen(server.port) or { panic('failed to listen') }
conn := listener.accept() or { panic('accept failed') }
listener.close() or {} // todo: do not close listener and recreate everytime
listener.close() or {} // todo: do not close listener and recreate it everytime
if app.debug {
println('===============')
println(conn)
Expand Down Expand Up @@ -290,7 +293,7 @@ pub fn (server Server) run() {
}
body = body.trim_space()
if app.debug {
println('------------')
println('------ request headers ------')
println(headers)
if body.len > 2000 {
println(body[..2000] + ' ...')
Expand All @@ -300,24 +303,37 @@ pub fn (server Server) run() {
}

res := app.handle(method, path, query, body, headers)
mut result := 'HTTP/1.1 $res.status ${res.status_msg()}\r\n'
result += 'Content-Type: $res.content_type\r\n'
result += 'Content-Length: $res.body.len\r\n'
result += '${res.header_text()}'
result += '\r\n'
result += '$res.body'

mut builder := strings.new_builder(1024)
builder.write('HTTP/1.1 $res.status ${res.status_msg()}\r\n')
builder.write('Content-Type: $res.content_type\r\n')
builder.write('Content-Length: $res.body.len\r\n')
builder.write('Connection: close\r\n')
builder.write('${res.header_text()}')
builder.write('\r\n')

result := builder.str()
conn.send(result.str, result.len) or {}
if app.debug {
println('------------')
if result.len > 2000 {
println(result[..2000] + ' ...')
println('------ response headers ------')
if result.len > 500 {
println(result[..500] + ' ...')
} else {
println(result)
}
}

conn.write(result) or {
conn.write(HTTP_500) or {}
builder.free()

conn.send(res.body.str, res.body.len) or {}
if app.debug {
println('------- response body -----')
if res.body.len > 2000 {
println(res.body[..2000] + ' ...')
} else {
println(res.body)
}
}

conn.close() or {}

if app.debug {
Expand Down

0 comments on commit cc122ad

Please sign in to comment.