annotate src/goodjava/webserver/examples/Example.java @ 1705:a6e27c8e7ef4

bbcode now returns data
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 Jul 2022 21:59:38 -0600
parents fa066aaa068c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Writer;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.OutputStreamWriter;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.Map;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 public class Example implements Handler {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public Response handle(Request request) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 Response response = new Response();
1607
fa066aaa068c nginx caching
Franklin Schmidt <fschmidt@gmail.com>
parents: 1454
diff changeset
26 response.headers.put( "Content-Type", "text/plain; charset=utf-8" );
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 try {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 Writer writer = new OutputStreamWriter( new ResponseOutputStream(response) );
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 writer.write("Hello World\n");
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 writer.close();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 } catch(IOException e) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 throw new RuntimeException("shouldn't happen",e);
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return response;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 public static void simple() throws IOException {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 Handler handler = new Example();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 new Server(8080,handler).start();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 public static void fancy() throws IOException {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
48 Handler mapHandler = new MapHandler(map);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
49 FileHandler fileHandler = new FileHandler();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
50 Handler dirHandler = new DirHandler(fileHandler);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1167
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 new Server(8080,handler).start();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 public static void main(String[] args) throws Exception {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 fancy();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }