• Stars
    star
    287
  • Rank 144,232 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 6 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift Talk Backend

This is the source code of the Swift Talk backend: https://talk.objc.io

While we abstracted non-app-specific parts away into frameworks, this is not a web framework. Here's a minimal description of the structure:

SwiftTalkServerLib

This framework contains the application-specific code. There are a few main parts to it:

  • The routes define the available endpoints and transform URLs to routes and routes to URLs
  • The interpret methods contain the application logic for each route
  • The views contain rendering logic
  • The queries abstract away the database (but only a little bit)
  • The third-party services communicate with JSON and XML APIs of third-party providers

Interpreting

For testability (and because we wanted to experiment), we wrote our route interpreter using the final-tagless 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, so that we can write high-level flow tests (with easy network tests).

Database

We 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).

Third-Party Services

Rather 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 library.

HTML

The 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.

For an example, see HTMLExtensions.swift. We add multiple extension to our Node type when the read-only state is of type STRequestEnvironment.

Routing

For routing, we use a Router struct that captures both parsing and generating a route in one. Our routes 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.

We also use the enum cases to generate links, making sure that every link is well-formed and has all the necessary parameters.

Incremental

We use our Incremental programming library 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.

NIOWrapper

This 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.

WebServer

The 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.

Installation Notes

Dependencies

If you want to run this locally (without Docker), you need to install the following dependencies:

  • postgresql
  • libpq-dev
  • cmake
  • cmark
  • curl

PostgreSQL

You need PostgreSQL and libpq. To set up a local postgres instance:

initdb -D .postgres
chmod 700 .postgres
pg_ctl -D .postgres start
createdb swifttalk_dev

Compiling Assets

To build the stylesheets:

./build-css.sh

Deployment

We deploy to a heroku-based docker app (needs postgres as well).

If you get a "basic auth" error: heroku container:login

heroku container:push web
heroku container:release web

Running in Docker

For the docker container to be able to access PostgreSQL on the host, you have to allow access via TCP/IP. Add 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.

docker run -a stdin -a stdout -i -t --env-file .env --env RDS_HOSTNAME=(ifconfig en1 | awk '/inet /{print $2}') -p 8765:8765 swifttalk-server

You could also set up a multi-container docker application. For example, like in this pull request.

Debugging Linux Bugs

You can run a docker container from one of the intermediate steps. Then install screen and vim, and you have a small linux dev environment.

https://medium.com/ihme-tech/troubleshooting-the-docker-build-process-454583c80665

Here are some steps, if you have a local postgres running (on en1):

docker build -t "swifttalk-server-debug" -f Dockerfile.debug .
docker 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
screen # this helps with having separate "windows"
swift build --configuration debug -Xswiftc -g
./build/debug/swifttalk-server &> output.log
# Wait for a crash to happen
 ./symbolicate-linux-fatal .build/debug/swifttalk-server output.log

Unfortunately, it doesn't seem like lldb is expected to work under Linux: https://bugs.swift.org/browse/SR-755

More Repositories

1

app-architecture

Sample Code of the App Architecture Book
Swift
2,021
star
2

functional-swift

Issue repository for the Functional Swift book
Swift
927
star
3

issue-13-viper

Objective-C
846
star
4

core-data

Sample code for the objc.io Core Data book
Swift
769
star
5

issue-1-lighter-view-controllers

Example project for the 1st issue of objc.io
Objective-C
740
star
6

articles

All current objc.io articles
593
star
7

markdown-playgrounds

A Markdown Editor that can execute Swift code
Swift
575
star
8

thinking-in-swiftui-sample-code

Sample code for the Thinking in SwiftUI book
Swift
318
star
9

tiny-networking

Tiny Networking Library
Swift
257
star
10

OptimizingCollections

Home of my talk about Optimizing Collections in Swift
Swift
233
star
11

issue-21-core-image-explorer

Sample code for the Introduction to Core Image article in objc.io issue #21
Swift
216
star
12

issue-3-collection-view-layouts

Objective-C
194
star
13

issue-4-full-core-data-application

Objective-C
164
star
14

issue5-view-controller-transitions

Simple demo app showing custom view controller transitions
Objective-C
124
star
15

attributed-string-builder

Attributed String Builders
Swift
108
star
16

issue-2-background-core-data

Objective-C
89
star
17

issue-10-core-data-network-application

Sample project for objc.io article http://www.objc.io/issue-10/networked-core-data-application.html
Objective-C
88
star
18

issue-7-lab-color-space-explorer

Objective-C
74
star
19

issue-12-custom-layer-property-animations

Issue 12: Custom Layer Property Animations
Objective-C
66
star
20

issue-3-auto-layout-debugging

Objective-C
62
star
21

issue-14-xpc

Sample Project for objc.o Article about XPC
Swift
55
star
22

LedgerGUI

Swift
55
star
23

issue-12-interactive-animations-pop

Objective-C
50
star
24

S01E51-playground-driven-development-at-kickstarter

Sample code for Swift Talk episode 51: Playground-Driven Development at Kickstarter
Swift
48
star
25

S01E01-networking

Sample code for Swift Talk episode 1: Networking
Swift
46
star
26

issue-12-interactive-animations-uidynamics

Interactive Animations with UIDynamics
Objective-C
45
star
27

swift-talk-app-swiftui

Swift Talk App using SwiftUI
Swift
45
star
28

swiftui-challenges

SwiftUI challenges
41
star
29

issue-12-interactive-animations

Interactive Animations on iOS
Objective-C
41
star
30

issue-2-background-networking

Objective-C
40
star
31

issue-4-importing-and-fetching

Objective-C
39
star
32

keychain-item

Keychain Item Property Wrapper
Swift
38
star
33

StaticSite

Simple and blunt static site generator
Swift
37
star
34

issue-2-background-file-io

Objective-C
34
star
35

metal-demo-objcio

Sample code for Issue #18 of objc.io
Objective-C
33
star
36

issue-7-contact-editor

Objective-C
30
star
37

issue-8-quadcopter-navigator

Objective-C
30
star
38

S01E34-reactive-programming

Sample code for Swift Talk episode 34: Reactive Programming
Swift
25
star
39

issue-14-scriptable-apps

Objective-C
24
star
40

thinking-in-swiftui-code

Accompanying code for the book Thinking in SwiftUI
24
star
41

S01E03-loading-view-controllers

Sample code for Swift Talk episode 3
Swift
23
star
42

advanced-swift

Feedback and issue-tracker for objc.io's Advanced Swift book
23
star
43

S01E53-test-driven-reactive-programming-at-kickstarer

Sample code for Swift Talk episode 53: Test-Driven Reactive Programming at Kickstarter
Swift
21
star
44

S01E90-concurrent-map

Sample code for Swift Talk episode 90: Concurrent Map
Swift
19
star
45

S01E25-network-caching

Sample code for Swift Talk episode 25: Network Caching
Swift
18
star
46

issue-7-linguistic-tagging

Objective-C
18
star
47

S01E05-connecting-view-controllers

Sample code for Swift Talk Episode #5: Connecting View Controllers
Swift
17
star
48

S01E06-generic-table-view-controllers

Sample code for Swift Talk Episode #6: Generic Table View Controllers
Swift
16
star
49

issue-6-compiler-tool

Example code for a standalone clang/llvm tool.
C++
16
star
50

S01E26-generic-table-view-controllers-part-2

Sample code for Swift Talk episode 26: Generic Table View Controllers (Part 2)
Swift
15
star
51

collection-view-swiftui

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
15
star
52

thinking-in-swiftui-qa

15
star
53

issue-16-functional-apis

Example playground for the article http://www.objc.io/issue16/functional-swift-apis.html
Swift
13
star
54

s01e47-testable-view-models-at-kickstarter

Sample code for Swift Talk episode 47: View Models at Kickstarter
Swift
12
star
55

swift-talk-shared

Swift
11
star
56

issue-12-interactive-animations-osx

Objective-C
11
star
57

S01E57-certificate-pinning

Sample code for Swift Talk episode 57: Certificate Pinning
Swift
10
star
58

S01E43-view-controller-refactoring

Swift
10
star
59

S01E02-rendering-commonmark

Sample code for Swift Talk episode 2: Rendering CommonMark
Swift
10
star
60

S01E62-testable-view-controllers-with-reducers

Sample code for Swift Talk episode 62: Testable View Controllers with Reducers
Swift
10
star
61

S01E145-setting-up-a-document-based-app

Sample code for Swift Talk episode 145: Markdown Playgrounds — Setting Up a Document Based App
Swift
10
star
62

issue-9-string-parsing

Objective-C
9
star
63

S01E75-auto-layout-with-key-paths

Sample code for Swift Talk episode 75: Auto Layout with Key Paths
Swift
9
star
64

S01E49-deeplinking-at-kickstarter

Swift
8
star
65

S01E133-tiny-networking-library-revisited

Sample code for Swift Talk episode 133: Networking: Tiny Networking Library Revisited
Swift
8
star
66

S01E174-animation-curves

Sample code for Swift Talk episode 174: SwiftUI — Animation Curves
Swift
7
star
67

S01E07-stack-views-with-enums

Sample code for Swift Talk episode #7: Stack Views with Enums
Swift
7
star
68

issue-14-plugins

Objective-C
7
star
69

S01E117-showing-hiding-sections

Sample code for Swift Talk episode 117: Building a Form Library: Showing & Hiding Sections
Swift
7
star
70

S01E08-networking-post-requests

Sample code for Swift Talk episode 8: Networking: POST Requests
Swift
7
star
71

S01E27-typed-notifications-part-1

Sample code for Swift Talk episode 27: Typed Notifications (Part 1)
Swift
6
star
72

DynamicLayout

Swift
6
star
73

S01E109-ios-remote-debugger-connecting-with-bonjour

Sample code for Swift Talk episode 109: iOS Remote Debugger: Connecting with Bonjour
Swift
6
star
74

S01E253-flow-layout-revisited

Sample code for Swift Talk 253: SwiftUI – Flow Layout Revisited
Swift
6
star
75

S01E225-swiftui-layout-explained-view-protocols-and-shapes

Sample code for Swift Talk 225: SwiftUI Layout Explained – View Protocols and Shapes
Swift
6
star
76

S01E333-sticky-headers-for-scroll-views

Sample code for Swift Talk 333: Sticky Headers for Scroll Views
Swift
6
star
77

S01E73-view-bindings-in-pure-swift

Sample code for Swift Talk episode 73: View Bindings in Pure Swift
Swift
6
star
78

S01E71-type-safe-file-paths-with-phantom-types

Sample code for Swift Talk episode 71: Type-Safe File Paths with Phantom Types
Swift
5
star
79

S01E66-the-elm-architecture-part-1

Sample code for Swift Talk episode 66: The Elm Architecture (Part 1)
Swift
5
star
80

S01E45-server-side-swift-routing

Swift
5
star
81

S01E28-typed-notifications-part-2

Sample code for Swift Talk episode 28: Typed Notifications (Part 2)
Swift
5
star
82

retro-rampage

Swift
5
star
83

S01E235-swiftui-layout-explained-layout-priorities

Sample code for Swift Talk 235: SwiftUI Layout Explained – Layout Priorities
Swift
5
star
84

S01E19-from-runtime-programming-to-functions

Sample code for Swift Talk episode 19: From Runtime Programming to Functions
Swift
5
star
85

S01E04-rendering-commonmark-part-2

Swift Talk Episode 4: Rendering CommonMark (Part 2)
Swift
5
star
86

S01E175-building-a-shopping-cart-animation

Sample code for Swift Talk episode 175: SwiftUI — Building a Shopping Cart Animation
Swift
5
star
87

libpq

Swift
4
star
88

S01E29-protocols-class-hierarchies

Sample code for Swift Talk episode 29: Protocols & Class Hierarchies
Swift
4
star
89

S01E156-a-first-look-at-swiftui

Sample code for Swift Talk episode 156: SwiftUI — A First Look
Swift
4
star
90

S01E149-swift-syntax-highlighting

Sample code for Swift Talk episode 149: Markdown Playgrounds — Swift Syntax Highlighting
Swift
4
star
91

S01E179-building-a-shopping-cart-cleanup-and-refactoring

Sample code for Swift Talk episode 179: SwiftUI — Building a Shopping Cart — Cleanup & Refactoring
Swift
4
star
92

S01E229-swiftui-layout-explained-flexible-frames

Sample code for Swift Talk 229: SwiftUI Layout Explained – Flexible Frames
Swift
4
star
93

S01E86-sharing-state-between-view-controllers-in-mvc-part-1

Sample code for Swift Talk episode 86: Sharing State Between View Controllers in MVC (Part 1)
Swift
4
star
94

S01E168-building-a-collection-view-part-2

Sample code for Swift Talk episode 168: SwiftUI — Building a Collection View (Part 2)
Swift
4
star
95

S01E406-detecting-visible-cells

Sample code for Swift Talk 406: Detecting Visible Cells
Swift
4
star
96

S01E36-futures

Sample Code for Swift Talk Episode 36: Futures
Swift
4
star
97

S01E211-quick-open-simple-fuzzy-matching

Sample code for Swift Talk 211: Quick Open — Simple Fuzzy Matching
Swift
4
star
98

S01E183-lap-times

Sample code for Swift Talk episode 183: SwiftUI Stopwatch — Lap Times
Swift
3
star
99

S01E137-testing-networking-code

Sample code for Swift Talk episode 136: Networking — Testing Networking Code
Swift
3
star
100

S01E63-mutable-shared-structs-part-2

Sample code for Swift Talk episode 63: Mutable Shared Structs (Part 2)
Swift
3
star