aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/vweb/tests/vweb_test_server.v
blob: 4dfeb7d3b40d157ec9062d959f67bd6b6cf78bd6 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module main

import os
import vweb
import time

const (
	known_users = ['bilbo', 'kent']
)

struct App {
	vweb.Context
	port          int
	timeout       int
	global_config shared Config
}

struct Config {
	max_ping int
}

fn exit_after_timeout(timeout_in_ms int) {
	time.sleep(timeout_in_ms * time.millisecond)
	// eprintln('webserver is exiting ...')
	exit(0)
}

fn main() {
	if os.args.len != 3 {
		panic('Usage: `vweb_test_server.exe PORT TIMEOUT_IN_MILLISECONDS`')
	}
	http_port := os.args[1].int()
	assert http_port > 0
	timeout := os.args[2].int()
	assert timeout > 0
	go exit_after_timeout(timeout)
	//
	shared config := &Config{
		max_ping: 50
	}
	app := &App{
		port: http_port
		timeout: timeout
		global_config: config
	}
	eprintln('>> webserver: started on http://127.0.0.1:$app.port/ , with maximum runtime of $app.timeout milliseconds.')
	// vweb.run<App>(mut app, http_port)
	vweb.run(app, http_port)
}

// pub fn (mut app App) init_server() {
//}

pub fn (mut app App) index() vweb.Result {
	assert app.global_config.max_ping == 50
	return app.text('Welcome to VWeb')
}

pub fn (mut app App) simple() vweb.Result {
	return app.text('A simple result')
}

pub fn (mut app App) html_page() vweb.Result {
	return app.html('<h1>ok</h1>')
}

// the following serve custom routes
['/:user/settings']
pub fn (mut app App) settings(username string) vweb.Result {
	if username !in known_users {
		return app.not_found()
	}
	return app.html('username: $username')
}

['/:user/:repo/settings']
pub fn (mut app App) user_repo_settings(username string, repository string) vweb.Result {
	if username !in known_users {
		return app.not_found()
	}
	return app.html('username: $username | repository: $repository')
}

['/json_echo'; post]
pub fn (mut app App) json_echo() vweb.Result {
	// eprintln('>>>>> received http request at /json_echo is: $app.req')
	app.set_content_type(app.req.header.get(.content_type) or { '' })
	return app.ok(app.req.data)
}

['/form_echo'; post]
pub fn (mut app App) form_echo() vweb.Result {
	app.set_content_type(app.req.header.get(.content_type) or { '' })
	return app.ok(app.form['foo'])
}

// Make sure [post] works without the path
[post]
pub fn (mut app App) json() vweb.Result {
	// eprintln('>>>>> received http request at /json is: $app.req')
	app.set_content_type(app.req.header.get(.content_type) or { '' })
	return app.ok(app.req.data)
}

pub fn (mut app App) shutdown() vweb.Result {
	session_key := app.get_cookie('skey') or { return app.not_found() }
	if session_key != 'superman' {
		return app.not_found()
	}
	go app.gracefull_exit()
	return app.ok('good bye')
}

fn (mut app App) gracefull_exit() {
	eprintln('>> webserver: gracefull_exit')
	time.sleep(100 * time.millisecond)
	exit(0)
}