https://github.com/renatompf/ember-project
The Ember project is a lightweight Java-based web framework designed for building HTTP-based applications with features like routing, middleware chaining, and request/response abstraction.
https://github.com/renatompf/ember-project
framework java rest web
Last synced: 2 months ago
JSON representation
The Ember project is a lightweight Java-based web framework designed for building HTTP-based applications with features like routing, middleware chaining, and request/response abstraction.
- Host: GitHub
- URL: https://github.com/renatompf/ember-project
- Owner: renatompf
- License: mit
- Created: 2025-04-20T13:35:38.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-05-08T20:32:10.000Z (about 1 year ago)
- Last Synced: 2025-05-08T21:27:25.482Z (about 1 year ago)
- Topics: framework, java, rest, web
- Language: Java
- Homepage:
- Size: 188 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ember Framework
## Overview
Ember Framework is a lightweight Java web framework designed for building RESTful APIs with simplicity and flexibility. It provides features like dependency injection, middleware support, request validation, and annotation-based development to streamline the creation of modern web applications.
## Features
- **Routing**: Flexible routing with support for path and query parameters.
- **Dependency Injection**: Built-in DI container for managing services and controllers.
- **Middleware**: Global and route-specific middleware for request handling.
- **Annotations**: Simplified development with custom annotations (`@Controller`, `@Service`, `@PathParameter`, etc.).
## Architecture
```mermaid
flowchart TB
G[Server] --> H[Incoming Request]
H --> I[Context]
I --> D[MiddlewareChain]
A[EmberApplication] --> |configures| G
A --> B[DIContainer]
B --> |registers| C[Router]
B --> |manages| Services[Services]
B --> |manages| Controllers[Controllers]
B --> |manages| Handlers[GlobalHandlers]
E[Middleware] --> D
C --> |provides| D
D --> P[ParameterResolver]
P --> F[Route Handler]
F --> V[ValidationManager]
V --> R[Response]
F --> R
R --> RH[ResponseHandler]
RH --> |serializes based on
content type| J[Final Response]
Handlers --> K[Exception Handling]
K --> J
%% Add notes/descriptions
A --- Note1[Entry point]
B --- Note2[Registers services/controllers
Handles constructor injection]
C --- Note3[Path + method to handler]
G --- Note4[Starts and handles HttpServer]
I --- Note5[Holds request state, response, etc.]
K --- Note6[Handles exceptions using Handles methods]
RH --- Note7[Handles content negotiation
and serialization]
P --- Note8[Resolves method parameters
body, query, path, etc. ]
V --- Note9[Validates request data
using Jakarta Validation]
%% Styling
classDef note fill:#533,stroke:#533,stroke-width:2px
class Note1,Note2,Note3,Note4,Note5,Note6,Note7,Note8,Note9 note
```
## Example
```java
import io.github.renatompf.ember.annotations.middleware.WithMiddleware;
import io.github.renatompf.ember.core.server.Context;
import io.github.renatompf.ember.core.server.Middleware;
// Declare middleware for authentication
public class AuthMiddleware implements Middleware {
@Override
public void handle(Context context) throws Exception {
String token = context.headers().header("Authorization");
if (token == null || !token.equals("valid-token")) {
throw new HttpException(HttpStatusCode.UNAUTHORIZED, "Invalid or missing token");
}
}
}
// Declare middleware for logging
public class LoggingMiddleware implements Middleware {
@Override
public void handle(Context context) {
System.out.println("Request received: " + context.getPath());
}
}
// Controller using middleware
@Controller("/secure")
@WithMiddleware({AuthMiddleware.class}) // Applied to all methods in this controller
public class SecureController {
@Get("/data")
public Response getData() {
return Response.ok().body("Secure data accessed successfully!").build();
}
@Get("/validate")
@WithMiddleware(LoggingMiddleware.class)
public Response validateAndGetData() {
return Response.ok().body("Validation passed, secure data accessed!").build();
}
}
```
Access the secure endpoint at `http://localhost:8080/secure/data` and `http://localhost:8080/secure/validate` with a valid token in the header.
## Documentation
### Annotations
- `@Controller`: Marks a class as a controller.
- `@Service`: Marks a class as a service.
- `@Get`, `@Post`, `@Put`, ...: Maps HTTP methods to controller methods.
- `@PathParameter`: Binds a path parameter to a method argument.
- `@QueryParameter`: Binds a query parameter to a method argument.
- `@RequestBody`: Binds the request body to a method argument.
- `@WithMiddleware`: Applies middleware to a controller or method.
- `@GlobalHandler`: Marks class as a global exception handler.
- `@Handles`: Marks a method to handle a specific exception.
- `@Consumes`: Specifies the content type that a method can consume.
- `@Produces`: Specifies the content type that a method can produce.
- `@Validated`: Marks an object for validation using [Jakarta Validation](https://beanvalidation.org/).
### Middleware
Use `@WithMiddleware` to apply middleware globally or to specific routes.
## Contributing
Contributions are welcome! Please follow these steps:
1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Submit a pull request with a detailed description of your changes.
## License
This project is licensed under the [MIT License](LICENSE).