All Projects → lolgab → snunit

lolgab / snunit

Licence: Apache-2.0 license
Scala Native HTTP server based on NGINX Unit

Programming Languages

scala
5932 projects
shell
77523 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to snunit

backend-server
📠 The backend of the Fairfield Programming Association website.
Stars: ✭ 26 (-70.11%)
Mutual labels:  http-server
http-server-pwa
👾 http-server alike but for serving and rendering PWA: pwa-server
Stars: ✭ 14 (-83.91%)
Mutual labels:  http-server
yams
YAMS: Awesome MIPS Server
Stars: ✭ 17 (-80.46%)
Mutual labels:  http-server
phorklift
Phorklift is an HTTP server and proxy daemon, with clear, powerful and dynamic configuration.
Stars: ✭ 43 (-50.57%)
Mutual labels:  http-server
http4ts
Server as a Function http toolkit for TypeScript & JavaScript
Stars: ✭ 30 (-65.52%)
Mutual labels:  http-server
tinyweb
Simple and lightweight HTTP async server for micropython
Stars: ✭ 182 (+109.2%)
Mutual labels:  http-server
WebServer
High-performance multi-threaded tcp network server in c++11
Stars: ✭ 58 (-33.33%)
Mutual labels:  http-server
minimal-http-server
Basic http server
Stars: ✭ 26 (-70.11%)
Mutual labels:  http-server
fancy
High performance web server
Stars: ✭ 20 (-77.01%)
Mutual labels:  http-server
smartacus-mqtt-broker
smartacus-mqtt-broker is a Java-based open source MQTT broker that fully supports MQTT 3.x .Using Netty 4.1.37
Stars: ✭ 25 (-71.26%)
Mutual labels:  http-server
quiki
a file-based web engine and server featuring a productive source language, markdown, image generation, categories, templates, and revision tracking
Stars: ✭ 20 (-77.01%)
Mutual labels:  http-server
squilu
A scripting language that accepts a subset of javascript and C/C++
Stars: ✭ 69 (-20.69%)
Mutual labels:  http-server
sheret
A tiny, simple static file web server.
Stars: ✭ 45 (-48.28%)
Mutual labels:  http-server
mirror-http-server
A dummy HTTP server that responds whatever you told it to
Stars: ✭ 35 (-59.77%)
Mutual labels:  http-server
aqua
A minimal and fast 🏃 web framework for Deno
Stars: ✭ 219 (+151.72%)
Mutual labels:  http-server
Jarvis
APL-based web service framework supporting JSON or REST
Stars: ✭ 17 (-80.46%)
Mutual labels:  http-server
lighttpd-Link
A lighttpd powered lightweight web server for the Steam Link
Stars: ✭ 21 (-75.86%)
Mutual labels:  http-server
python-simple-http-server
A super light way HTTP server written by python, support websocket and coroutine.
Stars: ✭ 26 (-70.11%)
Mutual labels:  http-server
go-import-server
HTTP server for canonical "go get" import path
Stars: ✭ 29 (-66.67%)
Mutual labels:  http-server
b23.wtf
Remove tracing parameters from b23.tv/*.
Stars: ✭ 85 (-2.3%)
Mutual labels:  http-server

SNUnit: Scala Native HTTP server based on NGINX Unit

import snunit._
object HelloWorldExample {
  def main(args: Array[String]): Unit = {
    val server = SyncServerBuilder()
      .withRequestHandler(req =>
        req.send(
          statusCode = StatusCode.OK,
          content = s"Hello world!\n",
          headers = Seq("Content-Type" -> "text/plain")
        )
      )
      .build()

    server.listen()
  }
}

SNUnit is a Scala Native library to write HTTP server applications on top of NGINX Unit. It allows you to write both synchronous and asynchronous web servers with automatic restart on crashes, automatic load balancing of multiple processes, great performance and all the nice NGINX Unit features.

Running your app

Once built your SNUnit binary, you need to deploy it to the unitd server.

You need to run unitd in a terminal with:

unitd --no-daemon --log /dev/stdout --control unix:control.sock

This will run unitd with a UNIX socket file named control.sock in your current directory.

Then, you need to create a json file with your configuration:

{
  "listeners": {
    "*:8081": {
      "pass": "applications/myapp"
    }
  },
  "applications": {
    "myapp": {
      "type": "external",
      "executable": "snunit/binary/path"
    }
  }
}

Where executable is the binary path which can be absolute or relative to the unitd working directory.

This configuration passes all requests sent to the port 8081 to the application myapp.

To know more about configuring NGINX Unit, refer to its documentation.

To deploy the setting you can use curl:

curl -X PUT --unix-socket control.sock -d @config.json localhost/config

If everything went right, you should see this response:

{
  "success": "Reconfiguration done."
}

In case of problems, you will get a 4xx response like this:

{
  "error": "Invalid configuration.",
  "detail": "Required parameter \"executable\" is missing."
}

Further informations can be found in unitd logs in the running terminal.

Sync and async support

SNUnit has two different server implementations.

With SyncServerBuilder you need to call .listen() to start listening. It is a blocking operation so your process is stuck on listening and can't do anything else while listening. Moreover, all the request handlers need to respond directly and can't be implemented using Futures or any other asyncronous mechanism since no Future will run, being the process stuck on the listen() Unit event loop. With AsyncServerBuilder the server is automatically scheduled to run either on the scala-native-loop event loop (based on the libuv library) or epollcat event loop, based on epoll/kqueue. This allows you to complete requests asyncronously using whatever mechanism you prefer. A process can accept multiple requests concurrently, allowing great parallelism. Add either snunit-async-loop or snunit-async-epollcat to decide what implementation you want to use.

Tapir support

SNUnit offers interpreters for Tapir server endpoints. You can write all your application using Tapir and the convert your Tapir endpoints with logic into a SNUnit Handler.

Currently two interpreters are available:

  • SNUnitIdServerInterpreter which works best with SyncServerHandler for synchronous applications
  • SNUnitFutureServerInterpreter which requires AsyncServerHandler for asynchronous applications
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].