https://github.com/amayaframework/amaya-routing
A module of the amaya framework that implements routing of http paths and processing of path and query parameters.
https://github.com/amayaframework/amaya-routing
Last synced: 3 months ago
JSON representation
A module of the amaya framework that implements routing of http paths and processing of path and query parameters.
- Host: GitHub
- URL: https://github.com/amayaframework/amaya-routing
- Owner: AmayaFramework
- License: apache-2.0
- Created: 2024-10-12T09:26:02.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2025-02-18T19:35:46.000Z (4 months ago)
- Last Synced: 2025-02-18T20:34:23.046Z (4 months ago)
- Language: Java
- Homepage:
- Size: 137 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# amaya-routing [](https://repo1.maven.org/maven2/io/github/amayaframework/amaya-routing)
A module of the amaya framework that implements routing of http paths
and processing of path and query parameters.## Getting Started
To install it, you will need:
* Java 11+
* Maven/Gradle
* Amaya Core or set of core modules### Features
* Fast route processing (now without collisions ^_^)
* Static routing
* Dynamic routing
* Path parameter parsing
* Query parameter parsing## Installing
### Gradle dependency
```Groovy
dependencies {
implementation group: 'io.github.amayaframework', name: 'amaya-core', version: '2.0.0'
implementation group: 'io.github.amayaframework', name: 'amaya-routing', version: '1.1.0'
// Optional dependency for built-in dynamic routing impl
implementation group: 'io.github.amayaframework', name: 'amaya-fsm-router', version: '1.0.0'
}
```### Maven dependency
```
io.github.amayaframework
amaya-core
2.0.0io.github.amayaframework
amaya-routing
1.1.0io.github.amayaframework
amaya-fsm-router
1.0.0```
## Examples
### Hello world
```Java
import io.github.amayaframework.core.WebBuilders;
import io.github.amayaframework.http.HttpMethod;public final class Main {
public static void main(String[] args) throws Throwable {
var cfg = RoutingConfigurers.create();
// Configure routes
var paths = cfg.getPathSet();
paths.set(HttpMethod.GET, "/hello", ctx -> {
ctx.getResponse().getWriter().write("Hello from amaya");
});
// Configure app
var builder = WebBuilders.create();
var app = builder
.setServerFactory(/* your web server factory here */)
.configureApplication(cfg)
.build();
app.bind(8080);
app.run();
}
}```
### Dynamic routing with jsm
```Java
import com.github.romanqed.jsm.bytecode.BytecodeMachineFactory;
import io.github.amayaframework.core.WebBuilders;
import io.github.amayaframework.fsm.MachineRouterFactory;
import io.github.amayaframework.http.HttpMethod;
import io.github.amayaframework.routing.RoutingConfigurers;public class Main {
public static void main(String[] args) throws Throwable {
// Prepare fsm and routing factories
var machineFactory = new BytecodeMachineFactory();
var routingFactory = new MachineRouterFactory(machineFactory);
// Create configurer
var cfg = RoutingConfigurers.create(routingFactory);
// Configure filters
var filters = cfg.getFilterSet();
filters.set("int", Integer::parseInt);
filters.set("positive", raw -> {
var ret = Integer.parseInt(raw);
if (ret < 0) {
throw new IllegalArgumentException("Value must be >= 0");
}
return ret;
});
filters.set("word", raw -> {
raw = URLDecoder.decode(raw, StandardCharsets.UTF_8);
raw = raw.strip();
if (raw.contains(" ")) {
throw new IllegalArgumentException("Value must be single world");
}
return raw;
});
// Configure routes
var paths = cfg.getPathSet();
paths.set(HttpMethod.GET, "/a/{p:int}", ctx -> {
ctx.getResponse().getWriter().write("/a/" + ctx.getRequest().getPathParameter("p"));
});
paths.set(HttpMethod.GET, "/a/1", ctx -> {
ctx.getResponse().getWriter().write("/a/1 (static)");
});
paths.set(HttpMethod.GET, "/b/{p:positive}", ctx -> {
ctx.getResponse().getWriter().write("/b/" + ctx.getRequest().getPathParameter("p"));
});
paths.set(HttpMethod.GET, "/word/{w:word}", ctx -> {
ctx.getResponse().getWriter().write("/word/" + ctx.getRequest().getPathParameter("w"));
});
// Configure app
var builder = WebBuilders.create();
var app = builder
.setServerFactory(/* your web server factory here */)
.configureApplication(cfg)
.build();
app.bind(8080);
app.run();
}
}```
## Built With
* [Gradle](https://gradle.org) - Dependency management
* [jfunc](https://github.com/RomanQed/jfunc) - Basic functional interfaces
* [jsm](https://github.com/RomanQed/jsm) - Finite state machine jit compiler
* [amaya-core](https://github.com/AmayaFramework/amaya-core) - Various amaya modules
* Black magic## Authors
* **[RomanQed](https://github.com/RomanQed)** - *Main work*
See also the list of [contributors](https://github.com/AmayaFramework/amaya-jetty/contributors)
who participated in this project.## License
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details