当前位置: 首页 > 工具软件 > vlang > 使用案例 >

vlang web demo

文喜
2023-12-01
import vweb
import net.http
import regex
// import sqlite // can change to 'mysql', 'pg'

const (
	port = 8013
)

struct App {
	vweb.Context
// mut:
// 	db shared sqlite.DB
}

pub fn (app App) before_request() {
	println('[web] before_request: ${app.req.method} ${app.req.url}')
}

fn main() {
	vweb.run(&App{
		// db: sqlite.connect('vweb.sql')!
}, port)
}


pub fn (mut app App) index() vweb.Result {
	return app.json({
		'hello': 'world'
	})
}

[get]
['/v2ray']
pub fn (mut app App) v2ray() vweb.Result {
	// 请求url
	resp := http.get('https://view.ssfree.ru/') or {
		println("failed to fetch data from the server")
		return app.json({
			'hello': 'world'
		})
	}

	query := r'vmess://(.*)"'

	// 获取响应内容
	body := resp.body

	mut re := regex.regex_opt(query) or { panic(err) }
	//println(re.get_code())   // uncomment to see the print of the regex execution code
	// re.debug=2  // enable maximum log
	str := re.find_all_str(body)

	// regex.regex_opt("")
	s1 := str[0]

	return app.text(s1.substr(0, s1.len - 1))
}


// [post]
// ['/register']
// pub fn (mut app App) register_user(name string, password string) vweb.Result {
// 	user := User{name:name, password, password}
// 	sql app.db {
// 		insert user into User
// 	}
// 	return app.redirect('/')
// }


/*['/users/:name']
pub fn (mut app App) user(name string) vweb.Result {
	user := sql app.db {
		select from User where name == name
	}
	return $vweb.html()
}

['/api/users/:name']
pub fn (mut app App) user(name string) vweb.Result {
	user := sql app.db {
		select from User where name == name
	}
	return app.json({
		user: id
	})
}*/

 类似资料:

相关阅读

相关文章

相关问答