Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HB2 articles to beta.1 #22

Merged
merged 1 commit into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/rss.xml
Expand Up @@ -4,7 +4,7 @@
<description>Articles about server-side Swift development. Created by Joannis Orlandos and Tibor Bödecs.</description>
<link>https://swiftonserver.com/</link>
<language>en-US</language>
<lastBuildDate>Tue, 12 Mar 2024 13:05:24 +0000</lastBuildDate>
<lastBuildDate>Thu, 14 Mar 2024 09:12:02 +0000</lastBuildDate>
<pubDate>Mon, 11 Mar 2024 23:00:00 +0000</pubDate>
<ttl>250</ttl>
<atom:link href="https://swiftonserver.com/rss.xml" rel="self" type="application/rss+xml"/>
Expand Down
24 changes: 12 additions & 12 deletions docs/sitemap.xml
@@ -1,34 +1,34 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://swiftonserver.com/using-environment-variables-in-swift/</loc>
<lastmod>2024-03-12</lastmod>
<loc>https://swiftonserver.com/using-openapi-with-hummingbird/</loc>
<lastmod>2024-03-14</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/logging-for-server-side-swift-apps/</loc>
<lastmod>2024-03-12</lastmod>
<loc>https://swiftonserver.com/whats-new-in-hummingbird-2/</loc>
<lastmod>2024-03-14</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/using-openapi-with-hummingbird/</loc>
<lastmod>2024-03-12</lastmod>
<loc>https://swiftonserver.com/using-environment-variables-in-swift/</loc>
<lastmod>2024-03-14</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/whats-new-in-hummingbird-2/</loc>
<lastmod>2024-03-12</lastmod>
<loc>https://swiftonserver.com/logging-for-server-side-swift-apps/</loc>
<lastmod>2024-03-14</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/building-swiftnio-clients/</loc>
<lastmod>2024-03-12</lastmod>
<lastmod>2024-03-01</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/using-swiftnio-channels/</loc>
<lastmod>2024-03-12</lastmod>
<lastmod>2024-03-01</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/using-swiftnio-fundamentals/</loc>
<lastmod>2024-03-12</lastmod>
<lastmod>2024-03-01</lastmod>
</url>
<url>
<loc>https://swiftonserver.com/developing-with-swift-in-visual-studio-code/</loc>
<lastmod>2024-03-12</lastmod>
<lastmod>2024-03-01</lastmod>
</url>
</urlset>
12 changes: 6 additions & 6 deletions docs/using-openapi-with-hummingbird/index.html
Expand Up @@ -89,8 +89,8 @@ <h1 class="title">Using OpenAPI Generator with Hummingbird</h1>
<h1>Using OpenAPI Generator with Hummingbird</h1><p>The <a href="https://github.com/apple/swift-openapi-generator" target="_blank">OpenAPI Generator</a> for Swift is a recent addition to the ecosystem that enables you to generate Swift code from OpenAPI specifications. This documentation-first approach allows you to define your API before starting a project, enabling both client and server code generation across multiple languages.</p><p>OpenAPI specifications can support a variety of content types, such as JSON, XML, Multipart, binary files and streams of data. When using OpenAPI to generate a client, you'll get a spec-compliant client implementation at the push of a button.</p><p>Servers generated from OpenAPI specifications are also spec-compliant. The generator creates an protocol named <code>APIProtocol</code> . Conforming to this type, you'll need to implement all the routes defined in your OpenAPI specification. This ensures that your server is always in sync with your API specification.</p><p>The OpenAPI generator ensures that data is handled efficiently and correctly. It also provides a clear and concise way to define your API, making it easy to understand and maintain. It integrates well with Hummingbird, requiring minimal setup to get started.</p><p>In this tutorial, we'll show you how to use the OpenAPI Generator to create a Swift API with <a href="./whats-new-in-hummingbird-2" target="_blank">Hummingbird</a>. If you're using Hummingbird with AWS Lambda, you can also use the OpenAPI generator for handling routes in your Lambda function.</p><h2>Prerequisites</h2><p>This tutorial has a <a href="https://github.com/swift-on-server/using-openapi-with-hummingbird-sample" target="_blank">sample project</a>, containing a starter and finished project. You can use this to verify your setup.</p><h2>OpenAPI Generation</h2><p>The <a href="https://swagger.io/specification/" target="_blank">OpenAPI Specification</a> is a standard for defining HTTP APIs. It allows you to define the endpoints, request and response bodies, and other relevant details of your API in a machine-readable format.</p><p>When adding the OpenAPI generator to your project, you'll need to add the following dependencies to your <code>Package.swift</code> file:</p><pre><code class="language-swift">.<span class="call">package</span>(url: <span class="string">"https://github.com/apple/swift-openapi-generator.git"</span>, from: <span class="string">"1.2.0"</span>),
.<span class="call">package</span>(url: <span class="string">"https://github.com/apple/swift-openapi-runtime.git"</span>, from: <span class="string">"1.3.0"</span>),
.<span class="call">package</span>(url: <span class="string">"https://github.com/apple/swift-argument-parser.git"</span>, from: <span class="string">"1.3.0"</span>),
.<span class="call">package</span>(url: <span class="string">"https://github.com/hummingbird-project/hummingbird.git"</span>, from: <span class="string">"2.0.0-alpha.3"</span>),
.<span class="call">package</span>(url: <span class="string">"https://github.com/swift-server/swift-openapi-hummingbird.git"</span>, from: <span class="string">"2.0.0-alpha.2"</span>),</code></pre><p><strong>Note:</strong> <code>swift-argument-parser</code> is not related to Hummingbird or OpenAPI.</p><!-- TODO: Update hummingbird dependency after release --><p>When working with OpenAPI generator, it's helpful to create a separate module (target) for your generated OpenAPI code. First of all, this allows you to import the generated code into a client implementation. But more importantly, it prevents the Swift compiler from getting confusing about the generated code "not existing" at times. When separating the OpenAPI module, it is compiled first, helping avoid these issues.</p><p>In order to complete the setup, add the following to your Package manifest:</p><pre><code class="language-swift">.<span class="call">target</span>(
.<span class="call">package</span>(url: <span class="string">"https://github.com/hummingbird-project/hummingbird.git"</span>, from: <span class="string">"2.0.0-beta.1"</span>),
.<span class="call">package</span>(url: <span class="string">"https://github.com/swift-server/swift-openapi-hummingbird.git"</span>, from: <span class="string">"2.0.0-beta.1"</span>),</code></pre><p><strong>Note:</strong> <code>swift-argument-parser</code> is not related to Hummingbird or OpenAPI.</p><!-- TODO: Update hummingbird dependency after release --><p>When working with OpenAPI generator, it's helpful to create a separate module (target) for your generated OpenAPI code. First of all, this allows you to import the generated code into a client implementation. But more importantly, it prevents the Swift compiler from getting confusing about the generated code "not existing" at times. When separating the OpenAPI module, it is compiled first, helping avoid these issues.</p><p>In order to complete the setup, add the following to your Package manifest:</p><pre><code class="language-swift">.<span class="call">target</span>(
name: <span class="string">"MyOpenAPI"</span>,
dependencies: [
<span class="comment">// 1</span>
Expand Down Expand Up @@ -165,8 +165,8 @@ <h1>Using OpenAPI Generator with Hummingbird</h1><p>The <a href="https://github.

<span class="keyword">func</span> run() <span class="keyword">async throws</span> {
<span class="comment">// 1</span>
<span class="keyword">let</span> router = <span class="type">HBRouter</span>()
router.<span class="property">middlewares</span>.<span class="call">add</span>(<span class="type">HBLogRequestsMiddleware</span>(.<span class="dotAccess">info</span>))
<span class="keyword">let</span> router = <span class="type">Router</span>()
router.<span class="property">middlewares</span>.<span class="call">add</span>(<span class="type">LogRequestsMiddleware</span>(.<span class="dotAccess">info</span>))

<span class="comment">// 2</span>
<span class="keyword">let</span> api = <span class="type">HelloAPI</span>()
Expand All @@ -175,15 +175,15 @@ <h1>Using OpenAPI Generator with Hummingbird</h1><p>The <a href="https://github.
<span class="keyword">try</span> api.<span class="call">registerHandlers</span>(on: router)

<span class="comment">// 4</span>
<span class="keyword">let</span> app = <span class="type">HBApplication</span>(
<span class="keyword">let</span> app = <span class="type">Application</span>(
router: router,
configuration: .<span class="keyword">init</span>(address: .<span class="call">hostname</span>(hostname, port: port))
)

<span class="comment">// 5</span>
<span class="keyword">try await</span> app.<span class="call">runService</span>()
}
}</code></pre><p>This code registers the <code>HelloAPI</code> routes, defined in your <code>openapi.yaml</code></p><p>Let's break down the above:</p><ol><li>Create a new <code>HBRouter</code> and add a logging middleware to it, allowing you to see the requests in the console.</li><li>Create a new <code>HelloAPI</code> instance. This is also the point where you can inject dependencies into your API implementation.</li><li>Register the HelloAPI's handlers on the router. This will make the <code>HelloAPI</code> respond to the <code>/hello</code> route.</li><li>Create a new <code>HBApplication</code> with the router and a configuration that specifies the hostname and port.</li><li>Run the application. This will make your route available at <a href="http://localhost:8080/hello" target="_blank">http://localhost:8080/hello</a>.</li></ol><p>That's all it takes to create a Hummingbird server that responds to the <code>/hello</code> route using the OpenAPI generator. There are many more features and options available in the OpenAPI generator, such as multipart or generating client code for use with a client transport such as <a href="https://github.com/swift-server/async-http-client" target="_blank">async-http-client</a> and URLSession. We hope this tutorial has given you a good starting point for using the OpenAPI generator with Hummingbird.</p>
}</code></pre><p>This code registers the <code>HelloAPI</code> routes, defined in your <code>openapi.yaml</code></p><p>Let's break down the above:</p><ol><li>Create a new <code>Router</code> and add a logging middleware to it, allowing you to see the requests in the console.</li><li>Create a new <code>HelloAPI</code> instance. This is also the point where you can inject dependencies into your API implementation.</li><li>Register the HelloAPI's handlers on the router. This will make the <code>HelloAPI</code> respond to the <code>/hello</code> route.</li><li>Create a new <code>Application</code> with the router and a configuration that specifies the hostname and port.</li><li>Run the application. This will make your route available at <a href="http://localhost:8080/hello" target="_blank">http://localhost:8080/hello</a>.</li></ol><p>That's all it takes to create a Hummingbird server that responds to the <code>/hello</code> route using the OpenAPI generator. There are many more features and options available in the OpenAPI generator, such as multipart or generating client code for use with a client transport such as <a href="https://github.com/swift-server/async-http-client" target="_blank">async-http-client</a> and URLSession. We hope this tutorial has given you a good starting point for using the OpenAPI generator with Hummingbird.</p>

</section>

Expand Down