<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Filipe A. Barroso</title>
    <description>The latest articles on DEV Community by Filipe A. Barroso (@abarroso).</description>
    <link>https://dev.to/abarroso</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1289%2FFompa8Ub.jpg</url>
      <title>DEV Community: Filipe A. Barroso</title>
      <link>https://dev.to/abarroso</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abarroso"/>
    <language>en</language>
    <item>
      <title>Deploy a Dart Shelf server to Google Run</title>
      <dc:creator>Filipe A. Barroso</dc:creator>
      <pubDate>Sat, 13 Nov 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/abarroso/deploy-a-dart-shelf-server-to-google-run-509o</link>
      <guid>https://dev.to/abarroso/deploy-a-dart-shelf-server-to-google-run-509o</guid>
      <description>&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@guillepozzi?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;guille pozzi&lt;/a&gt; on &lt;a href="https://unsplash.com/@guillepozzi?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--g3oNs79V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mac0gp01s3cvbabh4htq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--g3oNs79V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mac0gp01s3cvbabh4htq.png" alt="Image description" width="880" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us learn how to deploy your Dart project to Google Run. I will assume you already have a &lt;a href="https://developers.google.com/workspace/marketplace/create-gcp-project"&gt;GCP project&lt;/a&gt; running, have &lt;a href="https://cloud.google.com/sdk/docs/install"&gt;gcloud installed&lt;/a&gt;, &lt;a href="https://docs.docker.com/get-docker/"&gt;Docker installed&lt;/a&gt;, and are familiar with their CLI commands. You will also need a Billing account associated to your GCP Project, and the following GCP services activated in your project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Container Registry API&lt;/li&gt;
&lt;li&gt;  Cloud Run Admin API&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cloud Run while has a Free-tier, it is not a Free service, check out the pricing page for more detailed information: &lt;a href="https://cloud.google.com/run#section-14"&gt;https://cloud.google.com/run#section-14&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Cloud Run
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;"Cloud Run: Container to production in seconds."&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Cloud Run is a recent Google Cloud Platform product, built on top of &lt;a href="https://knative.dev/"&gt;KNative&lt;/a&gt; published in 2018, which just recently released &lt;a href="https://knative.dev/blog/releases/announcing-knative-v1-0-release/"&gt;version 1.0&lt;/a&gt;. After 1 year of the release of KNative, Google announced the Cloud Run in which with one container, you can deploy a containerized project, production-ready. It doesn't necessarily to be a Docker container (&lt;a href="https://cloud.google.com/run/docs/developing#containerizing"&gt;link&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note of advice, Docker has a rate limit on downloading images in the free tier, so do consider other repositories for your images to avoid disruptions.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Cloud Run, There are some criteria that your project needs to be under to be a valid project to run in Cloud Run, do check the following website to check if you can run your project: &lt;a href="https://cloud.google.com/run/docs/fit-for-run%20"&gt;https://cloud.google.com/run/docs/fit-for-run&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Dart Shelf
&lt;/h2&gt;

&lt;p&gt;Let me tell you an obvious secret, this section of this blog post will be the only one I need to mention Dart because to deploy to Cloud Run, it doesn't matter how you coded it but what it matters is if it is containerized. This blog post could be using any other programming language to create the server.&lt;/p&gt;

&lt;p&gt;I will assume you have a Dart Shelf project ready to deploy, I've shared my notes about Dart Shelf in my &lt;a href="https://filipebarroso.com/articles/dart%5Fshelf%5Fbackend/"&gt;previous blog post&lt;/a&gt; where I share how to create a simple API, through examples taken from the official resources with some added notes.&lt;/p&gt;

&lt;p&gt;To create a container for your Dart project we can use the official &lt;a href="https://hub.docker.com/%5F/dart"&gt;Dart Image&lt;/a&gt; and the Dockerfile is simple and easy to understand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; google/dart # The official Dart Image&lt;/span&gt;

&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app # Sets a default folder to keep the project organized&lt;/span&gt;

&lt;span class="c"&gt;# Do check the note.&lt;/span&gt;
&lt;span class="k"&gt;ONBUILD&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ADD&lt;/span&gt;&lt;span class="s"&gt; pubspec.* /app/&lt;/span&gt;
&lt;span class="k"&gt;ONBUILD RUN &lt;/span&gt;pub get
&lt;span class="k"&gt;ONBUILD&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ADD&lt;/span&gt;&lt;span class="s"&gt; . /app/&lt;/span&gt;
&lt;span class="k"&gt;ONBUILD RUN &lt;/span&gt;pub get &lt;span class="nt"&gt;--offline&lt;/span&gt;

&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; [] # Makes that is not possible to override parameters while running this image&lt;/span&gt;
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt;&lt;span class="s"&gt; ["/usr/bin/dart", "bin/server.dart"] # Forces to run the project main server dart file.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;em&gt;Check the reason why I used &lt;a href="https://docs.docker.com/engine/reference/builder/#onbuild"&gt;ONBUILD&lt;/a&gt; and why we have duplicated actions &lt;a href="https://github.com/dart-lang/dart%5Fdocker/blob/abba0f926187f52977735bc2f9e0c6630ab07b30/runtime/Dockerfile.template#L11"&gt;here&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that we have a Dockerfile and a Dart Shelf project, we can now continue with the deployment to Cloud Run.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to deploy to Cloud Run
&lt;/h2&gt;

&lt;p&gt;As mentioned, Cloud Run relies on containers to run the project, you can build the project from the &lt;a href="https://cloud.google.com/run/docs/deploying-source-code"&gt;source code&lt;/a&gt;, deploy through a &lt;a href="https://cloud.google.com/run/docs/deploying"&gt;container image&lt;/a&gt;, or even directly from a &lt;a href="https://cloud.google.com/run/docs/continuous-deployment-with-cloud-build"&gt;git repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am going through the process of using a deployed Image to Google's &lt;a href="https://cloud.google.com/container-registry"&gt;Container Registry&lt;/a&gt;. That is done in two steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Upload the Container Image to the Container Registry;&lt;/li&gt;
&lt;li&gt; Deploy the image to Cloud Run;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  1. Uploading a Docker image
&lt;/h3&gt;

&lt;p&gt;Now that we have a container image ready to upload, first, we will need to upload it to the Google Registry. For that, we will first A. tag and then B. submit.&lt;/p&gt;

&lt;h4&gt;
  
  
  A. Tagging a Docker image to upload to the Google Registry
&lt;/h4&gt;

&lt;p&gt;The first thing you have to do is to &lt;code&gt;tag&lt;/code&gt; your image with the prefix &lt;code&gt;gcr.io/&amp;lt;PROJECT_ID&amp;gt;/&amp;lt;IMAGE_NAME&amp;gt;&lt;/code&gt;. &lt;code&gt;gcr.io&lt;/code&gt; is the registry hosted in the USA for other registers check it &lt;a href="https://cloud.google.com/container-registry/docs/overview#registries"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Henceforth, our &lt;code&gt;&amp;lt;PROJECT_ID&amp;gt;&lt;/code&gt; will be &lt;code&gt;dart_project&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To tag an image to be able to upload to the Registry you simple use the following Docker command (more about Docker tags &lt;a href="https://docs.docker.com/engine/reference/commandline/tag/"&gt;here&lt;/a&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker tag dart_server:latest gcr.io/dart_project/dart_server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how I am tagging our image with the suffix &lt;code&gt;:latest&lt;/code&gt;, more about it in the section "Update the project".&lt;/p&gt;

&lt;h4&gt;
  
  
  B. Deploying the image {#b-dot-deploying-the-image}
&lt;/h4&gt;

&lt;p&gt;With the &lt;code&gt;tag&lt;/code&gt; ready you can now upload your image to your Cloud Registry of the project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud builds submit --tag gcr.io/dart_project/dart_server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Deploy Image as Google run
&lt;/h3&gt;

&lt;p&gt;We are now ready to deploy our dart server as a Google Run, we only need to define a &lt;code&gt;SERVICE_NAME&lt;/code&gt; and deploy it as a Google Run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SERVICE_NAME=dart-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note: The name of the services can only include alphanumeric and dashes, hence the name with dash and not the underscore as used so far.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud run deploy $SERVICE_NAME \
  --image=gcr.io/dart_project/dart_server \
  --allow-unauthenticated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Success! Your Cloud Run is now deployed and you can check the URL that the previous command outputs to try your deployed and new server!&lt;/p&gt;

&lt;h3&gt;
  
  
  Update the project
&lt;/h3&gt;

&lt;p&gt;After the deployment to Cloud Run, it makes sense that you want to update the server with the new code, for that, all you have to do is build your docker image and re-submit your image using the &lt;code&gt;gcloud builds submit&lt;/code&gt; since the tag we created is attached to the &lt;code&gt;:latest&lt;/code&gt; build of the image.&lt;/p&gt;

&lt;p&gt;To make our life easier we could use the following bash script to update our Cloud Run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SERVICE_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dart-server

gcloud builds submit &lt;span class="nt"&gt;--tag&lt;/span&gt; gcr.io/dart_project/dart_server

gcloud run deploy &lt;span class="nv"&gt;$SERVICE_NAME&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gcr.io/dart_project/dart_server &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--allow-unauthenticated&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Cloud Run is incredible to go from code to production, be advised that in this blog post I haven't written about security, managing persistent data, or having a full-blown project running with Cloud Run. Stay tuned for future blogposts to know more.&lt;/p&gt;

&lt;p&gt;Meanwhile, if you would like to learn more, there are some Codelabs you can learn from: &lt;a href="https://codelabs.developers.google.com/?cat=cloudrun"&gt;https://codelabs.developers.google.com/?cat=cloudrun&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Check the Cloud Run official webpage &lt;a href="https://cloud.google.com/run"&gt;https://cloud.google.com/run&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>dart</category>
      <category>backend</category>
      <category>gcp</category>
      <category>cloudrun</category>
    </item>
    <item>
      <title>Shelf — Web Server with Dart</title>
      <dc:creator>Filipe A. Barroso</dc:creator>
      <pubDate>Fri, 05 Nov 2021 00:00:00 +0000</pubDate>
      <link>https://dev.to/abarroso/shelf-backend-with-dart-42dj</link>
      <guid>https://dev.to/abarroso/shelf-backend-with-dart-42dj</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiysi5iti6u5541yie1jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiysi5iti6u5541yie1jo.png" alt="Shelf - Web Server with Dart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article is mostly a gathering of examples from Shelf and its official add-ons. Where I add my own comments that helped me understand the project and building a web server with Shelf, more about that in the end of the blogpost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Shelf
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/dart-lang/shelf" rel="noopener noreferrer"&gt;Shelf&lt;/a&gt;, is a Web Server Middleware, not a full blown server framework like Django, it is modular, and you add add-ons for the functionalities you need. This modular structure gives the community an easy way to expand the project.&lt;/p&gt;

&lt;p&gt;While &lt;a href="https://github.com/yissachar/awesome-dart#server-frameworks" rel="noopener noreferrer"&gt;other Dart for backend solutions exist&lt;/a&gt;. At the time of writing, two of the most promising Dart server frameworks, &lt;a href="https://github.com/stablekernel/aqueduct" rel="noopener noreferrer"&gt;Aqueduct&lt;/a&gt; and &lt;a href="https://github.com/angel-dart/angel" rel="noopener noreferrer"&gt;Angel&lt;/a&gt;, have stopped their development. Relying on the community to grab where they left and continuing the work done. While from &lt;a href="https://github.com/angel-dart/angel" rel="noopener noreferrer"&gt;Angel&lt;/a&gt; came &lt;a href="https://angel3-docs.dukefirehawk.com/" rel="noopener noreferrer"&gt;Angel3&lt;/a&gt; and from &lt;a href="https://github.com/stablekernel/aqueduct" rel="noopener noreferrer"&gt;Aqueduct&lt;/a&gt; came &lt;a href="https://github.com/conduit-dart/conduit" rel="noopener noreferrer"&gt;Conduit&lt;/a&gt;, I prefer to use a more stable solution, stable as maintained by a team with more resources. So, never forget to support the open-source community projects!&lt;/p&gt;

&lt;p&gt;Shelf, is maintained by the Dart team itself but still lacks functionalities, again because is not a server framework. If we want Shelf to compete with other server frameworks, such as Django (Python) it is necessary quite a bit of work. However, at the current status, we can create a web server that suits our needs.&lt;/p&gt;

&lt;p&gt;These are the official add-ons maintained by the Dart team:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Shelf (core)&lt;/li&gt;
&lt;li&gt;  Shelf Router&lt;/li&gt;
&lt;li&gt;  Shelf Router Generator&lt;/li&gt;
&lt;li&gt;  Shelf Static&lt;/li&gt;
&lt;li&gt;  Shelf WebSocket&lt;/li&gt;
&lt;li&gt;  Shelf CORS Headers&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Starting a backend Project
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;I am assuming you have Dart installed on your machine, if not you can follow the instructions at &lt;a href="https://dart.dev/get-dart" rel="noopener noreferrer"&gt;https://dart.dev/get-dart&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To run a server you need to create a new Dart project, I'll be using the terminal and the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart create shelf_core_example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a folder with all the code inside &lt;code&gt;shelf_core_example&lt;/code&gt; if you move into the new folder created you will notice the following file structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; CHANGELOG.md
 analysis_options.yaml
 pubspec.lock
 pubspec.yaml
 README.md
 bin/shelf_core_example.dart  # Same name as the project name created
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file &lt;code&gt;shelf_core_example.dart&lt;/code&gt; has a simple main file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Hello world!'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can run the existing file, with a simple command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart run bin/shelf_core_example.dart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will be prompted with the following message&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;dart run bin/shelf_core_example.dart
Hello World!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding Shelf
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have the project created, to use Shelf we simply add a Dart package dependency.&lt;/p&gt;

&lt;p&gt;In the project root you can add it by running the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add shelf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you are ready to work on your dart server! This command will also be used to add the other Shelf add-ons we will later add.&lt;/p&gt;

&lt;h4&gt;
  
  
  Shelf Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// For an easier evocation of its methods&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_echoRequest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Starts the server, in 'localhost' and with port 8080.&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// From the source documentation&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * Whether the [HttpServer] should compress the content, if possible.
   *
   * The content can only be compressed when the response is using
   * chunked Transfer-Encoding and the incoming request has `gzip`
   * as an accepted encoding in the Accept-Encoding header.
   *
   * The default value is `false` (compression disabled).
   * To enable, set `autoCompress` to `true`.
   */&lt;/span&gt;
  &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;autoCompress&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at http://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Method Handler that replies with the path called.&lt;/span&gt;
&lt;span class="c1"&gt;// The return =Response.ok= is our helper static function to return a 200 code result, but we have many other, such as, Response.found() for 302 redirects or Response.notFound() for 404 not founds and all other codes that you can expect.&lt;/span&gt;
&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;_echoRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Request for "&lt;/span&gt;&lt;span class="si"&gt;${request.url}&lt;/span&gt;&lt;span class="s"&gt;"'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;                                                                                                                                                                                           
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Permalink to &lt;code&gt;autoCompress&lt;/code&gt; source code: &lt;a href="https://github.com/dart-lang/sdk/blob/74dea5d1bdc40c2aac1ba3440513de3f46099173/sdk/lib/%5Fhttp/http.dart#l134" rel="noopener noreferrer"&gt;autoCompress&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Permalink to implementation of other &lt;a href="https://github.com/dart-lang/shelf/blob/769c76abf5550d1a0441822f69e8382a62ca19f1/lib/src/response.dart#L66" rel="noopener noreferrer"&gt;Response static methods&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Adding Shelf Add-ons
&lt;/h2&gt;

&lt;p&gt;The core library for Shelf, gives you the main functionalities of a server. The following example shows the barebones of a simplest server you can start running with Shelf.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shelf Router
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf_router" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf_router&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While Shelf (core) lacks the functionality of setting Routes, we use the add-on &lt;code&gt;shelf_router&lt;/code&gt; which will give you the &lt;code&gt;router&lt;/code&gt; instance, with all the REST methods, such as GET and POST and all other calls that make up REST.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add shelf_router
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shelf Router Example
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;server.dart&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'service.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;/// Similar with the previous example but here we create the routing in our new class 'Service' and we call its handler.&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at http://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;service.dart&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_router/shelf_router.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'api.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;/// Class used to set up all the routing for your server&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Service&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// The handler/middleware that will be used by the server, all the routing for the server will be implemented here.&lt;/span&gt;
  &lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// GET&lt;/span&gt;
    &lt;span class="c1"&gt;// Replies with the text inserted in the path, example: http://localhost:8080/say-hi/Filipe will prompt "hi Filipe".&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/say-hi/&amp;lt;name&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hi &lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// GET&lt;/span&gt;
    &lt;span class="c1"&gt;// You can use regex to limit the capture of the path arguments, in this case the userId param needs to be only digits. Note how you can also used 'async'&lt;/span&gt;
    &lt;span class="c1"&gt;// With the server running try to open http://localhost:8080/user/132 and http://localhost:8080/user/a123&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/user/&amp;lt;userId|[0-9]+&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delayed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;milliseconds:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'_o/'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// You can also embed other routers, in this case it will help organize your routers&lt;/span&gt;
    &lt;span class="c1"&gt;// Note: This needs be before the catch 'router.all' that follows&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/api/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Api&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// A catch all of the non implemented routing, useful for showing 404 page now found&lt;/span&gt;
    &lt;span class="c1"&gt;// With the server running try to open http://localhost:8080/abc&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/&amp;lt;ignored|.*&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Page not found'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;api.dart&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_router/shelf_router.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'service.dart'&lt;/span&gt;

&lt;span class="c1"&gt;/// Decoupled endpoints for a easier organisation of all endpoints&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Api&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;Future&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'[]'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;Router&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/messages'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/messages/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/messages/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/messages/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/messages/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_messages&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/&amp;lt;ignored|.*&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'null'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;router&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the previous code setup you can now run the server in the root of the project simply by executing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dart run bin/server.dart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Shelf Router Generator
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf_router_generator" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf_router_generator&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To reduce boilerplate, the add-on &lt;code&gt;shelf_router_generator&lt;/code&gt; helps us with the code generation. The add-on gives us annotations that replaces the need to call the 'router' instance.&lt;/p&gt;

&lt;p&gt;Shelf router generator leverages &lt;code&gt;build_runner&lt;/code&gt; to generate code, check the bullet Dependency for more information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add shelf_router_generator
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Dependency
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/build_runner" rel="noopener noreferrer"&gt;https://pub.dev/packages/build_runner&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn on how to use &lt;code&gt;build_runner&lt;/code&gt; check check the package URL.&lt;/p&gt;

&lt;p&gt;Add the &lt;code&gt;build_runner&lt;/code&gt; has &lt;code&gt;dev_dependencies&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add builder_runner
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shelf Router Generator Example
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;server.dart&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_router/shelf_router.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'service.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;part&lt;/span&gt; &lt;span class="s"&gt;'server.g.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Service&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at http://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;service.dart&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Service&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nd"&gt;@Route&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/say-hi/&amp;lt;name&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;_hi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'hi &lt;/span&gt;&lt;span class="si"&gt;$name&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nd"&gt;@Route&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'/&amp;lt;ignored|.*&amp;gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="n"&gt;_notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Page not found'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;Handler&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_$ServiceRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You need run once the &lt;code&gt;build_runner&lt;/code&gt; and only then the &lt;code&gt;dart run&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shelf Static
&lt;/h3&gt;

&lt;p&gt;If you need server side processing, to show an html page, you will need to add this add-on so that so you can present the users to&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf_static" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf_static&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add shelf_static
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shelf Static Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_static/shelf_static.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;createStaticHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'files'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;defaultDocument:&lt;/span&gt; &lt;span class="s"&gt;'index.html'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at http://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Shelf WebSocket
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf_web_socket/" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf_web_socket/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With Shelf, you don't necessary need to work with HTTP, you also have the possibility to have a socket open through &lt;code&gt;shelf_web_socket&lt;/code&gt; might be an add-on that not everyone uses, but it is interesting to know that it has.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dart pub add self_web_socket
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shelf WebSocket Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// For an easier evocation of its methods&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_web_socket/shelf_web_socket.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;webSocketHandler&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;webSocket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;webSocket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;webSocket&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;sink&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Echo: &lt;/span&gt;&lt;span class="si"&gt;$message&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at ws://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test this server with Web Socket I used the "Simple WebSocket Client" plugin for Chromium&lt;br&gt;
&lt;a href="https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en" rel="noopener noreferrer"&gt;https://chrome.google.com/webstore/detail/simple-websocket-client/pfdhoblngboilpfeibdedpjgfnlcodoo?hl=en&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Shelf CORS
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;package&lt;/strong&gt;: &lt;a href="https://pub.dev/packages/shelf_cors_headers" rel="noopener noreferrer"&gt;https://pub.dev/packages/shelf_cors_headers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Last but not least, we have this add-on to help us change the headers to have CORS working locally. Exposes a method called &lt;code&gt;corsHeaders()&lt;/code&gt; that returns a Middleware used in the Shelf Pipeline.&lt;/p&gt;

&lt;p&gt;Do not confuse this add-on with &lt;code&gt;shelf_cors&lt;/code&gt; that is not available for Dart 2.0&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dart pub add shelf_cors_headers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Shelf CORS Headers Example
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf/shelf_io.dart'&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="s"&gt;'package:shelf_cors_headers/shelf_cors_headers.dart'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;overrideHeaders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;ACCESS_CONTROL_ALLOW_HEADERS:&lt;/span&gt; &lt;span class="s"&gt;'example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'Content-Type'&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'application/json;charset=utf-8'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Pipeline&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;corsHeaders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="c1"&gt;// Middleware from 'shelf_cors_headers'&lt;/span&gt;
          &lt;span class="nl"&gt;headers:&lt;/span&gt; &lt;span class="n"&gt;overrideHeaders&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;addHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_echoRequest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;shelf_io&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;serve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'localhost'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Serving at http://&lt;/span&gt;&lt;span class="si"&gt;${server.address.host}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;${server.port}&lt;/span&gt;&lt;span class="s"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Response&lt;/span&gt; &lt;span class="nf"&gt;_echoRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Request for "&lt;/span&gt;&lt;span class="si"&gt;${request.url}&lt;/span&gt;&lt;span class="s"&gt;"'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While we wait for other Dart server frameworks to mature, we can always rely on Shelf to implement our project. There is still a lot needed to implement and few add-ons available that extends Shelf (&lt;a href="https://pub.dev/packages?q=shelf" rel="noopener noreferrer"&gt;https://pub.dev/packages?q=shelf&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Next article on the topic of Dart Backend will be about integrating Stripe into a server to pay recurrent or by metered API consumption. If I see myself using other Shelf add-on I'll write a new blogpost&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://filipebarroso.com/articles/dart_shelf_backend/" rel="noopener noreferrer"&gt;https://filipebarroso.com/articles/dart_shelf_backend/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dart</category>
      <category>backend</category>
    </item>
    <item>
      <title>Blogging Stack with Org Mode</title>
      <dc:creator>Filipe A. Barroso</dc:creator>
      <pubDate>Sun, 03 Oct 2021 15:55:51 +0000</pubDate>
      <link>https://dev.to/abarroso/blogging-stack-with-org-mode-2dn2</link>
      <guid>https://dev.to/abarroso/blogging-stack-with-org-mode-2dn2</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6q03fm49duu1zvjxprf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6q03fm49duu1zvjxprf.jpeg" alt="Bookshelves a Photo by Darwin Vegher"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;TL;DR&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://orgmode.org/" rel="noopener noreferrer"&gt;Org Mode&lt;/a&gt; - To write the articles, from anywhere

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://github.com/kaushalmodi/ox-hugo" rel="noopener noreferrer"&gt;Ox-hugo&lt;/a&gt; to parse the articles into Hugo&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;  &lt;a href="https://gohugo.io/" rel="noopener noreferrer"&gt;Hugo&lt;/a&gt; - For the website itself&lt;/li&gt;

&lt;li&gt;  &lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt; - Hosting&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Explaining the Stack
&lt;/h2&gt;

&lt;p&gt;I'm lazy, the more hurdles I have to publish a blog post, the harder it is for me to even start writing. To publish something I need to be simple, fast, and easy to write from anywhere, laptop, mobile, or even from handwritten notes.&lt;/p&gt;

&lt;p&gt;I use &lt;a href="https://en.wikipedia.org/wiki/Org-mode" rel="noopener noreferrer"&gt;Org mode&lt;/a&gt; to write all my notes, professionally or personally. If you are familiar with &lt;a href="https://en.wikipedia.org/wiki/Markdown" rel="noopener noreferrer"&gt;Markdown&lt;/a&gt;, Org Mode is just another &lt;a href="https://en.wikipedia.org/wiki/Lightweight%5Fmarkup%5Flanguage" rel="noopener noreferrer"&gt;Markup Language&lt;/a&gt; but on steroids.&lt;/p&gt;

&lt;p&gt;Since I've been using &lt;a href="https://www.gnu.org/software/emacs/" rel="noopener noreferrer"&gt;Emacs&lt;/a&gt;, more precisely &lt;a href="https://github.com/hlissner/doom-emacs" rel="noopener noreferrer"&gt;Doom Emacs&lt;/a&gt;, for a while now together with Org Mode to organize my work and life. To use Org Mode to write articles was the most natural transition, even more, after finding &lt;a href="https://github.com/kaushalmodi/ox-hugo" rel="noopener noreferrer"&gt;ox-hugo&lt;/a&gt; an Emacs package that parses Org Mode files to Markdown, that can be used in Hugo.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Org Mode
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;"Org Mode, your life in plain text"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Org Mode is a superset of Markdown. First and more important, Org Mode is just like any other text file, it can be opened and edited anywhere, just like Markdown. Org Mode is also like Markdown as in it is human-readable but more importantly, can be extended as you need it, check all default features &lt;a href="https://orgmode.org/features.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;, you can use it for clocking, tasks management, tables, and much more. Org Mode wins over Markdown since it is extensible as you need it to be, an example is that you can also use it for knowledge keeping, with &lt;a href="https://www.orgroam.com/" rel="noopener noreferrer"&gt;Org-Roam&lt;/a&gt;, just to mention one example.&lt;/p&gt;

&lt;p&gt;You can't do all this Markdown so easily but I understand that is unfair since I am using &lt;span&gt;Emacs&lt;/span&gt;. However, if you install Emacs solely to use Org Mode, you will be well served, no matter your background or for what you use Org Mode for. Check &lt;a href="https://orgmode.org/worg/org-in-the-wild.html" rel="noopener noreferrer"&gt;"Org Mode in the wild"&lt;/a&gt; for some examples of uses.&lt;/p&gt;

&lt;p&gt;Videos on how to use Org-Mode&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://www.youtube.com/watch?v=SzA2YODtgK4" rel="noopener noreferrer"&gt;Getting Started With Org Mode&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://youtu.be/VcgjTEa0kU4" rel="noopener noreferrer"&gt;Emacs From Scratch #5 - Org Mode Basics&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Hugo
&lt;/h3&gt;

&lt;p&gt;The main reason to use GoHugo is that you create articles in Markdown, in which I can parse them from Org Mode through the use of Ox-Hugo package, there are also plenty of resources, tutorials, and themes for Hugo that makes it easy to build any static website for your need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Firebase
&lt;/h3&gt;

&lt;p&gt;I'll be honest, I picked Firebase given the reason at the beginning of &lt;a href="https://filipebarroso.com/articles/blogging_with_orgmode/" rel="noopener noreferrer"&gt;this blog&lt;/a&gt; post, it is because I am lazy. &lt;br&gt;
Deploying a static website to Firebase is one of the easiest ways to deploy, and since I am really familiar with Firebase, it was my first choice. Nevertheless, Firebase is the one that is more easily replaceable from this Stack, with the Hugo website being static, you can deploy it to &lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;Github Pages&lt;/a&gt;, &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt;, &lt;a href="https://www.netlify.com/" rel="noopener noreferrer"&gt;Netlify&lt;/a&gt; or any other hosting service you prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to publish a new blog post
&lt;/h2&gt;

&lt;p&gt;In an overview the steps I need to publish a new article are the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Write article in Org Mode&lt;/li&gt;
&lt;li&gt; Parse the article by running a key binding, in Emacs is called &lt;a href="https://www.emacswiki.org/emacs/KeyChord" rel="noopener noreferrer"&gt;key chord&lt;/a&gt;, &lt;code&gt;C-c C-e H H&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Build Hugo website, for it I only have to run the command &lt;code&gt;$ hugo&lt;/code&gt; or &lt;code&gt;$ hugo serve&lt;/code&gt; that keeps building the website locally with every new changes&lt;/li&gt;
&lt;li&gt; Deploy to Firebase by running &lt;code&gt;$ firebase deploy&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the near future. I am going to add a &lt;a href="https://en.wikipedia.org/wiki/Make%5F(software)" rel="noopener noreferrer"&gt;Makefile&lt;/a&gt; so I can run the 3rd and 4th steps in one line. After, I might add a simple CI/CD to simplify even further. But the current setup is so simple that, for now, I don't feel the need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While the Stack I use for &lt;a href="https://filipebarroso.com/" rel="noopener noreferrer"&gt;this blog&lt;/a&gt; is unorthodox it is the one that fits me the best, and in my honest opinion, the fastest starting from writing to publishing also, and no less important is that everything can be versioned through &lt;a href="https://git-scm.com/" rel="noopener noreferrer"&gt;git&lt;/a&gt; and saved in a remote repository.&lt;/p&gt;

&lt;p&gt;Any part of the Stack can be replaceable, you can ignore all about Org Mode and write your articles directly in Hugo through Markdown, and you can go all crazy and use &lt;a href="https://pandoc.org/" rel="noopener noreferrer"&gt;Pandoc&lt;/a&gt; to convert a plethora of file formats to Markdown if you are crazy enough.&lt;/p&gt;

&lt;p&gt;About the hosting, since Hugo builds a static website, you can deploy the website anywhere you prefer.&lt;/p&gt;




&lt;p&gt;Hit me up at &lt;a href="https://twitter.com/ABarroso" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; let me know if you have any questions or if you prefer to feel free to schedule a 1 on 1 on &lt;a href="https://cal.com/filipebarroso" rel="noopener noreferrer"&gt;cal.com&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;This blog post was posted originally at &lt;a href="https://filipebarroso.com/" rel="noopener noreferrer"&gt;filipebarroso.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@darwiiiin?utm%5Fsource=unsplash&amp;amp;utm%5Fmedium=referral&amp;amp;utm%5Fcontent=creditCopyText" rel="noopener noreferrer"&gt;Darwin Vegher&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/pile?utm%5Fsource=unsplash&amp;amp;utm%5Fmedium=referral&amp;amp;utm%5Fcontent=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>orgmode</category>
      <category>emacs</category>
      <category>firebase</category>
      <category>gohugo</category>
    </item>
  </channel>
</rss>
