Skip to content

Commit 651c945

Browse files
committed
Added setupCommands for Modules and changed README
1 parent e8561eb commit 651c945

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,60 @@ JWS Web Framework
1313

1414
## Introduction
1515
The Web Framework combines multiple JavaWebStack libraries with glue code and helpers to allow fast web development
16+
## Quick Example
1617

18+
```java
19+
20+
import org.javawebstack.httpserver.router.annotation.Body;
21+
22+
class MyWebApp extends WebApplication {
23+
24+
protected void setupConfig(Config config) {
25+
config.addEnvFile(".env");
26+
}
27+
28+
protected void setupModels(SQL sql) throws ORMConfigurationException {
29+
}
30+
31+
protected void setupServer(HTTPServer server) {
32+
server.get("/", exchange -> {
33+
return "Hello World";
34+
});
35+
36+
server.controller(new ExampleController());
37+
}
38+
}
39+
40+
// Example Controller
41+
42+
@PathPrefix("/api/v1")
43+
public class ExampleController extends HttpController {
44+
@Get("/{name}")
45+
public GetSomethingResponse getSomething(@Path("name") String name) {
46+
GetSomethingResponse response = new GetSomethingResponse();
47+
response.name = name;
48+
// Automatically Serializes it to JSON if Accept header is not set.
49+
return response;
50+
}
51+
52+
@Post // If empty it uses the /api/v1 route
53+
public boolean createSomething(@Body CreateSomethingRequest request) {
54+
// @Body uses JSON for default to unserialize it into an object of the given type, but if the Accept header is set, you can use yaml or form
55+
System.out.println(request.name);
56+
return true;
57+
}
58+
59+
// Example Response Model
60+
public class GetSomethingResponse {
61+
public String name;
62+
}
63+
64+
// Example Request Model
65+
public class CreateSomethingRequest {
66+
public String name;
67+
}
68+
}
69+
```
1770
## Documentation
1871
You can find the current docs on our [website](https://docs.javawebstack.org/framework). This is a work-in-progress project though so it's not yet complete.
1972

src/main/java/org/javawebstack/framework/WebApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public WebApplication() {
7575

7676
server = new HTTPServer()
7777
.port(config.getInt("http.server.port", 80));
78+
7879
server.beforeInterceptor(new CORSPolicy(config.get("http.server.cors", "*")));
7980
server.beforeInterceptor(new MultipartPolicy(config.get("http.server.tmp", null)));
8081
if (config.isEnabled("http.server.autoserialization", true))
@@ -187,6 +188,7 @@ public void run(String[] args){
187188
CommandLine commandLine = new CommandLine(help);
188189
help.setCommandLine(commandLine);
189190
commandLine.addSubcommand(new StartWebServerCommand(this));
191+
modules.forEach(module -> module.setupCommands(this, commandLine));
190192
setupCommands(commandLine);
191193

192194
commandLine.execute(args);

src/main/java/org/javawebstack/framework/config/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void addEnvFile(String path, File file) {
167167
IO.readPropertyFile(file).forEach((key, value) -> set(path, transformEnvKey(key.toString()), value.toString()));
168168
} catch (IOException ignored) {
169169
}
170-
System.getenv().entrySet().forEach(e -> set(path, transformEnvKey(e.getKey()), e.getValue()));
170+
System.getenv().forEach((key, value) -> set(path, transformEnvKey(key), value));
171171
}
172172

173173
public void addEnvFile(File file) {

src/main/java/org/javawebstack/framework/module/Module.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.javawebstack.orm.exception.ORMConfigurationException;
77
import org.javawebstack.orm.wrapper.SQL;
88
import org.javawebstack.orm.wrapper.SQLDriverFactory;
9+
import picocli.CommandLine;
910

1011
public interface Module {
1112

@@ -36,4 +37,6 @@ default void setupServer(WebApplication application, HTTPServer server) {
3637
default void setupSeeding(WebApplication application) {
3738
}
3839

40+
default void setupCommands(WebApplication application, CommandLine commandLine) { }
41+
3942
}

0 commit comments

Comments
 (0)