Skip to content

Commit 43b7c4a

Browse files
committed
init
0 parents  commit 43b7c4a

14 files changed

+324
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Default ignored files
2+
/shelf/
3+
/.idea/workspace.xml
4+
# Datasource local storage ignored files
5+
/dataSources/
6+
/dataSources.local.xml
7+
# 基于编辑器的 HTTP 客户端请求
8+
/httpRequests/
9+
10+
out

.idea/description.html

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/uiDesigner.xml

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cool.gaolihai.Angie;
2+
3+
public class app {
4+
public static void main(String[] args) {
5+
Angie app = new Angie();
6+
7+
app.use("/test", (req, res) -> {
8+
res.setStatus(200)
9+
.setHeaders("Content-Type", "text/html")
10+
.send("<h1> Hello, web framework! </h1>");
11+
});
12+
13+
app.listen(80);
14+
}
15+
}

src/cool/gaolihai/Angie.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cool.gaolihai;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
import java.util.HashMap;
7+
8+
public class Angie {
9+
private final HashMap<String, Processor> routeMap = new HashMap();
10+
11+
public void use(String route, Processor processor) {
12+
routeMap.put(route, processor);
13+
}
14+
15+
public void listen(int port) {
16+
try {
17+
ServerSocket serverSocket = new ServerSocket(port);
18+
while (true) {
19+
Socket client = serverSocket.accept();
20+
new Thread(() -> {
21+
try {
22+
Request request = new Request(client.getInputStream());
23+
Response response = new Response(client.getOutputStream());
24+
if (routeMap.containsKey(request.getUrl())) {
25+
routeMap.get(request.getUrl()).callback(request, response);
26+
} else {
27+
response.setStatus(404).send(request.getUrl() + " not found");
28+
}
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}).start();
33+
}
34+
} catch (IOException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}

src/cool/gaolihai/Processor.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cool.gaolihai;
2+
3+
@FunctionalInterface
4+
public interface Processor {
5+
void callback(Request request, Response response);
6+
}

src/cool/gaolihai/Request.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cool.gaolihai;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
8+
public class Request {
9+
10+
private String url;
11+
private String params;
12+
private String method;
13+
14+
public Request(InputStream inputStream){
15+
try {
16+
String[] requestLine = new BufferedReader(new InputStreamReader(inputStream)).readLine().split(" ");
17+
if (requestLine.length == 3 && requestLine[2].equals("HTTP/1.1")) {
18+
this.method = requestLine[0];
19+
String allUrl = requestLine[1];
20+
if (allUrl.contains("?")) {
21+
this.url = allUrl.substring(0, allUrl.indexOf("?"));
22+
this.params = allUrl.substring(allUrl.indexOf("?") + 1);
23+
} else {
24+
this.url = allUrl;
25+
}
26+
}
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
public String getUrl() {
33+
return url;
34+
}
35+
36+
public String getParams() {
37+
return params;
38+
}
39+
40+
public String getMethod() {
41+
return method;
42+
}
43+
}

src/cool/gaolihai/Response.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cool.gaolihai;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.util.HashMap;
6+
7+
public class Response {
8+
private OutputStream outputStream;
9+
private HashMap<String, String> headers = new HashMap<>();
10+
private int status;
11+
12+
public Response(OutputStream outputStream) {
13+
this.outputStream = outputStream;
14+
}
15+
16+
public Response setHeaders(String key, String value) {
17+
this.headers.put(key, value);
18+
return this;
19+
}
20+
21+
public Response setStatus(int statusCode) {
22+
this.status = statusCode;
23+
return this;
24+
}
25+
26+
public void send(String data) {
27+
try {
28+
StringBuilder dataBuilder = new StringBuilder();
29+
dataBuilder.append("HTTP/1.1 ").append(this.status).append("\n");
30+
for (String key:
31+
this.headers.keySet()) {
32+
dataBuilder.append(key).append(": ").append(this.headers.get(key)).append("\n");
33+
}
34+
dataBuilder.append("\n").append(data);
35+
36+
outputStream.write(dataBuilder.toString().getBytes());
37+
outputStream.flush();
38+
outputStream.close();
39+
} catch (IOException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}

webFramework.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
12+

0 commit comments

Comments
 (0)