Skip to content

Commit

Permalink
all: 0.3.6 / support for latest V 0.4.x
Browse files Browse the repository at this point in the history
* untoched import paths

* all: fix option/result, fix tests, examples
  • Loading branch information
dnkdev authored Dec 4, 2023
1 parent 4dce806 commit 2e1ba86
Show file tree
Hide file tree
Showing 16 changed files with 4,353 additions and 3,976 deletions.
38 changes: 18 additions & 20 deletions ctx/ctx.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import v.vmod
import strings
import context

const (
vm = vmod.decode(@VMOD_FILE) or { panic(err) }
default_headers = {
'Content-Type': ['text/html; charset=UTF-8']
'X-Powered-By': ['$vm.name/$vm.version']
'Server': ['$vm.name']
}
)
const vm = vmod.decode(@VMOD_FILE) or { panic(err) }
const default_headers = {
'Content-Type': ['text/html; charset=UTF-8']
'X-Powered-By': ['${vm.name}/${vm.version}']
'Server': ['${vm.name}']
}

pub type HandlerFunc = fn (req &Req, mut res Resp)

Expand Down Expand Up @@ -83,7 +81,7 @@ pub fn (req &Req) parse_form() !map[string]string {
return form_data_map
}
'multipart/form-data' {
multipart_form_data := req.parse_files() !
multipart_form_data := req.parse_files()!
mut form_data_map := map[string]string{}
for key, datum in multipart_form_data {
for i, data in datum {
Expand Down Expand Up @@ -179,16 +177,16 @@ pub mut:
}

// send writes the body and status code to the response data.
[inline]
@[inline]
pub fn (mut res Resp) send(body string, status_code int) {
res.body = body.bytes()
res.status_code = status_code
}

// send_file writes the contents of the file to the response data.
[inline]
@[inline]
pub fn (mut res Resp) send_file(filename string, status_code int) {
fl := os.read_bytes(os.join_path(os.getwd(), '/$filename')) or {
fl := os.read_bytes(os.join_path(os.getwd(), '/${filename}')) or {
res.send_status(404)
return
}
Expand All @@ -200,37 +198,37 @@ pub fn (mut res Resp) send_file(filename string, status_code int) {

// send_json is a generic function that encodes the payload and
// writes the JSON string to the response data.
[inline]
pub fn (mut res Resp) send_json<T>(payload T, status_code int) {
@[inline]
pub fn (mut res Resp) send_json[T](payload T, status_code int) {
json_string := json.encode(payload)
res.send(json_string, status_code)
res.headers['Content-Type'] = ['application/json']
}

// send_status sends an HTML response of the status code
[inline]
@[inline]
pub fn (mut res Resp) send_status(status_code int) {
msg := utils.status_code_msg(status_code)
res.headers['Content-Type'] = ['text/html']
res.send('<h1>$status_code $msg</h1>', status_code)
res.send('<h1>${status_code} ${msg}</h1>', status_code)
}

// redirect writes a 301 response and redirects to the
// specified url or location
[inline]
@[inline]
pub fn (mut res Resp) redirect(url string) {
res.status_code = 302
res.headers['Location'] = [url]
}

[inline]
@[inline]
pub fn (mut res Resp) permanent_redirect(url string) {
res.status_code = 301
res.headers['Location'] = [url]
}

// send_html writes the body to the response data as an HTML content
[inline]
@[inline]
pub fn (mut res Resp) send_html(ht string, status_code int) {
res.headers['Content-Type'] = ['text/html']
res.send(ht, status_code)
Expand All @@ -240,7 +238,7 @@ pub fn (res &Resp) headers_bytes() []u8 {
mut headers := strings.new_builder(res.headers.len * 10)
for k, values in res.headers {
for v in values {
headers.write_string('\r\n$k: $v')
headers.write_string('\r\n${k}: ${v}')
}
}
buf := headers.clone()
Expand Down
14 changes: 7 additions & 7 deletions examples/db_example.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn layout(title string, body []html.Tag) html.Tag {
'name': 'referrer'
'content': 'origin-when-cross-origin'
}),
html.tag(name: 'title', text: '$title | Vex SQLite Test'),
html.tag(name: 'title', text: '${title} | Vex SQLite Test'),
html.style('
body {
width: 36rem;
Expand All @@ -44,7 +44,7 @@ fn layout(title string, body []html.Tag) html.Tag {
const db_ctx_key = 'db'

fn get_db(req &ctx.Req) ?&sqlite.DB {
raw_db := req.ctx.value(db_ctx_key) ?
raw_db := req.ctx.value(db_ctx_key)?
if raw_db is sqlite.DB {
return raw_db
}
Expand All @@ -53,9 +53,9 @@ fn get_db(req &ctx.Req) ?&sqlite.DB {

fn main() {
mut app := router.new()
db := sqlite.connect(':memory:') !
db.exec('drop table if exists users;')
db.exec('create table users (id integer primary key, name text default "");')
db := sqlite.connect(':memory:')!
db.exec('drop table if exists users;')!
db.exec('create table users (id integer primary key, name text default "");')!
app_ctx := context.with_value(context.todo(), db_ctx_key, db)
app.inject_context(app_ctx)
app.route(.get, '/', fn (req &ctx.Req, mut res ctx.Resp) {
Expand Down Expand Up @@ -111,7 +111,7 @@ fn main() {
res.send_status(500)
return
}
users_from_db, _ := db2.exec('select * from users;')
users_from_db := db2.exec('select * from users;') or { panic(err) }
mut users := []html.Tag{}
for row in users_from_db {
tag := html.Tag{
Expand Down Expand Up @@ -184,7 +184,7 @@ fn main() {
map[string]string{}
}
name := form_data['name']
db2.exec('insert into users (name) values ("$name");')
db2.exec('insert into users (name) values ("${name}");') or { panic(err) }
res.permanent_redirect('/users')
})
server.serve(app, 8080)
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware_example.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct Person {
}

fn print_req_info(mut req ctx.Req, mut res ctx.Resp) {
println('$req.method $req.path')
println('${req.method} ${req.path}')
}

fn do_stuff(mut req ctx.Req, mut res ctx.Resp) {
Expand Down
24 changes: 12 additions & 12 deletions html/html.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub struct BlockTagConfig {
}

// return the tag to encoded JSON
[inline]
@[inline]
pub fn (tag Tag) str() string {
return '{name: $tag.name, children: $tag.children.len}'
return '{name: ${tag.name}, children: ${tag.children.len}}'
}

// html returns a Tag equivalent to `<!DOCTYPE html><html>...</html>`
[inline]
@[inline]
pub fn html(children []Tag) Tag {
return Tag{
name: 'html'
Expand All @@ -33,7 +33,7 @@ pub fn html(children []Tag) Tag {
}

// block is the same as `tag` but with Tag children
[inline]
@[inline]
pub fn block(tag BlockTagConfig) Tag {
return Tag{
name: tag.name
Expand All @@ -43,7 +43,7 @@ pub fn block(tag BlockTagConfig) Tag {
}

// meta returns a Tag equivalent to `<meta />`
[inline]
@[inline]
pub fn meta(attr map[string]string) Tag {
return Tag{
name: 'meta'
Expand All @@ -53,7 +53,7 @@ pub fn meta(attr map[string]string) Tag {
}

// link returns a Tag equivalent to `<link />`
[inline]
@[inline]
pub fn link(attr map[string]string) Tag {
return Tag{
name: 'link'
Expand All @@ -63,7 +63,7 @@ pub fn link(attr map[string]string) Tag {
}

// style returns a Tag equivalent to `<style>...</style>`
[inline]
@[inline]
pub fn style(style string) Tag {
return Tag{
name: 'style'
Expand All @@ -72,13 +72,13 @@ pub fn style(style string) Tag {
}

// tag returns itself for uniformity when using function-based DSL
[inline]
@[inline]
pub fn tag(tag Tag) Tag {
return tag
}

// br returns a Tag equivalent to `<br />`
[inline]
@[inline]
pub fn br() Tag {
return Tag{
name: 'br'
Expand All @@ -99,9 +99,9 @@ pub fn (tag Tag) html() string {
if tag.name == 'html' {
sb.write_string('<!DOCTYPE html>')
}
sb.write_string('<$tag.name')
sb.write_string('<${tag.name}')
for prop_name, prop in tag.attr {
sb.write_string(' $prop_name="$prop"')
sb.write_string(' ${prop_name}="${prop}"')
}
if !tag.empty {
sb.write_string('>')
Expand All @@ -115,7 +115,7 @@ pub fn (tag Tag) html() string {
sb.write_string(child.html())
}
if !is_text {
sb.write_string('</$tag.name>')
sb.write_string('</${tag.name}>')
}
}
return sb.str()
Expand Down
Loading

0 comments on commit 2e1ba86

Please sign in to comment.