-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvweb.v
More file actions
54 lines (48 loc) · 1.59 KB
/
vweb.v
File metadata and controls
54 lines (48 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Example with VWeb
module main
// Imports
import vweb
import gamemaker1.http_negotiator
pub struct App {
vweb.Context
}
[get]
['/names']
pub fn (mut app App) get_names_of_languages() vweb.Result {
// Check what format the client wants the response in
preferred_media_type := http_negotiator.get_media_type(
// The `Accept` header passed by the client; defaults to '*/*' when the
// header is not provided, which means anything is fine
app.req.header.get(.accept) or { '*/*' },
// Valid media types that you are willing to respond in (in order of your
// preference)
['text/plain', 'text/html', 'application/json'],
) or {
// If:
// - none of the client's accepted media types can be provided by the server
// - a media type provided by the client is an invalid one
// Then: use 'text/html' by default
'text/html'
}
// Return the list of languages in various formats
match preferred_media_type {
'text/plain' {
return app.text('V, Go, Oberon, Rust, Swift, Kotlin, and Python')
}
'text/html' {
return app.html('<ul><li>V</li> <li>Go</li> <li>Oberon</li> <li>Rust</li> <li>Swift</li> <li>Kotlin</li> <li>Python</li></ul>')
}
'application/json' {
return app.json('{"languages":["V", "Go", "Oberon", "Rust", "Swift", "Kotlin", "Python"]}')
}
else {
// NOTE: Should not happen (the `or` block above should set the preferred
// media type to `text/html` in case any error occurs)
app.set_status(406, 'Not Acceptable')
return app.ok('Invalid response media type $preferred_media_type specified in `Accept` header')
}
}
}
fn main() {
vweb.run<App>(App{}, 8000)
}