JAPANESE
https://www.wenyanet.com/opensource/ja/602e6e496b745e6d3560c91d.html
ー--
ENGLISH
(The fastest web framework in V language (vlang))
Barbaru
Valval is the fastest web framework in the V language.
This means allows you to run development websites quickly and even faster !
Simple demo:
import valval
fn hello (req valval.Request) valval.Response {
return valval. response_ok ( ' hello world ' )
}
fn main () {
mut app : = valval. new_app ( true )
app. route ( ' / ' , hello)
valval. runserver (app, 8012 )
}
install
Using Git
$ git clone https://github.com/taojy123/valval $ mkdir -p ~ / .vmodules $ ln -s $ (pwd) / valval ~ / .vmodules / valval
Use of VPM
Watchmen123456 has registered the module with vpm. If you have v in your PATH variable, just use:
$ v install watchmen123456.valval
Note : When using vpm. You need to change the import as follows:
import watchmen123456.valval
quick start
Minimal application
The minimal Valval application looks like this:
// demo.v
module main
import valval
fn hello (req valval.Request) valval.Response {
return valval. response_ok ( ' hello world ' )
}
fn main () {
mut app : = valval. new_app ( true )
app. route ( ' / ' , hello)
valval. runserver (app, 8012 )
}
Run the server
$ v run demo.v
Then you
http://127.0.0.1:8012/
Can be visited to see the website$ curl http://127.0.0.1:8012/ hello world
Debug mode
You can decide whether to use debug mode when calling
valval.new_app ()
mut app : = valval. new_app ( true ) // debug mode
mut app : = valval. new_app ( false ) // production mode
In debug mode, detailed information is output while the app is running
Service port
You can determine the service port number when you call
valval.runserver ()
valval. runserver (app, 8012 ) // listening 8012 port
valval. runserver (app, 80 ) // listening 80 port
valval server
0.0.0.0
Because it binds the address127.0.0.1: Port
Or you can access the website.ServerIp: Port
routing
App.route ()
Use the function to band the handler function and request the pathThe handler function has a type parameter
Request
can be,Response Response
mut app : = valval. new_app ( true )
app. route ( ' / ' , hello) // http://127.0.0.1
app. route ( ' / users ' , function1) // http://127.0.0.1/users
app. route ( ' / user / info ' , function2) // http://127.0.0.1/user/info
app. route ( ' POST: / book ' , function3) // http://127.0.0.1/book by POST
app. route ( ' DELETE: / book ' , function4) // http://127.0.0.1/book by DELETE
app. route ( ' / book ' , function5) // http://127.0.0.1/book by other methods
app. route ( ' * ' , function6) // all remain
valval. runserver (app, 80 )
Access to request data
Currently, only the following data can be analyzed:
- Query parameters with a GET request. Along
valval.Request.query [xxx]
x-www-form-urlencoded
Parameters by POST / PUT / PATCH request. Alongvalval.Request.form [xxx]
fn hello (req valval.Request) valval.Response {
mut name = request.query [ ' name ' ]
if name = = '' ' {
name = ' world '
}
return valval. response_ok ( ' hello $ name ' )
}
fn post_hello (req valval.Request) valval.Response {
mut name = request.form [ ' name ' ]
if name = = ' ' {
name = ' world '
}
return valval. response_ok ( ' hello $ name ' )
}
app. route ( ' GET: / hello ' , hello)
app. route ( ' POST: / hello ' , post_hello)
valval.Request.get ()
fromquery query
Or provide a quick way to get data fromform
..fn hello (req valval.Request) valval.Response {
name = request. get ( ' name ' , ' world ' ) // default:'world'
return valval. response_ok ( ' hello $ name ' )
}
app. route ( ' / hello ' , hello)
More types of request data will be supported in the future.
- URL parameters
multipart / form-data
By POST requestapplication / json
By POST request- Uploaded file
Static file
valval.App.serve_static
Used to serve local filesmut app : = valval. new_app ( true )
app. serve_static ( ' / static / ' , ' ./relative/path/to/static/ ' )
// visit http://127.0.0.1/static/xxx.js ...
app. serve_static ( ' / static2 / ' , ' / absolute / path / to / static2 / ' )
// visit http://127.0.0.1/static2/yyy.css ...
valval. runserver (app, 80 )
Rendering template
Valval implemented a template function using a whole new idea. Inspired by Vue's system.
It has the following advantages:
Vue
If you've used it before, you don't have to spend time learning how to use the template.- Never used
Vue
Even if you are a person, it is so easy that you can learn it quickly . - It can integrate some commonly used UI frameworks such as:
element element
,mint
,vant
,antd
,bootstrap
... - No need to spend time developing embedded templates
😁 ..
Template example:
server.v
:import valval
import json
struct User {
name string
age int
sex bool
}
fn users (req valval.Request) valval.Response {
// create a view by template file (`test6.html` can be a relative or absolute path)
// use` element` (https://github.com/ElemeFE/element) as ui framework
mut view : = valval. new_view (req, ' users.html ' , ' element ' ) or {
return valval. response_bad (err)
}
users : = [
User { ' Lucy ' , 13 , false },
User { ' Lily ' , 13 , false },
User { ' Jim ' , 12 , true },
]
msg : = ' This is a page of three user '
// use view.set to bind data for rendering template
// the second parameter must be a json string
view. set ( ' users ' , json. encode (users))
view. set ( ' msg ' , json. encode (msg))
return valval. response_view (view)
}
users.html
:< html >
< head >
< title > Users Page </ title >
</ head >
< body >
<!-Content in body can use template syntax->
< h3 > {{msg}} </ h3 >
< p v-for = " u in users " >
< span > {{u.name}} </ span > ,
< span > {{u.age}} </ span > ,
< el-tag v-if = " u.sex " > Male </ el-tag >
< el-tag v-else > Female </ el-tag >
</ p >
</ body >
</ html >
redirect
valval.response_redirect ()
Used to generate a redirect responsefn test1 (req valval.Request) valval.Response {
name = req. get ( ' name ' , '' ' )
if name = = ' '' return valval
. response_redirect ( ' / not_found ' )
}
return valval. response_ok ( ' hello $ name ' )
}
reaction
In addition to the response (above
response_ok
,response_view
,response_redirect
)Valval also provides other response types, such as:
struct User {
name string
age int
sex bool
}
fn text (req valval.Request) valval.Response {
return valval. response_text ( ' this is plain text response ' )
}
fn json (req valval.Request) valval.Response {
user = User { ' Tom ' , 12 , true }
return valval. response_json (user)
// -> {"name": "Tom", "age": 12, "sex": true}
}
fn json_str (req valval.Request) valval.Response {
user = User { ' Tom ' , 12 , true }
user_str = json. encode (user)
return valval. response_json_str (user_str)
// -> {" name": "Tom "," age ": 12," sex ": true}
}
fn file (req valval.Request) valval.Response {
return valval. response_file ( ' path / to / local / file ' )
}
fn bad (req valval.Request) valval.Response {
return valval. response_bad ( ' Parameter error! ' )
// response with statu code 400
}
Complete example
- You can visit https://github.com/taojy123/valval/tree/master/example to see the full example.
- The official valval website ( https://valval.cool ) is also described in the valval framework : https : //github.com/taojy123/valval_website
Install V language
Valval framework is now
V language
Supports versions0.1.24
Here's how to install V:
1. Download the pre-built V package
Please access the official homepage https://vlang.io/ and download it .
2. Run V with docker [Recommended]
docker run -it -p 8012: 8012 --name vlang taojy123 / vlang bash
Includes OpenSSL
3. Install V from source
$ git clone https://github.com/vlang/v $ cd v $ make
Install OpenSSL
macOS: $ brew install openssl Debian / Ubuntu: $ sudo apt install libssl-dev openssl ca-certificates
Windows (Win10 verified): The source can be downloaded from:
0 コメント:
コメントを投稿