-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathhttp_server.gs
49 lines (46 loc) · 1.12 KB
/
http_server.gs
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
class Person{
string name;
}
func (HttpContext) handle (HttpContext ctx){
Person p = Person();
p.name = "abc";
println("p.name=" + p.name);
println("ctx=" + ctx);
ctx.JSON(200, p);
}
func (HttpContext) handle1 (HttpContext ctx){
Person p = Person();
p.name = "def";
println("p.name=" + p.name);
println("ctx=" + ctx);
ctx.JSON(200, p);
}
func (HttpContext) handle2 (HttpContext ctx){
DateTime d = DateTime();
string local = d.getCurrentTime("Asia/Shanghai","2006-01-02 15:04:05");
println(local);
string html =^
<html>
<title>hello</title>
<h1>current ^+ local +^</h1>
<p>hahaha</p>
</html>
^;
string queryPath = ctx.queryPath();
println("queryPath = " + queryPath);
// http://127.0.0.1:8000/p/2?id=100
string id = ctx.formValue("id");
println("id="+id);
ctx.HTML(200, html);
}
httpHandle("get", "/p", handle);
httpHandle("get", "/p/1", handle1);
httpHandle("get", "/p/2", handle2);
System s = System();
string[] args = s.getOSArgs();
println(args);
if (len(args) == 3){
httpRun(":" + args[2]);
}else {
httpRun(":8000");
}