肇鑫的技术博客

肇鑫 / Owen Zhao

独立开发者,主要开发 iOS、watchOS、macOS 应用。

目前在维护 SleepTapRooster Time,以及 Markdown Writer 相关工具。

最新文章

A Little Guide of NSViewControllerRepresentable

SwiftUI

What is NSViewControllerRepresentable

In SwiftUI, there are no View Controllers, only Views. So what should we do if want to use something related to View Controllers? Here comes NSViewControllerRepresentable/UIViewControllerRepresentable. Basically, we can use instance of View Controller Representable as View, contains a view controller.

Since view controllers have their own view, ViewControllerRepresentable has not view body.

protocol NSViewControllerRepresentable : View where Self.Body == Never

A Simple Sample of NSViewControllerRepresentable

  1. Create a new SwiftUI app.
  2. Add a new file, choose Cocoa Class, name it as TextViewController.swift.
    Choose create xib file as well.
  3. Add a close button and label.

xib

  1. TextViewController.swift
//
//  TextViewController.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import Cocoa

class TextViewController: NSViewController {
    @IBOutlet weak var label: NSTextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func closeButtonClicked(_ sender: Any) {
        self.dismiss(sender)
    }
}
  1. TextView
//
//  TextView.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import AppKit
import SwiftUI

struct TextView:NSViewControllerRepresentable {
    func makeNSViewController(context: Context) -> TextViewController {
        let controller  = TextViewController()        
        return controller
    }    
    
    func updateNSViewController(_ nsViewController: TextViewController, context: Context) {

    }
}
  1. ContentView.swift
//
//  ContentView.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import SwiftUI

struct ContentView: View {
    @State var title = ""
    @State var showTextView = false
    
    var body: some View {
        HStack {
            TextField("Title", text: $title)
        
            Button("Show Text View") {
                showTextView = true
            }
            .sheet(isPresented: $showTextView) {
                TextView()
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The app ran as below, but the close button didn't work.

first_sample

Coordinator

View Controllers are class, and View Representables are structs. We use Coordinator to pass changes to SwiftUI.

  1. TextView
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import AppKit
import SwiftUI

struct TextView:NSViewControllerRepresentable {
    @Environment(\.presentationMode) var presentationMode
    @Binding var title:String
    
    class Coordinator: NSObject, TextViewControllerDelegate {
        var parent: TextView

        init(_ parent: TextView) {
            self.parent = parent
        }
        
        func textView(_ tvc: TextViewController) {
            self.parent.presentationMode.wrappedValue.dismiss()
        }
    }
    
    func makeNSViewController(context: Context) -> TextViewController {
        let controller  = TextViewController()
        controller.delegate = context.coordinator
        
        return controller
    }
    
    func updateNSViewController(_ nsViewController: TextViewController, context: Context) {
        nsViewController.label.stringValue = title
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}
  1. TextViewController
//
//  TextViewController.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import Cocoa

class TextViewController: NSViewController {
    @IBOutlet weak var label: NSTextField!
    weak var delegate:TextViewControllerDelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func closeButtonClicked(_ sender: Any) {
        delegate?.textView(self)
    }
}

protocol TextViewControllerDelegate:AnyObject {
    func textView(_ textView:TextViewController)
}
  1. ContentView
//
//  ContentView.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import SwiftUI

struct ContentView: View {
    @State var title = ""
    @State var showTextView = false
    
    var body: some View {
        HStack {
            TextField("Title", text: $title)
        
            Button("Show Text View") {
                showTextView = true
            }
            .sheet(isPresented: $showTextView) {
                TextView(title: $title)
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}//
//  ContentView.swift
//  ViewControllerRepresentable Test
//
//  Created by zhaoxin on 2022/6/12.
//

import SwiftUI

struct ContentView: View {
    @State var title = ""
    @State var showTextView = false
    
    var body: some View {
        HStack {
            TextField("Title", text: $title)
        
            Button("Show Text View") {
                showTextView = true
            }
            .sheet(isPresented: $showTextView) {
                TextView(title: $title)
            }
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

App ran like this and the close button worked.

second_sample

Update Variables of NSViewController

You may aware that there was a func

func updateNSViewController(_ nsViewController: TextViewController, context: Context) {
    nsViewController.label.stringValue = title
}

We couldn't call nsViewController.label.stringValue = title in makeNSViewController, as in the make function the View Controller was created, but the xib was not loaded. We can think this update as viewDidLoad function in View Controller.

Resources

SwiftUI, Core Data and CloudKit Part 2

Core Data

According to Apple, adding CloudKit Core Data, was as easy as following three steps.

  1. Enable iCloud Capacity in Xcode and check CloudKit.
  2. Add a default iCloud container.
  3. Replacing NSPersistentContainer with NSPersistentCloudKitContainer.

Then everything will work.

Using Core Data With CloudKit

It is not true. I did that and that won't work. I even checked the Core Data model with "Used with CloudKit", and there was still an error and CloudKit schema couldn't be created as there was no store for CloudKit.

So I had to create a new project with Core Data and CloudKit enabled at the very beginning.

If you want to know how to convert a Core Data project with CloudKit. See this, Getting Started with Core Data and CloudKit

Create a new project

Create a new project named "Todo List 2" and enable Core Data and CloudKit.

Project Code

  1. Add iCloud capability and check CloudKit.
  2. Then add a default container "iCloud.(You app bundle id)"

iCloud Capability

This is enough for a macOS app. If yours is an iOS app, you must add Background Modes capability and check "Remote notifications".

Be careful when you testing your app. The development and release app used different databases in CloudKit. So if you test your app, you must use the development version.

References

SwiftUI, Core Data and CloudKit Part 1

Core Data

This is a simple project for SwiftUI, Core Data and CloudKit. In part 1, I will create a simple project in SwiftUI, then turn it into Core Data. In part 2, I will use CloudKit to sync with Core Data.

SwiftUI

Create a macOS SwiftUI project.

Project Code

//
//  ContentView.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import SwiftUI

struct ContentView: View {
    @State private var items = [Item]()
    @State private var addNewItem = false
    
    let addNewItemPublisher = NotificationCenter.default.publisher(for: AddItemView.addNewItem)
    
    var body: some View {
        VStack {
            ScrollView(.vertical, showsIndicators: true) {
                if items.isEmpty {
                    Text("No Data")
                } else {
                    List($items) { item in
                        HStack {
                            Text(DateFormatter.localizedString(from: item.startDate.wrappedValue, dateStyle: .none, timeStyle: .short))
                            Text(item.title.wrappedValue)
                        }
                    }
                    .frame(minWidth: 580, minHeight: 400, idealHeight: 600)
                }
            }
            .onReceive(addNewItemPublisher) { notification in
                if let userInfo = notification.userInfo as? [String:Item], let item = userInfo["new item"] {
                    items.append(item)
                    print(items)
                }
            }
            
            Button {
                add()
            } label: {
                Text("Add")
            }
            .sheet(isPresented: $addNewItem) {
                AddItemView()
            }
        }
        .padding()
        .frame(width: 600, height: 600, alignment: .center)
        
    }
    
    private func add() {
        addNewItem = true
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
//
//  AddItemView.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import SwiftUI

struct AddItemView: View {
    static let addNewItem = Notification.Name("addNewItem")
    
    @Environment(\.dismiss) private var dismiss
    @State private var item = Item(startDate: Date(), title: "")
    
    var body: some View {
        VStack {
            Text("Add New Item")
                .font(.title2)
            
            TextField("What to do?", text: $item.title, prompt: Text("Go shopping."))
            DatePicker("When?", selection: $item.startDate)
            
            HStack {
                Button {
                    save()
                } label: {
                    Text("Save")
                }
                .disabled(item.title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)

                Spacer()
                
                Button {
                    cancel()
                } label: {
                    Text("Cancel")
                }
            }
        }
        .padding()
    }
    
    private func save() {
        NotificationCenter.default.post(name: AddItemView.addNewItem, object: nil, userInfo: ["new item" : item])
        dismiss()
    }
    
    private func cancel() {
        dismiss()
    }
}

struct AddItemView_Previews: PreviewProvider {
    static var previews: some View {
        AddItemView()
    }
}
//
//  Model.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import Foundation

struct Item:Identifiable {
    let id = UUID().uuidString
    var startDate:Date
    var title:String
}

class ItemProvider:ObservableObject {
    @Published var items = [Item]()
}

You can test and run the app.

Core Data Basics

NSPersistentContainer

NSPersistentContainer handles the creation of the Core Data stack and offers access to the NSManagedObjectContext as well as a number of convenience methods.

NSManagedObjectModel

The NSManagedObjectModel instance describes the data that is going to be accessed by the Core Data stack. During the creation of the Core Data stack, the NSManagedObjectModel is loaded into memory as the first step in the creation of the stack.

NSPersistentStoreCoordinator

The NSPersistentStoreCoordinator sits in the middle of the Core Data stack. The coordinator is responsible for realizing instances of entities that are defined inside of the model. It creates new instances of the entities in the model, and it retrieves existing instances from a persistent store (NSPersistentStore).

NSManagedObjectContext

The managed object context (NSManagedObjectContext) is the object that your application will interact with the most, and therefore it is the one that is exposed to the rest of your application. Think of the managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered.

All managed objects must be registered with a managed object context. You use the context to add objects to the object graph and remove objects from the object graph. The context tracks the changes you make, both to individual objects’ attributes and to the relationships between objects. By tracking changes, the context is able to provide undo and redo support for you. It also ensures that if you change relationships between objects, the integrity of the object graph is maintained.

If you choose to save the changes you have made, the context ensures that your objects are in a valid state. If they are, the changes are written to the persistent store (or stores), new records are added for objects you created, and records are removed for objects you deleted.

Without Core Data, you have to write methods to support archiving and unarchiving of data, to keep track of model objects, and to interact with an undo manager to support undo. In the Core Data framework, most of this functionality is provided for you automatically, primarily through the managed object context.

Core Data

Project Code

To use core data, first we must remove "Model.swift" file. Then we created a Core Data Model file, named it as "Model.xcdatamodeld".

Then we create it attributes.

core data model

Create ItemProvider

Add a new file, name it as "ItemProvider.swift".

//
//  ItemProvider.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import Foundation
import CoreData
import AppKit

class ItemProvider:ObservableObject {
    let container = NSPersistentContainer(name: "Model")
    
    init() {
        container.loadPersistentStores { description, error in
            if let error = error {
                let alert = NSAlert(error: error)
                NSSound.beep()
                alert.runModal()
            }
        }
    }
}

We must first create the NSPersistentContainer, or the app will not know how to create an instance of "Item".

Core Data in SwiftUI

We need to set managedObjectContext in environment, so it will be use in the SwiftUI app.

//
//  Todo_ListApp.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

/*
 <a href="https://www.flaticon.com/free-icons/reminder" title="reminder icons">Reminder icons created by max.icons - Flaticon</a>
 */

import SwiftUI

@main
struct Todo_ListApp: App {
    @StateObject private var itemProvider = ItemProvider()
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, itemProvider.container.viewContext)
        }
    }
}

Then in ContentView.swift, we fetch the results from Core Data.

//
//  ContentView.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import SwiftUI

struct ContentView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(sortDescriptors: [SortDescriptor(\.startDate, order: .forward)]) var items:FetchedResults<Item>
    @State private var item:Item?
    
    var body: some View {
        VStack {
            ScrollView(.vertical, showsIndicators: true) {
                if items.isEmpty {
                    Text("No Data")
                } else {
                    List(items) { item in
                        HStack {
                            Text(DateFormatter.localizedString(from: item.startDate!, dateStyle: .none, timeStyle: .short))
                            Text(item.title!)
                        }
                    }
                    .frame(minWidth: 580, minHeight: 400, idealHeight: 600)
                }
            }
            
            Button {
                add()
            } label: {
                Text("Add")
            }
            .sheet(item: $item) { item in
                AddItemView(item: item)
            }
        }
        .padding()
        .frame(width: 600, height: 600, alignment: .center) 
    }
    
    private func add() {
        let item = Item(context: managedObjectContext)
        item.id = UUID()
        item.startDate = Date()
        self.item = item
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

In AddItemView, the similar.

//
//  AddItemView.swift
//  Todo List
//
//  Created by zhaoxin on 2022/5/7.
//

import SwiftUI
import CoreData

struct AddItemView: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @Environment(\.dismiss) private var dismiss
    @State var item:Item
    
    var body: some View {
        VStack {
            Text("Add New Item")
                .font(.title2)
            
            TextField("What to do?", text: Binding($item.title) ?? .constant(""), prompt: Text("Go shopping."))
            DatePicker("When?", selection: Binding($item.startDate) ?? .constant(Date()))
            
            HStack {
                Button {
                    save()
                } label: {
                    Text("Save")
                }

                Spacer()
                
                Button {
                    cancel()
                } label: {
                    Text("Cancel")
                }
            }
        }
        .padding()
    }
    
    private func save() {
        if var title = item.title {
            title = title.trimmingCharacters(in: .whitespacesAndNewlines)
            guard !title.isEmpty else {
                let alert = NSAlert()
                alert.messageText = NSLocalizedString("Title is empty!", comment: "")
                alert.alertStyle = .warning
                alert.addButton(withTitle: NSLocalizedString("OK", comment: ""))
                NSSound.beep()
                alert.runModal()
                
                return
            }
            
            do {
                item.title = title
                try managedObjectContext.save()
            } catch {
                let alert = NSAlert(error: error)
                NSSound.beep()
                alert.runModal()
            }
        }
        
        dismiss()
    }
    
    private func cancel() {
        managedObjectContext.rollback()
        dismiss()
    }
}

In SwiftUI, we use "NotificationCenter" to post a notification in save() function. In Core Data, we no longer need the notification as we could save directly and the change will automatically trigger the FetchedResults.

Reference

App Architecture of SwiftUI

SwiftUI

When developing with Cocoa/UIKit, the most common used architecture is MVC.

model_view_controller_2x

What is the architecture when using SwiftUI? Someone says it is MVVM. Someone says it is MV, MVC without C. I would like say it would be M-VC, Model with View and Controller.

M-VC

We want every element to be simple. For MV architecture, the View is too heavy for me. For M-VC, we want the View as simple as possible. The extra parts we think it to be Controller.

Split View into small Views

For a SwiftUI View, when one of the @State properties changed, all @State properties in the same view will be updated. If this is overhead, you can split those @State properties to another view, that will reduce CPU usage and speed up your app.

Get Macs, IP Addresses and Internet IP Addresses

通用

We need to use C function to get those values. And then we check the values with command ifconfig and understand which to which.

Get Local MACs and IP Addresses

private func getLocalIPAndMACAdress() {
    var address : String?

    // Get list of all interfaces on the local machine:
    var ifaddr:UnsafeMutablePointer<ifaddrs>!
    if getifaddrs(&ifaddr) == 0 {
        // For each interface ...
        var ptr:UnsafeMutablePointer<ifaddrs>! = ifaddr
        var networkLinkDictionary = [String:[String]]()
        
        repeat {
            defer { ptr = ptr.pointee.ifa_next}
            let interface = ptr.pointee
            // Check interface name:
            let name = String(cString: interface.ifa_name)
            // Convert interface address to a human readable string:
            var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
            getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.pointee.sa_len),
                        &hostname, socklen_t(hostname.count),
                        nil, socklen_t(0), NI_NUMERICHOST)
            address = String(cString: hostname)
            var values = networkLinkDictionary[name] ?? []
            values.append(address!)
            networkLinkDictionary.updateValue(values, forKey: name)
        } while ptr != nil
   
        freeifaddrs(ifaddr)
        
        set(networkLinkDictionary: networkLinkDictionary)
        
        #if DEBUG
        networkLinkDictionary.keys.sorted().forEach { key in
            print(key, "\t", networkLinkDictionary[key]!)
        }
        #endif
    }
}

private func set(networkLinkDictionary:[String:[String]]) {
    networkLinkDictionary.keys.sorted().forEach {
        switch $0 {
            // for iMac 5K, en0 is ethernet; en1 is WiFi
        case "en0":
            enthernet = getNetworkLink(values: networkLinkDictionary["en0"] ?? [])
            #if DEBUG
            debugPrint(enthernet)
            #endif
        case "en1":
            wifi = getNetworkLink(values: networkLinkDictionary["en1"] ?? [])
            #if DEBUG
            debugPrint(wifi)
            #endif
        default:
            break
        }
    }
}

private func getNetworkLink(values:[String]) -> NetworkLink {
    let networkLink:NetworkLink
    
    switch values.count {
    case 1:
        networkLink = NetworkLink(MAC: values[0])
    case 2:
        networkLink = NetworkLink(MAC: values[0], ipv6: values[1])
    case 3:
        networkLink = NetworkLink(MAC: values[0], ipv6: values[1], ipv4: values[2])
    default:
        networkLink = NetworkLink(MAC: "")
    }
    
    return networkLink
}

public struct NetworkLink:Equatable {
    public init(MAC:String, ipv6:String? = nil, ipv4:String? = nil){
        self.MAC = MAC
        self.ipv6 = ipv6
        self.ipv4 = ipv4
    }
    
    public let MAC:String
    public var ipv6:String? = nil
    public var ipv4:String? = nil
}

Get Internet IP Addresses

We use ipify.org to get internet IP addresses.

Don't use URLSession.share

Create a new URLSession each time your own. If you use URLSession.share, you app won't detect internet IP changes, it will always show the IP when the app starts.

I had tried 3 different frameworks on github.com. They all has the above issue. So I write the code myself.

private func setInterIP(type:IPType) async {
    let url:URL
    
    if !reachable {
        switch type {
        case .ipv4:
            internetIPV4 = NSLocalizedString("Inactive", bundle: .module, comment: "")
        case .ipv6:
            internetIPV6 = NSLocalizedString("Inactive", bundle: .module, comment: "")
        }
        
        return
    }
    
    switch type {
    case .ipv4:
        url = URL(string: "https://api.ipify.org?format=json")!
        
        if internetIPV4.isEmpty {
            internetIPV4 = "..."
        }
    case .ipv6:
        url = URL(string: "https://api64.ipify.org?format=json")!
        
        if internetIPV6.isEmpty {
            internetIPV6 = "..."
        }
    }
    let urlSessionConfiguration = URLSessionConfiguration.default
    let urlSession = URLSession(configuration: urlSessionConfiguration)
    
    do {
        let (data, urlResponse) = try await urlSession.data(from: url)
        DispatchQueue.main.async { [self] in
            if let httpResponse = urlResponse as? HTTPURLResponse, httpResponse.statusCode == 200 {
                if let dic = try? JSONSerialization.jsonObject(with: data, options: []) as? [String:String] {
                    switch type {
                    case .ipv4:
                        internetIPV4 = dic["ip"]!
                        #if DEBUG
                        debugPrint(internetIPV4)
                        #endif
                    case .ipv6:
                        internetIPV6 = dic["ip"]!
                        #if DEBUG
                        debugPrint(internetIPV6)
                        #endif
                    }
                }
            } else {
                #if DEBUG
                debugPrint(urlResponse)
                #endif
            }
        }
    } catch {
        print(error)
    }
}

Fix of Xcode SPM Downloading Speed Issue

Vapor

This command will fixed the issue that Xcode downloading too slow with SPM. Say you have a project name "hello", there is a Package.swift in it.

$ cd hello
$ xcodebuild -resolvePackageDependencies -scmProvider system
$ open Package.swift

hello

This issue was because when using git, Xcode ignore the proxy setting by default.

Pre-request

You should have your proxy set, for me, I have

$ cat .bashrc 
# proxy list
alias proxy='export all_proxy=socks5://127.0.0.1:7890'
alias unproxy='unset all_proxy'

# run
proxy

Xcode not follows system proxy settings to resolve SwiftPM dependencies

Xcode 13 ignores GIT proxy settings when resolving SwiftPM packages (and probably for other SCM operations as well).

Vapor with SQLite

Vapor

When using SQLite driver with Vapor, we have to install another package, or the project won't compile.

$ sudo apt install libsqlite3-dev

Vapor Behind NGINX in Ubuntu 20.04

Vapor

  1. Install developing packages.

How To Install Vapor On A Virtual Machine

  1. Install NGINX
  2. Install Swift
  3. Install Vapor Toolbox
  4. Check RAM
  5. Create Vapor Sample Project
  6. Set Vapor Proxy Behind NGINX
  7. Install SuperVisor

My server was 1GB RAM VPS in Ubuntu 20.04 LTS. And I developed vapor on my iMac 5K. So the first thing I did was to create a Linux client.

I prefer VMWare Fusion. As it was free for open source use. I tried VirtualBox, it ran slow with iMac 5K. Parallels Desktop was also good. But it was too expensive.

After all, now you should have a working Ubuntu 20.04.

Install developing packages.

 $ sudo apt update
 $ sudo apt install \
          binutils \
          git \
          gnupg2 \
          libc6-dev \
          libcurl4 \
          libedit2 \
          libgcc-9-dev \
          libpython2.7 \
          libsqlite3-0 \
          libstdc++-9-dev \
          libxml2 \
          libz3-dev \
          pkg-config \
          tzdata \
          zlib1g-dev

Install NGINX

$ sudo apt install nginx

Open Firefox with "localhost".

Screenshot from 2021-12-02 18-48-53

Install Swift

Download the latest release Swift Toolchain for Ubuntu 20.04.

$ cd Downloads/
$ wget https://download.swift.org/swift-5.5.1-release/ubuntu2004/swift-5.5.1-RELEASE/swift-5.5.1-RELEASE-ubuntu20.04.tar.gz
$ tar xzf swift-5.5.1-RELEASE-ubuntu20.04.tar.gz
$ vi ~/.bashrc

Append

export PATH=/home/zhaoxin/Downloads/swift-5.5.1-RELEASE-ubuntu20.04/usr/bin:”${PATH}”

Save and Quit。

Quit Terminal and Open it again. Type

$ swift --version
Swift version 5.5.1 (swift-5.5.1-RELEASE)
Target: x86_64-unknown-linux-gnu

Install Vapor Toolbox

$ cd ~/Downloads
$ git clone https://github.com/vapor/toolbox.git
$ cd toolbox/
$ git checkout 18.3.3
$ swift build -c release --disable-sandbox --enable-test-discovery
$ sudo mv .build/release/vapor /usr/local/bin

Check RAM

When compile, Vapor needs at least 2GB RAM. So for some VPS with only 1GB RAM, you will get fatalError when compile. You can check RAM with command

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       1.2Gi       447Mi        14Mi       2.1Gi       2.3Gi
Swap:         923Mi       1.0Mi       921Mi

Above is my VMWare Fusion client. However, for you VPS, you may get a result as below

$ free -h
              total        used        free      shared  buff/cache   available
Mem:          981Mi       215Mi        95Mi       2.0Mi       670Mi       575Mi
Swap:            0B          0B          0B

If that is your case, you must create a swap or you get your Vapor project compiled.

Create Swap File

How To Add Swap Space on Ubuntu 20.04

Vapor Hello Sample Won’t Compile

$ sudo swapon --show

You may get nothing shown as there was no swap for you VPS. Check you disk free space.

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            447M     0  447M   0% /dev
tmpfs            99M  944K   98M   1% /run
/dev/vda1        24G   12G   11G  51% /
tmpfs           491M     0  491M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           491M     0  491M   0% /sys/fs/cgroup
/dev/loop0       43M   43M     0 100% /snap/certbot/1514
/dev/loop1       43M   43M     0 100% /snap/certbot/1582
/dev/loop2      100M  100M     0 100% /snap/core/11798
/dev/loop3      100M  100M     0 100% /snap/core/11993
/dev/loop4       62M   62M     0 100% /snap/core20/1169
/dev/loop5       62M   62M     0 100% /snap/core20/1242
tmpfs            99M     0   99M   0% /run/user/0

Look at "/dev/vda1 24G 12G 11G 51% /", That meant you had 11G free space.

Create swap file.

$ sudo fallocate -l 1G /swapfile
$ ls -lh /swapfile
-rw-r--r-- 1 root root 1.0G Apr 25 11:14 /swapfile

Enable swap file

$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
-rw------- 1 root root 1.0G Apr 25 11:14 /swapfile
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
$ sudo swapon /swapfile
$ sudo swapon --show
NAME      TYPE  SIZE USED PRIO
/swapfile file 1024M   0B   -2
$ free - h
              total        used        free      shared  buff/cache   available
Mem:        1004716      166880      147152        2724      690684      643212
Swap:       1048572        5940     1042632

Save Swap File

$ sudo cp /etc/fstab /etc/fstab.bak
$ echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Create Vapor Sample Project

$ cd
$ mkdir vapor
$ cd vapor
$ git config --global user.email "you@email.com"
$ git config --global user.name "Your Name"
$ vapor new hello -n
$ cd hello/
$ swift run
...
[1686/1686] Build complete!
[ NOTICE ] Server starting on http://127.0.0.1:8080

Open Firefox and type "localhost:8080".
Screenshot from 2021-12-02 19-53-32

Set Vapor Proxy Behind NGINX

403 forbidden behind nginx

$ cd /etc/nginx/sites-available/
$ sudo cp default default.bak
$ sudo rm default
$ sudo vi default

Copy and paste below to default and save.

server {
        listen 80 default_server;
        listen [::]:80 default_server;

root /var/www/html;

index index.html index.htm index.nginx-debian.html;

server_name _;
        try_files $uri @proxy;
        location @proxy {
                proxy_pass http://127.0.0.1:8080;
                proxy_pass_header Server;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass_header Server;
                proxy_connect_timeout 3s;
                proxy_read_timeout 10s;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
        }
}

Restart NGINX.

sudo systemctl restart nginx

How To Install Vapor On A Virtual Machine

Install SuperVisor

$ sudo apt install supervisor
$ cd /etc/supervisor/conf.d/
$ sudo vi hello.conf

Copy and paste below code and save.

[program:hello]
command=/home/zhaoxin/vapor/hello/.build/debug/Run serve --env production
directory=/home/zhaoxin/vapor/hello
user=zhaoxin
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log
$ sudo supervisorctl reread
$ sudo supervisorctl add hello
$ sudo supervisorctl start hello

Supervisor