{"id":21389339,"url":"https://github.com/isaced/aoxiang","last_synced_at":"2025-08-13T05:15:53.381Z","repository":{"id":177767581,"uuid":"660254273","full_name":"isaced/Aoxiang","owner":"isaced","description":"A lightweight HTTP server library written in Swift for iOS/macOS/tvOS.","archived":false,"fork":false,"pushed_at":"2023-07-03T17:01:43.000Z","size":41,"stargazers_count":16,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-06T05:41:20.612Z","etag":null,"topics":["http","server","swift","web"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/isaced.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"zenodo":null}},"created_at":"2023-06-29T15:29:18.000Z","updated_at":"2025-01-25T06:56:20.000Z","dependencies_parsed_at":"2023-07-10T20:01:36.178Z","dependency_job_id":null,"html_url":"https://github.com/isaced/Aoxiang","commit_stats":null,"previous_names":["isaced/aoxiang"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/isaced/Aoxiang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaced%2FAoxiang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaced%2FAoxiang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaced%2FAoxiang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaced%2FAoxiang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isaced","download_url":"https://codeload.github.com/isaced/Aoxiang/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isaced%2FAoxiang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270183606,"owners_count":24541341,"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","status":"online","status_checked_at":"2025-08-13T02:00:09.904Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["http","server","swift","web"],"created_at":"2024-11-22T12:26:02.934Z","updated_at":"2025-08-13T05:15:53.357Z","avatar_url":"https://github.com/isaced.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Aoxiang\nAoxiang(翱翔) is a lightweight HTTP server library written in Swift for iOS/macOS/tvOS.\n\n## Features\n\n- Lightweight, Zero-dependency\n- Asynchronous, Non-blocking\n- Support HTTP/1.1\n- Support HTTP chunked transfer encoding (streaming)\n- Support SSE (Server-Sent Events)\n- Middleware support\n- Friendly API, keep it simple and easy to use\n\n## Hello World\n\n```swift\nimport Aoxiang\n\nlet server = HTTPServer()\n\nserver.get(\"/\") { req, res in\n    res.send(\"Hello World!\")\n}\n\ntry server.start(8080)\n```\n\nThen open your browser and visit `http://localhost:8080/` to see the result.\n\n## Basic routing\n\nRouting refers to how an application’s endpoints (URIs) respond to client requests. You define routing using methods of the `HTTPServer` instance that correspond to HTTP methods; for example, `get()` to handle GET requests and `post()` to handle POST requests.\n\nThe following examples illustrate defining simple routes.\n\nRespond with `Hello World!` on the homepage:\n\n```swift\nserver.get(\"/\") { req, res in\n    res.send(\"hello Aoxiang!\")\n}\n```\n\nRespond to POST request on the homepage:\n\n```swift\nserver.post(\"/\") { req, res in\n    res.send(\"Got a POST request\")\n}\n```\n\nRespond to a PUT request to the `/user` route:\n\n```swift\nserver.put(\"/user\") { req, res in\n    res.send(\"Got a PUT request at /user\")\n}\n```\n\nRespond to a DELETE request to the `/user` route:\n\n```swift\nserver.delete(\"/user\") { req, res in\n    res.send(\"Got a DELETE request at /user\")\n}\n```\n\n## Async/Await\n\nAoixang supports `async`/`await` to handle asynchronous operations, it makes your code more readable and easier to maintain.\n\n```swift\nserver.get(\"/async\") { req, res async in\n    let user = await readUserInfo()\n    res.send(user.name)\n}\n```\n\n## Middleware\n\nMiddleware functions are functions that have access to the HTTP request(`req`) and response(`res`) objects, and the next function in the application’s request-response cycle. The next function is a function in the router which, when invoked, executes the middleware succeeding the current middleware.\n\nMiddleware functions can perform the following tasks:\n\n- Execute any code.\n- Make changes to the request and the response objects.\n- End the request-response cycle.\n- Call the next middleware in the stack.\n\nYou can use middleware for many purposes in your web application, for example:\n\n- Logging\n- Authentication\n- Error handling\n- ...\n\nAoxiang supports middleware like [Express](https://expressjs.com/). You can use `use()` to add middleware to your server.\n\nThis example shows a middleware function with no mount path. The function is executed every time the app receives a request.\n\n```swift\nserver.use { req, res, next in\n    print(\"Time: \\(Date())\")\n    next()\n}\n```\n\nAlso you can use `HTTPMiddleware` to implement middleware.\n\n```swift\nclass TimeMiddleware: HTTPMiddleware {\n    override func handle(_ req: HTTPRequest, _ res: HTTPResponse, next: @escaping () -\u003e Void) {\n        print(\"Time: \\(Date())\")\n        next()\n    }\n}\n\nserver.use(TimeMiddleware())\n```\n\n\u003e The order of middleware loading is important: middleware functions that are loaded first are also executed first.\n\n## Chunked streaming\n\nAoxiang supports [HTTP chunked transfer encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding) out of the box. You can use `write()` to send chunked data to client, and use `end()` to end the response.\n\n```swift\nserver.get(\"/stream\") { _, res in\n    res.write(\"hi,\")\n    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {\n        res.write(\"Aoxiang.\")\n        res.end()\n    }\n}\n```\n\n## SSE(Server-Sent Events)\n\nAoxiang supports [SSE(Server-Sent Events)](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) out of the box. You can use `sendEvents()` to get a `EventSource` and send events to client. don't forget to call `close()` to close the connection.\n\n```swift\nserver.get(\"/sse\") { req, res in\n    let target = res.sendEvents()\n    target.dispatchMessage(\"SSE Response:\")\n    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {\n        target.dispatchMessage(\"chunk 1\")\n        target.close()\n    }\n}\n```\n\n## Change response header\n\nAoxiang provides a `headers` property to help you change response header, you can set it before call `send()` or `write()`.\n\nThis example shows how to set a single header:\n\n```swift\nserver.post(\"/send\") { req, res in\n    res.headers[\"Access-Control-Allow-Origin\"] = \"*\"\n    // ...\n}\n```\n\nThis example shows how to set HTTP status code:\n\n```swift\nserver.post(\"/send\") { req, res in\n    res.statusCode = 404\n    // ...\n}\n```\n\n\u003e For more customization, you can check `HTTPResponse` class.\n\n## Installation\n\n### Requirements\n\n- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+\n- Swift 5.4+\n\n### Swift Package Manager\n\n- File \u003e Swift Packages \u003e Add Package Dependency\n- Add `https://github.com/isaced/Aoxiang.git`\n- Select \"Up to Next Major\" with \"1.0.0\"\n\nor add the following to your `Package.swift` file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/isaced/Aoxiang.git\", .upToNextMajor(from: \"1.0.0\"))\n]\n```\n\n## FAQ\n\n### Why named Aoxiang?\n\nAoxiang(翱翔) means \"soar\" in Chinese. I hope this library can help you to build your own web server lightweight and easily.\n\n### res.send() vs res.write()\n\n- `res.write()` is used to send chunked data to client, it will keep the connection alive, until you call `res.end()` manually.\n- `res.send()` is used to send data to client, it will close the connection after sending data automatically.\n\n## Contact\n\nFollow and contact me on [Twitter](https://twitter.com/isacedx). If you find an issue, just [open a ticket](https://github.com/isaced/Aoxiang/issues/new). Pull requests are warmly welcome as well.\n\n## License\n\nAoxiang is released under the MIT license. See LICENSE for details.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaced%2Faoxiang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisaced%2Faoxiang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisaced%2Faoxiang/lists"}