Broader Frontends
Author : Kazuhiro Hara
Article permalink

Starting to Get Deep into Vapor, a Framework Made with Swift

vapor-new-project

As I wrote a little while ago, I have started touching Vapor on WSL. Swift feels quite different from TypeScript, so coding in this different style is fun.

Vapor is a web application framework made with Swift. There are several others too, but after trying various options, this one fit me best.

As I build apps for Vision Pro, the more time I spend touching Swift, the better. However, although I have a desktop Mac, I do not have a laptop Mac. Strictly speaking, I do have a 12-inch MacBook, but it is an Intel Mac, and even launching Xcode normally is heavy on it.

That means using Swift on the Windows machine I carry every day. Since I am doing that anyway, I started to feel like I might as well learn a new technology along the way and maybe even build one site with a Swift backend.

That is why I chose Vapor. If I keep getting into it, finish the site, and still have not lost interest, maybe I can buy a MacBook.

Install

Even before installing Vapor, you first need to install Swift.

Using that as a reference, I started by installing swiftly.

After installation, install the latest Swift version.

$ swiftly install latest
$ swift -v
Swift version 5.10 (swift-5.10-RELEASE)

Next, install Vapor. It seems you need to install a CLI called Toolbox. I checked out 18.7.5, which was the latest version at the time.

$ git clone https://github.com/vapor/toolbox.git
$ cd toolbox
$ git checkout 18.7.5
$ make install
$ vapor --version
note: no Package.resolved file was found. Possibly not currently in a Swift package directory
framework: Vapor framework for this project: no Package.resolved file found. Please ensure you are in a Vapor project directory. If you are, ensure you have built the project with `swift build`. You can create a new project with `vapor new MyProject`
toolbox: 18.7.5

Toolbox installed successfully. Depending on the environment, you may need to install additional required libraries. In my environment, zlib1g-dev was required as a dependency package.

$ sudo apt-get update
$ sudo apt-get install zlib1g-dev

Now create a new Vapor project.

$ vapor new hello -n
Cloning template...
name: hello
Would you like to use Fluent (ORM)? (--fluent/--no-fluent)
y/n> no
fluent: No
Would you like to use Leaf (templating)? (--leaf/--no-leaf)
y/n> no
leaf: No
Generating project files
+ Package.swift
+ entrypoint.swift
+ configure.swift
+ routes.swift
+ .gitkeep
+ AppTests.swift
+ .gitkeep
+ Dockerfile
+ docker-compose.yml
+ .gitignore
+ .dockerignore
Creating git repository
Adding first commit
...

$ cd hello

Try running it in its bare state.

$ swift run
Building for debugging...

...

[ NOTICE ] Server started on http://127.0.0.1:8080

The application started on port 8080.

The official Vapor documentation says that Fluent, an ORM (Object-relational mapping) library, and Leaf, a templating engine, are supported. I think this is a sufficient setup for building an MPA application. I plan to keep tinkering with it for a while.

SwiftVapor

Share