https://github.com/marcinzh/enterprise
Serve HTTP using Algebraic Effects and Handlers
https://github.com/marcinzh/enterprise
algebraic-effects functional-programming http-server scala
Last synced: 13 days ago
JSON representation
Serve HTTP using Algebraic Effects and Handlers
- Host: GitHub
- URL: https://github.com/marcinzh/enterprise
- Owner: marcinzh
- License: mit
- Created: 2023-07-25T20:20:08.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-08-07T10:50:32.000Z (8 months ago)
- Last Synced: 2025-08-07T11:14:13.140Z (8 months ago)
- Topics: algebraic-effects, functional-programming, http-server, scala
- Language: Scala
- Homepage:
- Size: 79.1 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://maven-badges.herokuapp.com/maven-central/io.github.marcinzh/enterprise-core_3) [](https://javadoc.io/doc/io.github.marcinzh/enterprise-core_3)
# Enterprise
Minimalist library for creating HTTP services, using algebraic effects and handlers.
- βοΈ π¬ π§ͺ Β π·πΉπΆπ»πΆπ»ππ·π¬ Β π§ WIP π§
- Uses Scala 3.
- Uses [Turbolift](https://marcinzh.github.io/turbolift/) as effect system.
- Adapts parts of design of preexisting purely-functional HTTP libraries ([Http4s](https://github.com/http4s/http4s), [ZIO-Http](https://github.com/zio/zio-http)) for the new effect system.
## Design
Services are defined as values of type:
```scala
type Service[U] = Response[U] !! (RequestEffect & U)
````
Which should be read as: "Computation, that **returns** `Response` and **requests effects**: `RequestEffect` and `U`".
Where `U` means user selected set of effects involved in computing the response.
`RequestEffect` is an instance of Turbolift's `Reader` effect, predefined in Enterprise.
Examples of ptional effects, predefined in Enterprise, are:
- `ResponseError` - An instance of Turbolift's `Error` effect. Allows interruption of processing of the request, returning given `Response` value.
- `RouterEffect` - An instance of Turbolift's `Choice` effect. Allows defining routes by partial functions. Composition can be done with Turbolift's `++!` operator (similar to `<|>` of `Alternative`).
Such effects can be handled at:
- Request scope: before `.serve` call (i.e. once per each served request).
- Server scope: after `.serve` call (i.e. once per server launch).
Multiple handlers can be chained, using Turbolift's `&&&!` operator.
## Example
Run the server with `scala-cli`:
> [!IMPORTANT]
> Turbolift requires **Java 11** or newer.
```scala
//> using scala "3.3.7"
//> using dep "io.github.marcinzh::enterprise-core:0.12.0"
package examples
import turbolift.!!
import turbolift.Extensions._
import enterprise.{Response, Router}
import enterprise.DSL._
import enterprise.server.Syntax._
import enterprise.server.undertow.UndertowServer
@main def main =
Router:
case GET / "sarcastic" / text =>
val text2 = text.zipWithIndex.map((c, i) => if i % 2 == 0 then c.toLower else c.toUpper).mkString
Response.text(text2).pure_!!
.handleWith(Router.handler)
.serve
.handleWith(UndertowServer.handler)
.handleWith(Config.localhost(9000).handler)
.runIO
```
Query the server with `HTTPie`:
```bash
http GET "http://localhost:9000/sarcastic/Resistance is futile"
```
---
See [examples folder](./modules/examples/src/main/scala/examples/) for more.