kristofer revisou este gist . Ir para a revisão
1 file changed, 85 insertions
AytchTeeTeePee.md(arquivo criado)
@@ -0,0 +1,85 @@ | |||
1 | + | # AytchTeeTeePee | |
2 | + | ||
3 | + | Verrrrry simple HTTP server in Java. Well, not as simple as golang or python, but well, _because java._ | |
4 | + | ||
5 | + | ||
6 | + | in python3 | |
7 | + | ||
8 | + | ```python | |
9 | + | bash$ python3 -m http.server 9000 | |
10 | + | ``` | |
11 | + | (seriously, will start serving files from CWD) | |
12 | + | ||
13 | + | ||
14 | + | in golang | |
15 | + | ||
16 | + | ```go | |
17 | + | package main | |
18 | + | ||
19 | + | import ( | |
20 | + | "net/http" | |
21 | + | ) | |
22 | + | ||
23 | + | // create a handler struct | |
24 | + | type HttpHandler struct{} | |
25 | + | ||
26 | + | // implement `ServeHTTP` method on `HttpHandler` struct | |
27 | + | func (h HttpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) { | |
28 | + | // create response binary data | |
29 | + | data := []byte("Hello World!") // slice of bytes | |
30 | + | // write `data` to response | |
31 | + | res.Write(data) | |
32 | + | } | |
33 | + | ||
34 | + | func main() { | |
35 | + | ||
36 | + | // create a new handler | |
37 | + | handler := HttpHandler{} | |
38 | + | ||
39 | + | // listen and serve | |
40 | + | http.ListenAndServe(":9000", handler) | |
41 | + | ||
42 | + | } | |
43 | + | ``` | |
44 | + | ||
45 | + | or again in go | |
46 | + | ||
47 | + | ```go | |
48 | + | package main | |
49 | + | ||
50 | + | import ( | |
51 | + | "fmt" | |
52 | + | "io" | |
53 | + | "log" | |
54 | + | "net" | |
55 | + | "net/http" | |
56 | + | "os" | |
57 | + | ) | |
58 | + | ||
59 | + | func main() { | |
60 | + | http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { | |
61 | + | fmt.Fprint(w, "Hello, playground") | |
62 | + | }) | |
63 | + | ||
64 | + | log.Println("Starting server...") | |
65 | + | l, err := net.Listen("tcp", "localhost:8080") | |
66 | + | if err != nil { | |
67 | + | log.Fatal(err) | |
68 | + | } | |
69 | + | go func() { | |
70 | + | log.Fatal(http.Serve(l, nil)) | |
71 | + | }() | |
72 | + | ||
73 | + | log.Println("Sending request...") | |
74 | + | res, err := http.Get("http://localhost:8080/hello") | |
75 | + | if err != nil { | |
76 | + | log.Fatal(err) | |
77 | + | } | |
78 | + | ||
79 | + | log.Println("Reading response...") | |
80 | + | if _, err := io.Copy(os.Stdout, res.Body); err != nil { | |
81 | + | log.Fatal(err) | |
82 | + | } | |
83 | + | } | |
84 | + | ``` | |
85 | + |
kristofer revisou este gist . Ir para a revisão
1 file changed, 88 insertions
httpjava.java(arquivo criado)
@@ -0,0 +1,88 @@ | |||
1 | + | package rocks.zipcode; | |
2 | + | ||
3 | + | import java.io.IOException; | |
4 | + | import java.io.OutputStream; | |
5 | + | import java.net.InetSocketAddress; | |
6 | + | ||
7 | + | import com.sun.net.httpserver.HttpExchange; | |
8 | + | import com.sun.net.httpserver.HttpHandler; | |
9 | + | import com.sun.net.httpserver.HttpServer; | |
10 | + | ||
11 | + | public class Main { | |
12 | + | static final int port = 8000; | |
13 | + | public static void main(String[] args) throws Exception { | |
14 | + | HttpServer server = HttpServer.create(new InetSocketAddress(Main.port), 0); | |
15 | + | server.createContext("/", new RootHandler()); | |
16 | + | server.createContext("/test", new TestHandler()); | |
17 | + | server.createContext("/foo", new FooHandler()); | |
18 | + | server.createContext("/bar", new BarHandler()); | |
19 | + | // server.setExecutor(null); // creates a default executor | |
20 | + | // server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool()); | |
21 | + | server.setExecutor(java.util.concurrent.Executors.newFixedThreadPool(5)); | |
22 | + | server.start(); | |
23 | + | } | |
24 | + | ||
25 | + | static class RootHandler implements HttpHandler { | |
26 | + | @Override | |
27 | + | public void handle(HttpExchange t) throws IOException { | |
28 | + | String response = "Zip Code Rocks!"; | |
29 | + | t.sendResponseHeaders(200, response.length()); | |
30 | + | OutputStream os = t.getResponseBody(); | |
31 | + | os.write(response.getBytes()); | |
32 | + | os.close(); | |
33 | + | } | |
34 | + | } | |
35 | + | static class TestHandler implements HttpHandler { | |
36 | + | @Override | |
37 | + | public void handle(HttpExchange t) throws IOException { | |
38 | + | String response = "Testing! Testing! Zip Code Rocks!"; | |
39 | + | t.sendResponseHeaders(200, response.length()); | |
40 | + | OutputStream os = t.getResponseBody(); | |
41 | + | os.write(response.getBytes()); | |
42 | + | os.close(); | |
43 | + | } | |
44 | + | } | |
45 | + | static class FooHandler implements HttpHandler { | |
46 | + | @Override | |
47 | + | public void handle(HttpExchange t) throws IOException { | |
48 | + | String response = "<HTML><BODY><h1>HTML</h1><p>This is a proper paragraph of text.</p></BODY></HTML>"; | |
49 | + | t.sendResponseHeaders(200, response.length()); | |
50 | + | OutputStream os = t.getResponseBody(); | |
51 | + | os.write(response.getBytes()); | |
52 | + | os.close(); | |
53 | + | } | |
54 | + | } | |
55 | + | ||
56 | + | static class BarHandler implements HttpHandler { | |
57 | + | @Override | |
58 | + | public void handle(HttpExchange t) throws IOException { | |
59 | + | String response = MakeBody(); | |
60 | + | t.sendResponseHeaders(200, response.length()); | |
61 | + | OutputStream os = t.getResponseBody(); | |
62 | + | os.write(response.getBytes()); | |
63 | + | os.close(); | |
64 | + | } | |
65 | + | } | |
66 | + | ||
67 | + | static String MakeBody() { | |
68 | + | StringBuilder sb = new StringBuilder(); | |
69 | + | String css = "body { font-family: Arial, sans-serif; line-height: 1.6; color: #222; " + | |
70 | + | "max-width: 40rem; padding: 2rem; margin: auto; background: #fafafa; }" + | |
71 | + | "a { color: #2ECC40; } h1, h2, strong { color: #111; }"; | |
72 | + | ||
73 | + | sb.append("<HTML>"); | |
74 | + | sb.append("<style>"); | |
75 | + | sb.append(css); | |
76 | + | sb.append("</style>"); | |
77 | + | sb.append("<BODY><h1>HTML Title</h1>"); | |
78 | + | sb.append("<p>"); | |
79 | + | for(int i = 0; i<10; i++) { | |
80 | + | sb.append("This is a proper paragraph of text. "); | |
81 | + | } | |
82 | + | sb.append("</p>"); | |
83 | + | sb.append("</BODY></HTML>"); | |
84 | + | ||
85 | + | return sb.toString(); | |
86 | + | } | |
87 | + | ||
88 | + | } |