|
| 1 | +<p align="center"><img src="https://raw.githubusercontent.com/JavaWebStack/docs/master/docs/assets/img/icon.svg" width="100"> |
| 2 | + |
1 | 3 | # Web-Framework |
2 | 4 | The Web Framework combines multiple JavaWebStack libraries with glue code and helpers to allow fast web development |
| 5 | + |
| 6 | + |
| 7 | +# Getting started |
| 8 | +## Repository |
| 9 | +### Maven (pom.xml) |
| 10 | +```xml |
| 11 | +<repositories> |
| 12 | + <repository> |
| 13 | + <id>javawebstack</id> |
| 14 | + <url>https://repo.javawebstack.org</url> |
| 15 | + </repository> |
| 16 | +</repositories> |
| 17 | + |
| 18 | +<dependencies> |
| 19 | + <dependency> |
| 20 | + <groupId>org.javawebstack</groupId> |
| 21 | + <artifactId>Web-Framework</artifactId> |
| 22 | + <version>1.0-SNAPSHOT</version> |
| 23 | + </dependency> |
| 24 | + |
| 25 | + <dependency> |
| 26 | + <groupId>mysql</groupId> |
| 27 | + <artifactId>mysql-connector-java</artifactId> |
| 28 | + <version>8.0.22</version> |
| 29 | + <scope>compile</scope> |
| 30 | + </dependency> |
| 31 | +</dependencies> |
| 32 | +``` |
| 33 | +`Build` [Buildscript for compilation with Maven](https://gist.github.com/JulianFun123/14b412245ecf0257c7819eb8dadc1438) |
| 34 | + |
| 35 | +## Usage |
| 36 | +```java |
| 37 | + |
| 38 | +public class ExampleApplication extends WebApplication { |
| 39 | + |
| 40 | + protected void setupConfig(Config config) { |
| 41 | + Map<String, String> map = new HashMap<>(); |
| 42 | + // Maps a config entry with an key for .get("key") |
| 43 | + map.put("EXAMPLE_KEY", "example.key"); |
| 44 | + config.addEnvKeyMapping(map); |
| 45 | + |
| 46 | + // Registers .env as a configuration and adds System envs |
| 47 | + config.addEnvFile(".env"); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + protected void setupModels(SQL sql) throws ORMConfigurationException { |
| 52 | + // The connection is going to be built by the credentials given in the config |
| 53 | + |
| 54 | + ORMConfig config = new ORMConfig().setTablePrefix("example_"); // .setDefaultSize(255) |
| 55 | + // Registeres every Model in the Users package with the sql connection and ORMConfig |
| 56 | + ORM.register(User.class.getPackage(), sql, config); |
| 57 | + // Adds or updates the database structure |
| 58 | + ORM.autoMigrate(); |
| 59 | + } |
| 60 | + |
| 61 | + protected void setupServer(HTTPServer server) { |
| 62 | + server.beforeInterceptor(exchange -> { |
| 63 | + // Just an attrib example (Passes it through following routes & middlewares) |
| 64 | + exchange.attrib("user", Repo.get(User.class).get(exchange.header("example-user-id"))); |
| 65 | + // Do not intercept |
| 66 | + return false; |
| 67 | + }); |
| 68 | + |
| 69 | + // Adds every Controller in the package of ExampleController and extends HttpController (recursive) |
| 70 | + server.controller(HttpController.class, ExampleController.class.getPackage()); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +``` |
| 77 | + |
| 78 | +```java |
| 79 | +@PathPrefix("/user") |
| 80 | +public class ExampleController extends HttpController { |
| 81 | + @Get("/{id}") |
| 82 | + public String getUser(Exchange exchange, @Attrib("user") User user) { |
| 83 | + return user.name; |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | +```java |
| 88 | +public class User extends Model { |
| 89 | + @Column |
| 90 | + public int id; |
| 91 | + |
| 92 | + @Column |
| 93 | + public String name; |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +- Docs: https://javawebstack.org/framework/ |
| 100 | +- Example Project https://github.com/javawebstack/ |
0 commit comments