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