Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from mamorum/v0.2.6
Browse files Browse the repository at this point in the history
v0.2.6
  • Loading branch information
mamorum committed Feb 10, 2018
2 parents 93ea46a + 2b36ed5 commit 296957a
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 332 deletions.
3 changes: 1 addition & 2 deletions pom.poml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Release to the Central -> mvn -Dgpg.skip=false deploy
pkg=com.github.mamorum:kaze:0.2.5:jar
pkg=com.github.mamorum:kaze:0.2.6:jar
depend=
javax.servlet:javax.servlet-api:3.1.0,
org.eclipse.jetty.websocket:javax-websocket-server-impl:9.4.6.v20170531:provided,
junit:junit:4.12:test,
com.google.code.gson:gson:2.8.1:test,
Expand Down
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@

<groupId>com.github.mamorum</groupId>
<artifactId>kaze</artifactId>
<version>0.2.5</version>
<version>0.2.6</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
Expand Down
47 changes: 30 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,53 @@
[![Build Status](https://travis-ci.org/mamorum/kaze.svg?branch=master)](https://travis-ci.org/mamorum/kaze)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.mamorum/kaze/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.mamorum/kaze)

- Minimal dependencies (only Servlet API)
- Supports embedded Jetty
- Also runs in other servlet containers
- Easy to create RESTful API, Web API, etc
- Runs on servlet containers
- Minimal dependencies (Only Servlet API)
- Supports embedded Jetty (Optional)


## Hello World Example (Using embedded Jetty)
### 1. Add dependency
## Hello World Example
This example is in [kaze-samples/hw](https://github.com/mamorum/kaze-sample/tree/master/hw).

### 1. Add dependencies
```xml
<dependency>
<groupId>com.github.mamorum</groupId>
<artifactId>kaze</artifactId>
<version>0.2.5</version>
<version>0.2.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.6.v20170531</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
```

### 2. Create code
```java
package kaze.sample.html;
package kaze.sample.hw;

import com.google.gson.Gson;

import kaze.App;
import kaze.server.Jetty;

// To check:
// app -> http://localhost:8080/app/hello
// doc -> http://localhost:8080/ (or /index.html)
public class JettyApp {
public class Main {
public static void main(String[] args) {
App app = new App();
Gson gson = new Gson();
app.conv(gson::fromJson, gson::toJson);
app.get("/hello", (req, res) -> {
res.html("<p>Hello World from Jetty.</p>");
res.json("msg", "Hello, World.");
});
Jetty.app(app, "/app/*");
Jetty.doc("/public", "/");
Expand All @@ -48,17 +59,19 @@ public class JettyApp {

### 3. Run
```
$ mvn compile
$ mvn exec:java -Dexec.mainClass=kaze.sample.html.JettyApp
hw> mvn compile
hw> mvn exec:java -Dexec.mainClass=kaze.sample.hw.Main
```

### 4. Check
```
$ curl -s -X GET http://localhost:8080/app/hello
<p>Hello World from Jetty.</p>
> curl -s -X GET http://localhost:8080/app/hello
{"msg":"Hello, World."}
```

## Samples
- [kaze-sample-hw](https://github.com/mamorum/kaze-sample/tree/master/hw): hello world examples, including html and json responses.
- [kaze-sample-rdb](https://github.com/mamorum/kaze-sample/tree/master/rdb): web app accessing relational database, packaged as fatjar.
- [kaze-sample-war](https://github.com/mamorum/kaze-sample/tree/master/war): web app, packaged as war.

## Other Samples
- [war](https://github.com/mamorum/kaze-sample/tree/master/war): depending only on servlet api, packaged as war.
- [rdb](https://github.com/mamorum/kaze-sample/tree/master/rdb): accessing relational database, packaged as fatjar.
- [tomcat](https://github.com/mamorum/kaze-sample/tree/master/tomcat): using embedded tomcat.
- [jackson](https://github.com/mamorum/kaze-sample/tree/master/jackson): using [jackson](https://github.com/FasterXML/jackson) as a json converter.
144 changes: 44 additions & 100 deletions src/main/java/kaze/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,60 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class App {
//-> routing
private final List<Path>
get=new ArrayList<>(), post=new ArrayList<>(),
put=new ArrayList<>(), delete=new ArrayList<>();
////-> add
public void get(String path, Func f) { add(path, f, get); }
public void post(String path, Func f) { add(path, f, post); }
public void put(String path, Func f) { add(path, f, put); }
public void delete(String path, Func f) { add(path, f, delete); }
private void add(String p, Func f, List<Path> paths) {
paths.add(Path.of(p, f));
}
////-> run
public boolean runGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { return exec(get, req, res); }
public boolean runPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { return exec(post, req, res); }
public boolean runPut(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { return exec(put, req, res); }
public boolean runDelete(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { return exec(delete, req, res); }
private boolean exec(
List<Path> paths, HttpServletRequest sreq, HttpServletResponse sres)
@SuppressWarnings("serial")
public class App extends HttpServlet {
//-> functions
@FunctionalInterface
public interface Func { void exec(Req req, Res res) throws Exception; }
@FunctionalInterface
public interface Json2obj { <T> T exec(String json, Class<T> to); }
@FunctionalInterface
public interface Obj2json { String exec(Object obj); }
//-> settings
///-> encoding
String enc = "utf-8";
public void enc(String encoding) {
this.enc=encoding;
}
///-> converter
Json2obj j2o; Obj2json o2j;
public void conv(Json2obj toObj, Obj2json toJson) {
this.j2o=toObj; this.o2j=toJson;
}
///-> routing
Route get=new Route(), post=new Route(), put=new Route(), delete=new Route();
public void get(String path, Func func) { get.add(path, func); }
public void post(String path, Func func) { post.add(path, func); }
public void put(String path, Func func) { put.add(path, func); }
public void delete(String path, Func func) { delete.add(path, func); }
//-> servlet api
@Override protected void doGet(HttpServletRequest rq, HttpServletResponse rs)
throws ServletException, IOException { run(get, rq, rs); }
@Override protected void doPost(HttpServletRequest rq, HttpServletResponse rs)
throws ServletException, IOException { run(post, rq, rs); }
@Override protected void doPut(HttpServletRequest rq, HttpServletResponse rs)
throws ServletException, IOException { run(put, rq, rs); }
@Override protected void doDelete(HttpServletRequest rq, HttpServletResponse rs)
throws ServletException, IOException { run(delete, rq, rs); }
protected void run(
Route rt, HttpServletRequest rq, HttpServletResponse rs)
throws ServletException, IOException {
if (paths.isEmpty()) return false;
String[] ptree = Path.tree(sreq);
Path path = find(ptree, paths);
if (path == null) return false;
encoding(sreq, sres);
Req req = new Req(sreq, ptree, path, json2obj);
Res res = new Res(sres, obj2json);
try { path.func.exec(req, res); }
catch (Exception e) { throw new ServletException(e); }
return true;
}
private Path find(String[] ptree, List<Path> from) {
for (Path p: from) {
if (p.match(ptree)) return p;
}
return null;
}
//-> servlet
public Servlet servlet() {
Servlet s = new Servlet();
s.app = this;
return s;
}
@SuppressWarnings("serial")
public static class Servlet extends HttpServlet {
protected App app;
@Override protected void doGet(
HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
if (!app.runGet(req, res)) res.sendError(404);
}
@Override protected void doPost(
HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
if (!app.runPost(req, res)) res.sendError(404);
}
@Override protected void doPut(
HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
if (!app.runPut(req, res)) res.sendError(404);
}
@Override protected void doDelete(
HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
if (!app.runDelete(req, res)) res.sendError(404);
}
encoding(rq, rs);
boolean run = rt.run(rq, rs, this);
if (!run) rs.sendError(404);
}
//-> encoding
public String encoding = "utf-8";
private void encoding(
protected void encoding(
HttpServletRequest req, HttpServletResponse res)
throws UnsupportedEncodingException
{
if (encoding == null) return;
throws UnsupportedEncodingException {
if (req.getCharacterEncoding() == null) {
req.setCharacterEncoding(encoding);
req.setCharacterEncoding(enc);
}
res.setCharacterEncoding(encoding);
}
//-> json
Json2obj json2obj = App::noJson2obj;
Obj2json obj2json = App::noObj2json;
public void parser(Json2obj j2o, Obj2json o2j) {
json2obj=j2o; obj2json=o2j;
}
@FunctionalInterface public static interface Json2obj {
<T> T exec(String json, Class<T> obj);
}
@FunctionalInterface public static interface Obj2json {
String exec(Object obj);
}
static String errMsg =
"No json parser found. Call `App#parser(Json2obj, Obj2json)` to set.";
static <T> T noJson2obj(String json, Class<T> obj) {
throw new IllegalStateException(errMsg);
}
static String noObj2json(Object obj) {
throw new IllegalStateException(errMsg);
res.setCharacterEncoding(enc);
}
}
5 changes: 0 additions & 5 deletions src/main/java/kaze/Func.java

This file was deleted.

50 changes: 0 additions & 50 deletions src/main/java/kaze/Path.java

This file was deleted.

Loading

0 comments on commit 296957a

Please sign in to comment.