{"id":13800940,"url":"https://github.com/lolgab/snunit","last_synced_at":"2025-04-04T11:15:13.750Z","repository":{"id":37030657,"uuid":"277323123","full_name":"lolgab/snunit","owner":"lolgab","description":"Scala Native HTTP server based on NGINX Unit","archived":false,"fork":false,"pushed_at":"2025-03-27T18:56:27.000Z","size":418,"stargazers_count":137,"open_issues_count":8,"forks_count":7,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-28T10:09:17.365Z","etag":null,"topics":["cask","http","http-server","http4s","nginx-unit","scala","scala-native","serverless","tapir"],"latest_commit_sha":null,"homepage":"https://lolgab.github.io/snunit/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lolgab.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-05T14:54:14.000Z","updated_at":"2025-03-18T14:45:17.000Z","dependencies_parsed_at":"2024-05-01T23:44:13.892Z","dependency_job_id":"80085f34-7ec6-4734-9529-f92cff94f23e","html_url":"https://github.com/lolgab/snunit","commit_stats":{"total_commits":209,"total_committers":5,"mean_commits":41.8,"dds":0.02392344497607657,"last_synced_commit":"dfaea2d338d66c0688324e26c1022abedb83a30b"},"previous_names":[],"tags_count":50,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolgab%2Fsnunit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolgab%2Fsnunit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolgab%2Fsnunit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lolgab%2Fsnunit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lolgab","download_url":"https://codeload.github.com/lolgab/snunit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247166168,"owners_count":20894654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cask","http","http-server","http4s","nginx-unit","scala","scala-native","serverless","tapir"],"created_at":"2024-08-04T00:01:17.815Z","updated_at":"2025-04-04T11:15:13.728Z","avatar_url":"https://github.com/lolgab.png","language":"Scala","readme":"# SNUnit: Scala Native HTTP server based on NGINX Unit\n\n```scala\nimport snunit.*\n\n@main\ndef run =\n  SyncServerBuilder\n    .setRequestHandler(req =\u003e\n      req.send(\n        statusCode = StatusCode.OK,\n        content = \"Hello world!\\n\",\n        headers = Headers(\"Content-Type\" -\u003e \"text/plain\")\n      )\n    )\n    .build()\n    .listen()\n```\n\nSNUnit is a Scala Native library to write HTTP server applications on top of\n[NGINX Unit](https://unit.nginx.org/). It allows you to write both synchronous\nand asynchronous web servers with automatic restart on crashes, automatic\nload balancing of multiple processes, great performance and all the nice\n[NGINX Unit features](http://unit.nginx.org/#key-features).\n\n## Running your app\n\nOnce built your SNUnit binary, you need to deploy it to the `unitd` server.\n\nYou need to run `unitd` in a terminal with:\n\n```bash\nunitd --no-daemon --log /dev/stdout --control unix:control.sock\n```\n\nThis will run `unitd` with a UNIX socket file named control.sock in your current directory.\n\nThen, you need to create a json file with your configuration:\n\n```json\n{\n  \"listeners\": {\n    \"*:8081\": {\n      \"pass\": \"applications/myapp\"\n    }\n  },\n  \"applications\": {\n    \"myapp\": {\n      \"type\": \"external\",\n      \"executable\": \"snunit/binary/path\"\n    }\n  }\n}\n```\n\nWhere `executable` is the binary path which can be absolute or relative\nto the `unitd` working directory.\n\nThis configuration passes all requests sent to the port `8081` to the application `myapp`.\n\nTo know more about configuring NGINX Unit, refer to [its documentation](http://unit.nginx.org/configuration).\n\nTo deploy the setting you can use curl:\n\n```bash\ncurl -X PUT --unix-socket control.sock -d @config.json localhost/config\n```\n\nIf everything went right, you should see this response:\n\n```json\n{\n  \"success\": \"Reconfiguration done.\"\n}\n```\n\nIn case of problems, you will get a 4xx response like this:\n\n```json\n{\n  \"error\": \"Invalid configuration.\",\n  \"detail\": \"Required parameter \\\"executable\\\" is missing.\"\n}\n```\n\nFurther information can be found in `unitd` logs in the running terminal.\n\n## Sync and async support\n\nSNUnit has two different server implementations.\n\nWith `SyncServerBuilder` you need to call `.listen()` to start listening.\nIt is a blocking operation so your process is stuck on listening and can't do\nanything else while listening.\nMoreover, all the request handlers need to respond directly and can't be implemented\nusing `Future`s or any other asyncronous mechanism since no `Future` will run, being\nthe process stuck on the `listen()` Unit event loop.\nWith http4s or tapir-cats-effect the server is automatically scheduled to run either on the\ncats effect event loop, based on epoll/kqueue.\nThis allows you to complete requests asyncronously using whatever mechanism you prefer.\nA process can accept multiple requests concurrently, allowing great parallelism.\n\n## Tapir support\n\nSNUnit offers interpreters for [Tapir](https://tapir.softwaremill.com) server endpoints.\nYou can write all your application using Tapir and the convert your Tapir endpoints\nwith logic into a SNUnit `Handler`.\n\nCurrently two interpreters are available:\n- `SNUnitIdServerInterpreter` which works best with `SyncServerHandler` for synchronous applications\n  - You can find an example [in tests](./integration/tests/tapir-helloworld/src/Main.scala)\n- An interpreter for cats hidden behind `snunit.tapir.SNUnitServerBuilder` in the `snunit-tapir-cats-effect` artifact.\n  - You can find an example [in tests](./integration/tests/tapir-helloworld-cats-effect/src/Main.scala)\n\n### Automatic server creation\n\n`snunit.TapirApp` extends `cats.effect.IOApp` building the SNUnit server.\n\nIt exposes a `def serverEndpoints: Resource[IO, List[ServerEndpoint[Any, IO]]]` that you need to\nimplement with your server logic.\n\nHere an example \"Hello world\" app:\n\n```scala\nimport cats.effect.*\nimport sttp.tapir.*\n\nobject Main extends snunit.TapirApp {\n  def serverEndpoints = Resource.pure(\n    endpoint.get\n      .in(\"hello\")\n      .in(query[String](\"name\"))\n      .out(stringBody)\n      .serverLogic[IO](name =\u003e IO(Right(s\"Hello $name!\"))) :: Nil\n  )\n}\n```\n\n## Http4s support\n\nSNUnit offers a server implementation for [http4s](https://http4s.org).\nIt is based on the [epollcat](https://github.com/armanbilge/epollcat) asynchronous event loop.\n\nThere are two ways you can build a http4s server.\n\n### Automatic server creation\n\n`snunit.Http4sApp` extends `cats.effect.IOApp` building the SNUnit server.\n\nIt exposes a `def routes: Resource[IO, HttpApp[IO]]` that you need to implement with your\nserver logic.\n\nHere an example \"Hello world\" app:\n\n```scala\nimport cats.effect.*\nimport org.http4s.*\nimport org.http4s.dsl.io.*\n\nobject app extends snunit.Http4sApp {\n  def routes = Resource.pure(\n    HttpRoutes\n      .of[IO] { case GET -\u003e Root =\u003e\n        Ok(\"Hello from SNUnit Http4s!\")\n      }\n      .orNotFound\n  )\n}\n```\n\n### Manual server creation\n\nIf you want to have more control over the server creation, you can use the\n`SNUnitServerBuilder` and manually use it.\n\nFor example, here you see it in combination with `cats.effect.IOApp`\n\n```scala\npackage snunit.tests\n\nimport cats.effect.*\nimport org.http4s.*\nimport org.http4s.dsl.io.*\nimport snunit.http4s.*\n\nobject Http4sHelloWorld extends IOApp.Simple {\n  def helloWorldRoutes: HttpRoutes[IO] =\n    HttpRoutes.of[IO] { case GET -\u003e Root =\u003e\n      Ok(\"Hello Http4s!\")\n    }\n\n  def run: IO[Unit] =\n    SNUnitServerBuilder\n      .default[IO]\n      .withHttpApp(helloWorldRoutes.orNotFound)\n      .run\n}\n```\n","funding_links":[],"categories":["网络编程","Web Development"],"sub_categories":["Spring Cloud框架"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flolgab%2Fsnunit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flolgab%2Fsnunit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flolgab%2Fsnunit/lists"}