Mercurial Hosting > luan
annotate src/goodjava/webserver/examples/Example.java @ 1706:93b9fd13a06c
Luan.arg fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Jul 2022 12:30:16 -0600 |
parents | fa066aaa068c |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver.examples; |
1137 | 2 |
3 import java.io.Writer; | |
4 import java.io.OutputStreamWriter; | |
5 import java.io.IOException; | |
6 import java.util.Map; | |
7 import java.util.HashMap; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
8 import goodjava.webserver.Handler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
9 import goodjava.webserver.Request; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
10 import goodjava.webserver.Response; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
11 import goodjava.webserver.ResponseOutputStream; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
12 import goodjava.webserver.Server; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
13 import goodjava.webserver.handlers.MapHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
14 import goodjava.webserver.handlers.SafeHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
15 import goodjava.webserver.handlers.LogHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
16 import goodjava.webserver.handlers.FileHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
17 import goodjava.webserver.handlers.DirHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
18 import goodjava.webserver.handlers.ListHandler; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
19 import goodjava.webserver.handlers.ContentTypeHandler; |
1137 | 20 |
21 | |
22 public class Example implements Handler { | |
23 | |
24 public Response handle(Request request) { | |
25 Response response = new Response(); | |
1607 | 26 response.headers.put( "Content-Type", "text/plain; charset=utf-8" ); |
1137 | 27 try { |
28 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); | |
29 writer.write("Hello World\n"); | |
30 writer.close(); | |
31 } catch(IOException e) { | |
32 throw new RuntimeException("shouldn't happen",e); | |
33 } | |
34 return response; | |
35 } | |
36 | |
37 public static void simple() throws IOException { | |
38 Handler handler = new Example(); | |
39 new Server(8080,handler).start(); | |
40 } | |
41 | |
42 public static void fancy() throws IOException { | |
43 Map<String,Handler> map = new HashMap<String,Handler>(); | |
1141
14927c34cbb7
webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
44 map.put( "/hello", new Example() ); |
14927c34cbb7
webserver - add examples/Headers
Franklin Schmidt <fschmidt@gmail.com>
parents:
1139
diff
changeset
|
45 map.put( "/headers", new Headers() ); |
1143
3bf5190b3c77
webserver - handle GET params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1141
diff
changeset
|
46 map.put( "/params", new Params() ); |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
1143
diff
changeset
|
47 map.put( "/cookies", new Cookies() ); |
1168 | 48 Handler mapHandler = new MapHandler(map); |
49 FileHandler fileHandler = new FileHandler(); | |
50 Handler dirHandler = new DirHandler(fileHandler); | |
51 Handler handler = new ListHandler( mapHandler, fileHandler, dirHandler ); | |
1146
2dda3c92a473
webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents:
1143
diff
changeset
|
52 handler = new ContentTypeHandler(handler); |
1137 | 53 handler = new SafeHandler(handler); |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
54 handler = new LogHandler(handler); |
1137 | 55 new Server(8080,handler).start(); |
56 } | |
57 | |
58 public static void main(String[] args) throws Exception { | |
59 fancy(); | |
60 } | |
61 } |