Mercurial Hosting > luan
annotate src/luan/webserver/examples/Example.java @ 1138:4189027691b7
webserver - add LogHandler
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 29 Jan 2018 19:48:21 -0700 |
parents | c123ee15f99b |
children | 8126370ea8c0 |
rev | line source |
---|---|
1137 | 1 package luan.webserver.examples; |
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; | |
8 import org.apache.log4j.BasicConfigurator; | |
9 import luan.webserver.Handler; | |
10 import luan.webserver.Request; | |
11 import luan.webserver.Response; | |
12 import luan.webserver.ResponseOutputStream; | |
13 import luan.webserver.Server; | |
14 import luan.webserver.handlers.MapHandler; | |
15 import luan.webserver.handlers.SafeHandler; | |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
16 import luan.webserver.handlers.LogHandler; |
1137 | 17 |
18 | |
19 public class Example implements Handler { | |
20 | |
21 public Response handle(Request request) { | |
22 Response response = new Response(); | |
23 response.headers.put( "Content-Type", "text/plain; charset=UTF-8" ); | |
24 try { | |
25 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) ); | |
26 writer.write("Hello World\n"); | |
27 writer.close(); | |
28 } catch(IOException e) { | |
29 throw new RuntimeException("shouldn't happen",e); | |
30 } | |
31 return response; | |
32 } | |
33 | |
34 public static void simple() throws IOException { | |
35 Handler handler = new Example(); | |
36 new Server(8080,handler).start(); | |
37 } | |
38 | |
39 public static void fancy() throws IOException { | |
40 Handler handler = new Example(); | |
41 Map<String,Handler> map = new HashMap<String,Handler>(); | |
42 map.put("/hello",handler); | |
43 handler = new MapHandler(map); | |
44 handler = new SafeHandler(handler); | |
1138
4189027691b7
webserver - add LogHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
1137
diff
changeset
|
45 handler = new LogHandler(handler); |
1137 | 46 new Server(8080,handler).start(); |
47 } | |
48 | |
49 public static void main(String[] args) throws Exception { | |
50 BasicConfigurator.configure(); | |
51 fancy(); | |
52 } | |
53 } |