{"id":13496257,"url":"https://github.com/objcio/swift-talk-backend","last_synced_at":"2025-09-24T01:12:58.667Z","repository":{"id":50223982,"uuid":"142851371","full_name":"objcio/swift-talk-backend","owner":"objcio","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-01T10:32:24.000Z","size":115407,"stargazers_count":287,"open_issues_count":17,"forks_count":12,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-09-16T06:22:50.381Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/objcio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2018-07-30T09:03:30.000Z","updated_at":"2025-01-11T19:44:12.000Z","dependencies_parsed_at":"2024-10-31T11:41:17.310Z","dependency_job_id":null,"html_url":"https://github.com/objcio/swift-talk-backend","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/objcio/swift-talk-backend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/objcio%2Fswift-talk-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/objcio%2Fswift-talk-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/objcio%2Fswift-talk-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/objcio%2Fswift-talk-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/objcio","download_url":"https://codeload.github.com/objcio/swift-talk-backend/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/objcio%2Fswift-talk-backend/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276674837,"owners_count":25684151,"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-09-23T02:00:09.130Z","response_time":73,"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":[],"created_at":"2024-07-31T19:01:44.658Z","updated_at":"2025-09-24T01:12:58.628Z","avatar_url":"https://github.com/objcio.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# Swift Talk Backend\n\nThis is the source code of the Swift Talk backend: [https://talk.objc.io](https://talk.objc.io)\n\nWhile we abstracted non-app-specific parts away into frameworks, this is not a web framework. Here's a minimal description of the structure:\n\n## SwiftTalkServerLib\n\nThis framework contains the application-specific code. There are a few main parts to it:\n\n- The *routes* define the available endpoints and transform URLs to routes and routes to URLs\n- The *interpret* methods contain the application logic for each route\n- The *views* contain rendering logic\n- The *queries* abstract away the database (but only a little bit)\n- The *third-party services* communicate with JSON and XML APIs of third-party providers\n\n### Interpreting\n\nFor testability (and because we wanted to experiment), we wrote our route interpreter using the [final-tagless](https://talk.objc.io/episodes/S01E89-extensible-libraries-2-protocol-composition) style. This allows us to write a normal interpreter that does the usual web-server things: execute database queries, perform network requests, etc. It also allows us to have a [test interpreter](/Tests/swifttalkTests/TestHelpers.swift), so that we can write high-level flow tests (with [easy network tests](https://talk.objc.io/episodes/S01E137-testing-networking-code)).\n\n### Database\n\nWe use PostgreSQL and write standard SQL queries to access the database. We represent each table with a struct and use Codable to help generate simple queries and to parse the results from PostgreSQL back into struct values ([Episode #114](https://talk.objc.io/episodes/S01E114-reflection-with-mirror-and-decodable)).\n\n### Third-Party Services\n\nRather than depending on third-party frameworks, we decided to write our own wrappers around the REST endpoints of third-party services (e.g. GitHub, Recurly, Sendgrid, Vimeo) using our [tiny networking](https://talk.objc.io/episodes/S01E133-tiny-networking-library-revisited) library.\n\n## HTML\n\nThe HTML framework defines an enum to represent HTML/XML nodes. There is one special feature: a `Node` is generic over some read-only state. This allows us to pass around \"global\" state like a CSRF token and session/user data without actually making that global, and without having to explicitly pass it around everywhere.\n\nFor an example, see [HTMLExtensions.swift](/Sources/SwiftTalkServerLib/Views/HTMLExtensions.swift). We add multiple extension to our `Node` type when the read-only state is of type `STRequestEnvironment`.\n\n## Routing\n\nFor routing, we use a [`Router` struct](/Sources/Routing/Routing.swift#L49) that captures both *parsing* and *generating* a route in one. [Our routes](/Sources/SwiftTalkServerLib/Routes.swift#L13) are defined as enums, and using the `Router` we can write one description that converts the case into a URL and parses a URL, without having too worry too much about keeping them in sync.\n\nWe also use the enum cases to generate links, making sure that every link is well-formed and has all the necessary parameters.\n\n## Incremental\n\nWe use our [Incremental programming library](https://talk.objc.io/collections/incremental-programming) to transform and cache static data. For example, when the markdown file for an episode is changed, we recompute the highlighted version (highlighting is done using a `SourceKitten` wrapper). Because this can take a little while, the results are cached.\n\n## NIOWrapper\n\nThis framework is a lightweight wrapper around SwiftNIO, which contains a few primitives to write data, send redirects, process POST data, etc. The wrapper depends only minimally on NIO, which makes it easy to test without NIO.\n\n## WebServer\n\nThe WebServer framework builds on top of the NIOWrapper, providing some higher level abstractions e.g. to write HTML or JSON responses. It also integrates the with the Database and Networking frameworks to provide response APIs to execute queries or call third-party network endpoints.\n\n# Installation Notes\n\n### Dependencies\n\nIf you want to run this locally (without Docker), you need to install the following dependencies:\n\n- postgresql\n- libpq-dev\n- cmake\n- cmark\n- curl\n\n### PostgreSQL\n\nYou need PostgreSQL and libpq. To set up a local postgres instance:\n\n```sh\ninitdb -D .postgres\nchmod 700 .postgres\npg_ctl -D .postgres start\ncreatedb swifttalk_dev\n```\n\n### Compiling Assets\n\nTo build the stylesheets:\n\n```sh\n./build-css.sh\n```\n\n### Deployment\n\nWe deploy to a heroku-based docker app (needs postgres as well).\n\nIf you get a \"basic auth\" error: `heroku container:login`\n\n```sh\nheroku container:push web\nheroku container:release web\n```\n\n### Running in Docker\n\nFor the docker container to be able to access PostgreSQL on the host, you have to allow access via TCP/IP.\nAdd `host all all 0.0.0.0/0 trust` to pg_hba.conf (this opens up the PostgreSQL instance to everybody in your network, use something more finegrained if that's a problem) and add `listen_addresses = '*'` to postgresql.conf.\n\n```sh\ndocker run -a stdin -a stdout -i -t --env-file .env --env RDS_HOSTNAME=(ifconfig en1 | awk '/inet /{print $2}') -p 8765:8765 swifttalk-server\n```\n\nYou could also set up a multi-container docker application. For example, like in [this pull request](https://github.com/objcio/swift-talk-backend/pull/99/files).\n\n\n### Debugging Linux Bugs\n\nYou can run a docker container from one of the intermediate steps. Then install screen and vim, and you have a small linux dev environment.\n\nhttps://medium.com/ihme-tech/troubleshooting-the-docker-build-process-454583c80665\n\nHere are some steps, if you have a local postgres running (on `en1`):\n\n```sh\ndocker build -t \"swifttalk-server-debug\" -f Dockerfile.debug .\ndocker run  --cap-add=SYS_PTRACE --security-opt seccomp=unconfined --security-opt apparmor=unconfined -a stdin -a stdout -i -t --env-file .env --env RDS_HOSTNAME=(ifconfig en1 | awk '/inet /{print $2}') -p 8765:8765 swifttalk-server-debug bash\nscreen # this helps with having separate \"windows\"\nswift build --configuration debug -Xswiftc -g\n./build/debug/swifttalk-server \u0026\u003e output.log\n# Wait for a crash to happen\n ./symbolicate-linux-fatal .build/debug/swifttalk-server output.log\n```\n\nUnfortunately, it doesn't seem like lldb is expected to work under Linux: https://bugs.swift.org/browse/SR-755\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobjcio%2Fswift-talk-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fobjcio%2Fswift-talk-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobjcio%2Fswift-talk-backend/lists"}