Skip to content

Commit 6b47895

Browse files
authored
Merge pull request #26 from loloop/mauricio/translate-linux-spritekit
fixes a couple of issues with contentloader and adds two articles
2 parents 0c8a9ff + 6a4d0c9 commit 6b47895

File tree

11 files changed

+662
-7
lines changed

11 files changed

+662
-7
lines changed

site/Build/br/event-based-architechture-for-spritekit/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ <h1>Arquitetura baseada em eventos para SpriteKit</h1>
175175
<span class="call">playDeathSound</span>()
176176
}
177177
}</code></pre>
178-
<p>E por fim, para mostrar a facilidade de se criar objetos que interagem com o sistema como um todo e causam efeitos na cadeia, mesmo que não sejam SKNodes, para adicionar suporte aos controles de Xbox One e PlayStation 4 no nosso jogo, podemos simplesmente adicionar uma classe como esta:</p>
178+
<p>E por fim, para mostrar a facilidade de se criar objetos que interagem com o sistema como um todo e causam efeitos na cadeia, mesmo que não sejam SKNodes, para adicionar suporte aos controles de Xbox e PlayStation no nosso jogo, podemos simplesmente adicionar uma classe como esta:</p>
179179
<pre class="splash"><code><span class="keyword">import</span> GameController
180180

181181
<span class="keyword">final class</span> GameController: <span class="type">EventHandler</span> {

site/Build/en/event-based-architecture-for-spritekit/index.html

+206
Large diffs are not rendered by default.

site/Build/en/index.html

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@
3838
<h1 class="huge">Hey, it's me, Mauricio</h1>
3939
<h2>Apple tech talk and the like</h2>
4040
</div>
41-
<div></div>
41+
<div>
42+
<div>
43+
<h2><a href="/en/serversideswift" class="inverted link-underline link-underline-opacity-100 link-underline-opacity-100-hover">Let's talk about Swift on Linux?</a></h2>
44+
<p>Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning.</p>
45+
</div>
46+
<div>
47+
<h2><a href="/en/event-based-architecture-for-spritekit" class="inverted link-underline link-underline-opacity-100 link-underline-opacity-100-hover">Event-Based Architecture for SpriteKit</a></h2>
48+
<p>Let’s take a look at how we can simplify app development with SpriteKit.</p>
49+
</div>
50+
</div>
4251
</div>
4352
</main>
4453
</body>
+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<!doctype html>
2+
<html data-bs-theme="auto" lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta content="width=device-width, initial-scale=1" name="viewport" />
6+
<meta content="Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning." name="description" />
7+
<meta content="Mauricio Cardozo" name="author" />
8+
<meta content="Ignite v0.2.1" name="generator" />
9+
<title>Let's talk about Swift on Linux? – Mauricio Cardozo</title>
10+
<link href="https://mauriciocardozo.me/en/serversideswift" rel="canonical" />
11+
<meta content="Mauricio Cardozo" property="og:site_name" />
12+
<meta content="https://mauriciocardozo.me/serverswift/tux.png" property="og:image" />
13+
<meta content="https://mauriciocardozo.me/serverswift/tux.png" name="twitter:image" />
14+
<meta content="Let's talk about Swift on Linux?" property="og:title" />
15+
<meta content="Let's talk about Swift on Linux?" name="twitter:title" />
16+
<meta content="Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning." property="og:description" />
17+
<meta content="Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning." name="twitter:description" />
18+
<meta content="https://mauriciocardozo.me/en/serversideswift" property="og:url" />
19+
<meta content="mauriciocardozo.me" name="twitter:domain" />
20+
<meta content="summary_large_image" name="twitter:card" />
21+
<meta content="on" name="twitter:dnt" />
22+
<meta content="article" name="og:type" />
23+
<meta content="en" name="og:locale" />
24+
<meta content="Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning." property="og:description" />
25+
<link href="/css/style.css" rel="stylesheet" />
26+
</head>
27+
<body class="container">
28+
<nav>
29+
<div class="site-navigation">
30+
<a href="/en" id="title" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover"><p style="font-weight: 500">MC</p></a>
31+
<ul>
32+
<li><a href="/en" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover m-0"><p>articles</p></a></li>
33+
<li><a href="/apps" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover m-0"><p>apps</p></a></li>
34+
<li><a href="/contact" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover m-0"><p>contact</p></a></li>
35+
<li><a href="https://nsbrazil.com" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover m-0"><p>NSBrazil</p></a></li>
36+
<li><a href="/" class="inverted link-plain link-underline link-underline-opacity-100 link-underline-opacity-100-hover m-0"><p>🇧🇷</p></a></li>
37+
</ul>
38+
</div>
39+
</nav>
40+
<main>
41+
<div class="content">
42+
<div>
43+
<article>
44+
<img alt="" src="/images/serverswift/tux.png" class="rounded" />
45+
<h1>Let's talk about Swift on Linux?</h1>
46+
<p></p>
47+
<h2>Let's talk about Swift on Linux?</h2>
48+
<p>Swift on Linux is an area little explored by Apple platform developers, but it can be a very useful tool and an immense source of knowledge, and it is very worth learning.</p>
49+
<p>We all already understand writing code, and seeing the result of our creation is not very different from other platforms when we are on the server, but one thing that I personally have always had a lot of problems with is: How do I make my code available to other people? We already know how to upload an app to the App Store, but what about uploading an open service on the internet? There are so many ways, all correct, each in its own way and it is almost impossible to choose one.</p>
50+
<h2>The Project</h2>
51+
<p>I created <a href="https://t.me/F1LandinhoBot">@F1LandinhoBot</a> with the purpose of helping me to remember the times of Formula 1 races in a group of friends, accessing Telegram’s API and sending messages so that we would not miss any races whatsoever.</p>
52+
<img src="/images/serverswift/telegram.png" />
53+
<p>And everything worked fine running directly on my Mac, but I don't want to leave my Mac on all the time running this code for the bot to work and I don't even want to think about how I'm going to guarantee that it will work all the time. Where I live, power outages are pretty common, and it seems like a waste of my computer's processing power anyway.</p>
54+
<p>So I decided to put this code to run on my Amazon server, which I've already had running a <a href="https://quake.host">Quake server</a> for a few years, so I wouldn't need to pay a single penny more than I already paid for it to get the service up and running. The Swift setup on Linux is fully detailed on <a href="https://swift.org">Swift.org</a> and despite being a little long, it is easy to follow. I cloned the code from GitHub and I wasn't expecting what came next:</p>
55+
<img src="/images/serverswift/wont-build.png" />
56+
<p class="center muted caption">OpenCombine is a library made specifically for us to use Combine on Linux, how come it doesn't exist? Since when is URLSession an AnyObject?</p>
57+
<h2>Swift on Linux</h2>
58+
<p>There are some speed bumps along the way to server-side Swift that are rarely mentioned, mainly because they are abstracted by the famous frameworks, but that you will encounter very quickly if you go the route of writing "pure" code. They all exist because Swift on Mac does not have the same libraries as Swift on Linux. The Foundation, which we are quite used to using in the Apple world, is not the same Foundation that exists in Linux, and <a href="https://forums.swift.org/t/what-are-best-practices-to-write-a-linux-software-on-macos/">this is not documented</a> anywhere. It's a problem that <a href="https://www.swift.org/blog/future-of-foundation/">is not going to last long</a>, but it's still a problem.</p>
59+
<img src="/images/serverswift/urlsession.png" />
60+
<p class="center muted caption">URLSession is on a completely different library on Linux, that does not even exist on macOS!</p>
61+
<h2>Publishing our work</h2>
62+
<p>Once these problems were resolved, I finally had my project compiling and running on the server! But now, how do I leave it running there? I can't just close the ssh in my terminal, because it will stop the code from executing. To solve this problem, I used <code>systemd</code>, which is Ubuntu's default service manager.</p>
63+
<p>The configuration file is very simple. I asked GPT to create a base and I customized it with the needs I had, like my environment variables (don't put them directly in your code!) and what my service would have to do if a problem occurred.</p>
64+
<pre class="splash"><code>[<span class="type">Unit</span>]
65+
<span class="type">Description</span>=<span class="type">Landinho</span> formula one schedule telegram bot
66+
<span class="type">After</span>=network-online.<span class="property">target</span>
67+
68+
[<span class="type">Service</span>]
69+
<span class="type">Type</span>=simple
70+
<span class="type">User</span>=ubuntu
71+
<span class="type">WorkingDirectory</span>=/home/ubuntu/<span class="type">LandinhoBot<br />ExecStart</span>=/home/ubuntu/swift/swift-<span class="number">5.7.3</span>-<span class="type">RELEASE</span>-ubuntu22.<span class="number">04</span>/usr/bin/swift run
72+
<span class="type">Environment</span>=<span class="string">"TELEGRAM_TOKEN=6103171:AAHrN7p7rTJPgeNoeYRo"</span>
73+
<span class="type">Environment</span>=<span class="string">"DEBUG_CHAT=-123131234"</span>
74+
<span class="type">Restart</span>=always
75+
76+
[<span class="type">Install</span>]
77+
<span class="type">WantedBy</span>=multi-user.<span class="property">target</span></code></pre>
78+
<p>After that, I still need to enter the server, download the code, compile and run the service. This whole process is very boring to do, and as it is not part of our daily lives, we may even forget what all these steps are like, so I wrote a very simple deploy script, to make the process easier. It downloads the code, compiles it and restarts the service:</p>
79+
<pre class="splash"><code>#!/bin/bash
80+
81+
git fetch
82+
git pull origin main
83+
(
84+
cd telegram
85+
swift build
86+
sudo systemctl restart landinho.<span class="property">service</span>
87+
)</code></pre>
88+
<p>But I still have to log into the server, run the script and check if everything is ok. It can be better. To solve this, after talking to a friend who is full-stack, I decided to go for the simplest route possible: Automate the process that I was already doing manually, through GitHub Actions.</p>
89+
<pre class="splash"><code>on:
90+
push:
91+
branches:
92+
- main
93+
jobs:
94+
deploy:
95+
runs-on: ubuntu-latest
96+
steps:
97+
- name: configure-ssh
98+
run: |
99+
mkdir -p ~/.<span class="property">ssh</span>/
100+
echo <span class="string">"$key"</span> &gt; ~/.<span class="property">ssh</span>/server.<span class="property">key</span>
101+
chmod <span class="number">400</span> ~/.<span class="property">ssh</span>/server.<span class="property">key</span>
102+
cat &gt;&gt;~/.<span class="property">ssh</span>/config &lt;&lt;<span class="type">END<br /> Host</span> server
103+
<span class="type">HostName</span> <span class="property">$host</span>
104+
<span class="type">User</span> <span class="property">$username</span>
105+
<span class="type">IdentityFile</span> ~/.<span class="property">ssh</span>/server.<span class="property">key</span>
106+
<span class="type">StrictHostKeyChecking</span> no
107+
<span class="type">END</span>
108+
- name: run-deploy-script
109+
run: ssh server 'cd <span class="type">LandinhoBot</span>; sh deploy.<span class="property">sh</span>'
110+
env:
111+
host: ${% raw %}{{secrets.<span class="type">SSH_HOST</span>}}{% endraw %}
112+
username: ${% raw %}{{secrets.<span class="type">SSH_USERNAME</span>}}{% endraw %}
113+
key: ${% raw %}{{secrets.<span class="type">SSH_KEY</span>}}{% endraw %}</code></pre>
114+
<p>It's important to thank <a href="https://twitter.com/flpms">@flpms</a>, who is the friend I mentioned during the article. The part of the configuration that creates the SSH configuration file for GitHub Actions is his authorship.</p>
115+
<p>Dissecting the Actions configuration, we have a job that will be executed every time a push happens on my <code>main</code> branch, with a step to configure SSH and a step to execute our deploy script.</p>
116+
<p>And that's it! This way you can automate the execution of your code on any Linux machine that you have access to the terminal. But there is another alternative:</p>
117+
<h2>Platform as a Service</h2>
118+
<p>An easier way to do this entire process is using a platform like <a href="https://render.com">Render</a> (or thousands of other services that do the same thing), which will abstract the entire process of configuring the environment, deploying it when there is a merge, and it will give you other perks, such as database backups and so on. This whole facility has its cost ($), and if you want to upgrade another service, the costs will rise way faster.</p>
119+
<img src="/images/serverswift/render.png" />
120+
<p>There's not much to say about these platforms, each one will have its own particularities, but it's an alternative that I really enjoy using! The less time we spend on infrastructure, the more we can invest in the product itself, and that's what matters in the end. Until next time!</p>
121+
<p></p>
122+
</article>
123+
</div>
124+
</div>
125+
</main>
126+
</body>
127+
</html>

0 commit comments

Comments
 (0)