<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ishaan Bahal on Medium]]></title>
        <description><![CDATA[Stories by Ishaan Bahal on Medium]]></description>
        <link>https://medium.com/@ishaanbahal?source=rss-587ac0efe474------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*k8MV_QAivZgXnH9S4NWlLA.jpeg</url>
            <title>Stories by Ishaan Bahal on Medium</title>
            <link>https://medium.com/@ishaanbahal?source=rss-587ac0efe474------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 23:54:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ishaanbahal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Startup friendly guide to securing GRPC connections using Traefik]]></title>
            <link>https://medium.com/meeve/startup-friendly-guide-to-securing-grpc-connections-using-traefik-cc383c0b9d55?source=rss-587ac0efe474------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc383c0b9d55</guid>
            <category><![CDATA[tls]]></category>
            <category><![CDATA[traefik]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[grpc]]></category>
            <dc:creator><![CDATA[Ishaan Bahal]]></dc:creator>
            <pubDate>Tue, 29 Jan 2019 21:00:34 GMT</pubDate>
            <atom:updated>2019-01-29T21:00:34.717Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TZz1RX24GUaZgPO9BsW3ZA.jpeg" /></figure><p>Anyone who has read even a little bit about GRPC, has probably read about the fantastic TLS support it provides. However, its actually hard to set it up if you’re using GRPC with mobile devices and web, at least it was for me. The issue was, most docs and tutorials talked about using a custom cert to talk directly to the GRPC server and securing that communication.</p><p>In this post, we cover SSL termination at the LB or reverse proxy level and still keep the original servers running without a change.</p><p>As a small example, we’ll try connecting a flutter (dart) app to our now secure setup.</p><h3>Let’s get to know our options</h3><ol><li><strong>Nginx (v1.3.10):</strong> Nginx recently added support for GRPC with the version 1.3.10. If you’re already setup with Nginx, you can keep using it. The issue with nginx is, it cannot do automatic cert renewal and you have to either manually get the certs from LetsEncrypt or setup a cron. Read more about GRPC support for nginx at: <a href="https://www.nginx.com/blog/nginx-1-13-10-grpc/">https://www.nginx.com/blog/nginx-1-13-10-grpc/</a> (this is pretty much the only documentation on the subject)</li><li><strong>Envoy-proxy</strong>: The envoy is a sidecar proxy, and a great concept. The only issue was, it’s slightly harder to get started with and seemed to have a whole lot of configuration to be done. It does have good support for HTTP/2 and GRPC. Also, the documentation for envoy is a bit scary, and mostly talking about configurations, and you have to read through a lot to understand the GRPC bit.</li><li><strong>Traefik: </strong>Traefik is a reverse proxy that is supposed to be simple, automatically detects services, so you don’t need to write rules, has support for HTTP/2 and GRPC and has automatic cert renewal from LetsEncrypt. Oh and its got 19K+ stars on Github at the time of writing this.</li></ol><p>So, as the title suggests, I chose Traefik.</p><h3>Our setup</h3><p>We’ll be talking about this particular setup:</p><ol><li>Services are all running in dockers.</li><li>Multiple microservices, some GRPC and some normal REST.</li><li>DNS-01 based ACME challenge to get Certs for the domain.</li><li>Hosted on Google cloud (just environment variables for cert generation).</li></ol><p><strong>Note:</strong> This post isn’t about a complex setup with Kubernetes orchestration or an even more complex service mesh. Most online tutorials for Traefik are about that only and getting newbie friendly data is the hard part.</p><h3>Getting started with Traefik</h3><p>We’ll be using the latest traefik docker image (please make sure you have docker installed).</p><p>To get the latest image based out of scratch linux (cannot shell into it):</p><p>docker pull traefik</p><p>There are more images, such as the alpine image, nano server etc. You can find those images here: <a href="https://hub.docker.com/_/traefik">https://hub.docker.com/_/traefik</a></p><p>To understand the Traefik basics, please visit: <a href="https://docs.traefik.io/basics/">https://docs.traefik.io/basics/</a></p><p>Let’s create a docker network, which we’ll use with the other services as well.</p><p>docker network create proxy</p><p>If you’re on the same server, you don’t quite need this and can point your network to host and be done with it.</p><p>Traefik supports service discovery and can automatically detect your services running on the network if their dockers are started with the correct labels. We’re not taking this approach, since in a small startup environment, its mostly known which servers will spawn what service and changing a config is much more easy to manage.</p><h4>traefik.toml</h4><p>Traefik configs are done in TOML (<a href="https://github.com/toml-lang/toml">https://github.com/toml-lang/toml</a>), you have to mainly define your frontend, backend and your entry points. Let’s take a look at the amazing illustration on traefik.io</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-Yb2bY8EX-njxfy_.png" /><figcaption>Closer look at Traefik. Source: <a href="https://docs.traefik.io/">https://docs.traefik.io/</a></figcaption></figure><p>It’s a self explanatory image, but let’s see what it says. There are majorly three parts to Traefik:</p><ol><li><strong>Entrypoints:</strong> Can be http (80) or https(443) or any other custom entry point. You can even force a redirect to https from http entry points. Take a look at <a href="https://docs.traefik.io/basics/#entrypoints">https://docs.traefik.io/basics/#entrypoints</a></li><li><strong>Frontends:</strong> This is where you define frontend rules based on either hostname, path, header or a custom regex. <a href="https://docs.traefik.io/basics/#frontends">https://docs.traefik.io/basics/#frontends</a></li><li><strong>Backends:</strong> Connect the frontends to the backend services and APIs. <a href="https://docs.traefik.io/basics/#frontends">https://docs.traefik.io/basics/#frontends</a></li></ol><p>Time for the config (traefik.toml):</p><pre>defaultEntryPoints = [&quot;http&quot;, &quot;https&quot;]<br>logLevel = &quot;INFO&quot;</pre><pre>[traefikLog]</pre><pre>[accessLog]<br>  filePath = &quot;/var/log/access.log&quot;<br>  format = &quot;json&quot;</pre><pre>[entryPoints]<br>  [entryPoints.http]<br>  address = &quot;:80&quot;<br>  [entryPoints.https]<br>  address = &quot;:443&quot;<br>    [entryPoints.https.tls]</pre><pre>[api]</pre><pre>[file]</pre><pre>[acme]<br>email = &quot;<a href="/service/mailto:admin@example.com">admin@example.com</a>&quot;<br>storage = &quot;acme.json&quot;<br>entryPoint = &quot;https&quot;<br>acmeLogging = true<br>  [acme.dnsChallenge]<br>    provider = &quot;gcloud&quot;<br>  [[acme.domains]]<br>    main = &quot;*.example.com&quot;<br>    sans = [&quot;<a href="http://www.example.com/">www.example.com</a>&quot;]</pre><pre>[backends]<br>  [backends.foo]<br>    [backends.foo.servers.server1]<br>    url = &quot;h2c://127.0.0.1:3000&quot;<br>  [backends.bar]<br>    [backends.bar.servers.server1]<br>    url = &quot;<a href="http://127.0.0.1:3001/">http://127.0.0.1:3001</a>&quot;</pre><pre>[frontends]<br>  [frontends.foo]<br>  backend = &quot;foo&quot;<br>    [frontends.foo.routes.server1]<br>    rule = &quot;Host:foo.example.com&quot;<br>  passHostHeader = true<br>  passTLSCert = false<br>  [frontends.bar]<br>  backend = &quot;bar&quot;<br>    [frontends.bar.routes.server1]<br>    rule = &quot;Host:bar.example.com&quot;<br>  passHostHeader = true<br>  passTLSCert = false</pre><p>Alright, now let’s understand what’s happening here.</p><ul><li>defaultEntryPoints option tells Traefik to allow http and https by default. You can also set this up per frontend, using the entryPoints option.</li><li>I’ve setup logging in two ways here, one is access logs and the other are traefik logs. For my scenario, I wanted to be able to look at traefik logs directly by doing docker logs traefik and not specifying a file path outputs to STDOUT, which is what enables that. The access logs I store in a specific path, so they are not routed to STDOUT.</li><li>[api] is something you may or may not require. This enables the GUI on port 8080 by default. You can either open that port using password based security or you can enable it using a VPS.</li><li>[file] tells Traefik that the config resides in the config file and it should read those. If you don’t provide this option, the configs below might not work.</li><li>[acme] This is where we setup our acme certs. There are multiple ways to complete the challenge, for my case I wanted to utilise the DNS challenge. You might notice a provider, in my case it was Google Cloud, so the value is gcloud but it might vary depending on your provider. For a full list of qualified providers, visit <a href="https://docs.traefik.io/configuration/acme/#provider">https://docs.traefik.io/configuration/acme/#provider</a></li><li>[backends] defines the services on our servers. Traefik lets you specify a URL to the backend service, and it also lets you choose the protocol. For our purpose we’ve used two services, one runs on simple http and the other runs on GRPC which uses the protocol h2c. For secure backends that require custom certs, you must use https only.</li><li>[frontends] lets you define rules for reaching your backends. Be it path, subdomain or even a complex regex, you can define those here. In our case, we have defined two sub domains foo.example.com and bar.example.com .</li></ul><h3>Let’s start some dockers</h3><p>Now that we have a working TOML configuration for Traefik, let’s go ahead and start Traefik.</p><p>Let’s first create an empty acme.json file.</p><pre>touch acme.json</pre><p>We create this to generate the certs outside the docker container. And use it later too if the docker is removed or recreated for some reason.</p><pre>docker run --rm -d -p 8080:8080 -p 80:80 -p 443:443 \<br>-v /path/to/traefik.toml:/traefik.toml \<br>-v /var/run/docker.sock:/var/run/docker.sock \<br>-v /path/to/gca.json:/gca.json \<br>-v /path/to/acme.json:/acme.json \<br>-l traefik.frontend.rule=Host:local \<br>-l traefik.port=8080 \<br>--network proxy \<br>--name traefik \<br>-e GCE_PROJECT=&quot;project_name-123456&quot; \<br>-e GCE_SERVICE_ACCOUNT_FILE=&quot;/gca.json&quot; \<br>traefik</pre><p>Now this is a fully qualified docker command to run Traefik on GCE. Let’s go through this command and see what’s done and why?</p><ol><li>We’ve mapped 3 ports, 8080 for the API, 80 &amp; 443 for http and https respectively.</li><li>We have mounted 4 volumes, the TOML configuration, docker.sock file so that Traefik can still listen to newer services getting added and it automatically starts routing them, gca.json is the service account file and acme.json is the JSON file we created before to store the certs.</li><li>The network proxy is chosen using --network proxy</li><li>Two environment variables are provided. These environment variables are the cloud provider specific variables. For Google cloud, these two are required, and GCE_PROJECT tells the project name, while GCE_SERVICE_ACCOUNT_FILE provides the path of the service account file inside the docker, which we mounted at /gca.json .</li><li>We give it the name traefik and start the image called traefik that we pulled from docker hub in the beginning.</li></ol><p>Once you start docker, monitor traefik logs by typing docker logs traefik and you’ll start seeing ACME TLS cert process starting and you’ll also see the acme.json file filling up.</p><p><strong>Note: </strong>Make sure you have an A name record for both, your domain and the sans domain you’ve provided, LetsEncrypt will try both and the DNS resolution is required for both. You do not need a sans domain, and autocert can work without it too.</p><p>If it goes through, you’ll receive an email from LetsEncrypt telling you that your domain has got a new certificate. Autocert will also make sure that your certs don’t expire and will renew them 15 days before expiry.</p><h3>Securing the clients</h3><p>Now that we’ve successfully setup SSL termination at the server end using Traefik, it’s time for our apps to start asking for the secure connection.</p><p>For this example, I’m going to be showing dart code snippets to connect to a now secure GRPC service.</p><p>Let’s see the original connection class:</p><pre><strong>class </strong>GrpcClientSingleton {<br>  ClientChannel client;<br>  <strong>static final </strong>GrpcClientSingleton <em>_singleton </em>=<br>      <strong>new </strong>GrpcClientSingleton._internal();<br><br>  <strong>factory </strong>GrpcClientSingleton() =&gt; <em>_singleton</em>;<br><br>  GrpcClientSingleton._internal() {<br>    client = ClientChannel(Constants.<em>SERVICE_URL</em>,<br>        port: Constants.<em>SERVICE_PORT</em>,<br>        options: ChannelOptions(<br>          credentials: ChannelCredentials.insecure(),<br>        ));<br>  }<br>}</pre><p>Let’s modify it to use secure a connection but also an insecure for cases when the build environment is dev or staging. Please read:</p><p><a href="https://medium.com/meeve/build-variants-in-flutter-for-multiple-backend-environments-7e139128949b">Build variants in Flutter for multiple backend environments</a></p><p>to get an idea of how to create build variants for different environments.</p><p>The modified class would look something like this:</p><pre><strong>class </strong>GrpcClientSingleton {<br>  ClientChannel client;<br>  <strong>static final </strong>GrpcClientSingleton <em>_singleton </em>=<br>      <strong>new </strong>GrpcClientSingleton._internal();<br><br>  <strong>factory </strong>GrpcClientSingleton() =&gt; <em>_singleton</em>;<br><br>  GrpcClientSingleton._internal() {<br>    client = ClientChannel(Constants.<em>SERVICE_URL</em>,<br>        port: Constants.<em>SERVICE_PORT</em>,<br>        options: ChannelOptions(<br>          credentials: Constants.<em>CURRENT_ENVIRONMENT </em>== Environment.PROD<br>              ? ChannelCredentials.secure()<br>              : ChannelCredentials.insecure(),<br>        ));<br>  }<br>}</pre><p>Now, based on what environment rules are needed, one can configure their singleton based on that. For me, it’s a simple prod = secure, else insecure.</p><p>The constants.dart for this scenario would look something like this:</p><pre>import &#39;package:flutter/foundation.dart&#39;;</pre><pre>enum Environment { DEV, STAGING, PROD }</pre><pre>class Constants {</pre><pre>static get SERVICE_URL {<br>    return _config[_Config.SERVICE_URL];<br>  }</pre><pre>static get SERVICE_PORT {<br>    return _config[_Config.SERVICE_PORT] as int;<br>  }</pre><pre>static void setEnvironment(Environment env) {<br>    switch (env) {<br>      case Environment.DEV:<br>        _config = _Config.debugConstants;<br>        break;<br>      case Environment.STAGING:<br>        _config = _Config.qaConstants;<br>        break;<br>      case Environment.PROD:<br>        _config = _Config.prodConstants;<br>        break;<br>    }<br>  }</pre><pre>static get CURRENT_ENVIRONMENT {<br>    return _config[_Config.ENV] as Environment;<br>  }<br>}</pre><pre>class _Config {<br>  static const SERVICE_URL = &quot;SERVICE_URL&quot;;<br>  static const SERVICE_PORT = &quot;SERVICE_PORT&quot;;<br>  static const ENV = &quot;ENV&quot;;</pre><pre>static Map&lt;String, dynamic&gt; debugConstants = {<br>    ENV:Environment.DEV,<br>    SERVICE_URL: &quot;192.168.1.2&quot;,<br>    SERVICE_PORT: 3000<br>  };</pre><pre>static Map&lt;String, dynamic&gt; qaConstants = {<br>    ENV:Environment.STAGING,<br>    SERVICE_URL: &quot;foo.omgstaging.com&quot;,<br>    SERVICE_PORT: 3000<br>  };</pre><pre>static Map&lt;String, dynamic&gt; prodConstants = {<br>    ENV:Environment.PROD,<br>    SERVICE_URL: &quot;foo.example.com&quot;,<br>    SERVICE_PORT: 443<br>  };<br>}</pre><p>Here the staging and dev environments have a direct connection to the service, whereas the production system will be connected over port 443 using SSL.</p><p>If you’ve successfully managed to do this, your flutter GRPC based app is now running on a secure connection. Congratulations 🎉.</p><h4>Notes:</h4><ol><li>We have terminated SSL on the proxy (LB) end only, because we trust the internal communications. You might want to encrypt those too depending on your environment.</li><li>All services must be run on the network proxy for traefik be able to route those. You can also simply start Traefik on the network host if everything is on a single machine and you don’t need the complexity right now.</li><li>Google cloud provider explicitly needs access to the service account file. I couldn’t find a method to do this without it. And even this took a long while to figure out.</li><li>Nginx is simpler to setup than this, and doesn’t require any specific configs. You can setup a cronjob to do the autocert part using the certbot tool. Traefik once setup correctly, takes the headache off of managing a lot of “enabled websites” files and does cert management automatically.</li><li>Traefik does a lot of things that are not covered here, for things like LB, healthcheck etc, visit <a href="https://traefik.io/">https://traefik.io</a></li><li>HTTP traffic can be redirected to HTTPS automatically in Traefik but I have chosen to not include that option, because some people might be running older apps, and for that they need to allow http connections, and GRPC will simply refuse to connect to a secure channel using an insecure connection.</li><li>If you are going to be shipping your iOS flutter app to the app store, you should start selecting app uses secure transport not powered by ATS and then choose HTTPS transport. This self declaration is required and if you choose no HTTPS, then Apple might block your app.</li></ol><p>Hope this guide helps a few people who are in search of setting up their apps with GRPC and securing them. It was a major pain point while setting up Meeve’s services and took a lot of trial and error, to set it up sort of securely.</p><p>Meeve is a hyperlocal platform that connects similar people through events happening nearby. Checkout Meeve at:</p><p><a href="https://meeve.page.link/invite">Meeve - Connecting People Through Events</a></p><p>Thanks for reading through, have a great day! 😀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc383c0b9d55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/meeve/startup-friendly-guide-to-securing-grpc-connections-using-traefik-cc383c0b9d55">Startup friendly guide to securing GRPC connections using Traefik</a> was originally published in <a href="https://medium.com/meeve">Meeve</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build variants in Flutter for multiple backend environments]]></title>
            <link>https://medium.com/meeve/build-variants-in-flutter-for-multiple-backend-environments-7e139128949b?source=rss-587ac0efe474------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e139128949b</guid>
            <category><![CDATA[build]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[flutter]]></category>
            <dc:creator><![CDATA[Ishaan Bahal]]></dc:creator>
            <pubDate>Wed, 26 Dec 2018 18:37:40 GMT</pubDate>
            <atom:updated>2018-12-26T18:37:40.709Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of months ago, I started developing Meeve with Flutter. I’ve worked with android in the past and was used to having build variants (flavours) that could target different backends. Flutter didn’t come with any such thing and I had to figure out stuff myself. So, this post would be a small one, to help prepare your apps for actual releases and not playing around on a laptop and for teams that have testers that need different builds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QkLBz6xn7WkHs89A" /><figcaption>Photo by <a href="https://unsplash.com/@mikesetchell?utm_source=medium&amp;utm_medium=referral">Mike Setchell</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What are build variants and why would you need them?</h3><p>A flavour or a build variant can be multiple things, it can be used to target different device architectures or different backends. Android presents a separate distinction to both, so you can target different Android API versions and well as different backend support, for iOS I’m not quite certain, but it also does support these in some fashion.</p><p>I needed a solution to build my app, and test on different backend environments without having to change constants every time! To do this, I needed a more dart based solution and not dependent on platforms. Now keeping it in dart also meant that I couldn’t use the features that platforms ship with and most certainly cannot use different build names on Android to deploy multiple apps on the same hardware. But this approach worked for me, and it will work for any target platform in future since its dart only and is just a hook before the <em>main.dart</em> process.</p><p>For people building with CI systems, this method might be helpful since a single command would make sure the correct backend values are being bundled into the code for every environment the CI system wants to create a build for.</p><h3>Modifying main.dart to support variants</h3><p>As I said, the process needed to be dart only, and the entry point to the app is <em>main.dart</em>, in fact while running or building, you usually do this</p><pre>flutter build --apk --release</pre><p>but you can also do this</p><pre>flutter build --apk -t lib/main.dart --release</pre><p>This method actually tells the build system to use lib/main.dart as the entry point for the flutter app, and this is what we’ll modify.</p><p>So, let’s say we need 3 environments, dev, staging and prod. Dev would be the local environment, staging would be a separate config of staging servers and prod would be the production config.</p><p>Let’s see an example of environment based build file</p><pre><strong>import </strong>&#39;package:build_amaze/constants/constants.dart&#39;;<br><strong>import </strong>&#39;package:build_amaze/main.dart&#39;;<br><br><strong>void </strong>main(){<br>  Constants.<em>setEnvironment</em>(Environment.DEV);<br>  mainDelegate();<br>}</pre><p>This is main_dev.dart and similarly, main_staging.dart and main_prod.dart follow the same logic and just change the value to Environment.STAGING and Environment.PROD.The actual logic for environment selection and values lies with constants.dart, we’ll look at it in a bit, but first here’s a look at main.dart</p><pre><strong>import </strong>&#39;package:build_amaze/constants/constants.dart&#39;;<br><strong>import </strong>&#39;package:flutter/material.dart&#39;;<br><br><strong>void </strong>mainDelegate() =&gt; runApp(MyApp());<br><br><strong>class </strong>MyApp <strong>extends </strong>StatelessWidget {<br>  // This widget is the root of your application.<br>  @override<br>  Widget build(BuildContext context) {<br>    <strong>return </strong>MaterialApp(<br>        title: &#39;Build env - ${Constants.<em>WHERE_AM_I</em>}&#39;,<br>        theme: ThemeData(<br>          primarySwatch: Colors.<em>purple</em>,<br>        ),<br>        home: Scaffold(<br>          appBar: AppBar(<br>            title: Text(&#39;Build env - ${Constants.<em>WHERE_AM_I</em>}&#39;),<br>          ),<br>      body: Center(<br>          child: Column(<br>            mainAxisAlignment: MainAxisAlignment.center,<br>            crossAxisAlignment: CrossAxisAlignment.center,<br>            children: &lt;Widget&gt;[<br>              Text(&#39;Build env - ${Constants.<em>WHERE_AM_I</em>}&#39;,),<br>              Divider(),<br>              Text(&quot;Server 1: ${Constants.<em>SERVER_ONE</em>}&quot;),<br>              Divider(),<br>              Text(&quot;Server 2: ${Constants.<em>SERVER_TWO</em>}&quot;),<br>              Divider(),<br>              Constants.<em>WHERE_AM_I </em>== &quot;prod&quot;<br>                  ? Text(&quot;OMG! Such a prod!&quot;)<br>                  : Text(&quot;You&#39;re still testing man!&quot;),<br>            ],<br>          ))),<br>      debugShowCheckedModeBanner: <strong>false</strong>,<br>    );<br>  }<br>}</pre><p>So the only change here is I renamed main() method to mainDelegate() so no one can accidentally run this and the exported method is used by all three of my environment based main files.</p><p>Now, let’s take a look at the constants.dart file.</p><pre><strong>enum </strong>Environment { DEV, STAGING, PROD }<br><br><strong>class </strong>Constants {<br>  <strong>static </strong>Map&lt;String, <strong>dynamic</strong>&gt; <em>_config</em>;<br><br>  <strong>static void </strong><em>setEnvironment</em>(Environment env) {<br>    <strong>switch </strong>(env) {<br>      <strong>case </strong>Environment.DEV:<br>        <em>_config </em>= _Config.<em>debugConstants</em>;<br>        <strong>break</strong>;<br>      <strong>case </strong>Environment.STAGING:<br>        <em>_config </em>= _Config.<em>qaConstants</em>;<br>        <strong>break</strong>;<br>      <strong>case </strong>Environment.PROD:<br>        <em>_config </em>= _Config.<em>prodConstants</em>;<br>        <strong>break</strong>;<br>    }<br>  }<br><br>  <strong>static get </strong><em>SERVER_ONE </em>{<br>    <strong>return </strong><em>_config</em>[_Config.<em>SERVER_ONE</em>];<br>  }<br><br>  <strong>static get </strong><em>SERVER_TWO </em>{<br>    <strong>return </strong><em>_config</em>[_Config.<em>SERVER_TWO</em>];<br>  }<br><br>  <strong>static get </strong><em>WHERE_AM_I </em>{<br>    <strong>return </strong><em>_config</em>[_Config.<em>WHERE_AM_I</em>];<br>  }<br>}<br><br><strong>class </strong>_Config {<br>  <strong>static const </strong><em>SERVER_ONE </em>= &quot;SERVER_ONE&quot;;<br>  <strong>static const </strong><em>SERVER_TWO </em>= &quot;SERVER_TWO&quot;;<br>  <strong>static const </strong><em>WHERE_AM_I </em>= &quot;WHERE_AM_I&quot;;<br><br>  <strong>static </strong>Map&lt;String, <strong>dynamic</strong>&gt; <em>debugConstants </em>= {<br>    <em>SERVER_ONE</em>: &quot;localhost:3000/&quot;,<br>    <em>SERVER_TWO</em>: &quot;localhost:8080/&quot;,<br>    <em>WHERE_AM_I</em>: &quot;local&quot;,<br>  };<br><br>  <strong>static </strong>Map&lt;String, <strong>dynamic</strong>&gt; <em>qaConstants </em>= {<br>    <em>SERVER_ONE</em>: &quot;https://staging1.example.com/&quot;,<br>    <em>SERVER_TWO</em>: &quot;https://staging2.example.com/&quot;,<br>    <em>WHERE_AM_I</em>: &quot;staging&quot;,<br>  };<br><br>  <strong>static </strong>Map&lt;String, <strong>dynamic</strong>&gt; <em>prodConstants </em>= {<br>    <em>SERVER_ONE</em>: &quot;https://itsallwidgets.com/&quot;,<br>    <em>SERVER_TWO</em>: &quot;https://flutter.io/&quot;,<br>    <em>WHERE_AM_I</em>: &quot;prod&quot;<br>  };<br>}</pre><p>Now as you can see, for any value I need in my code, I have to create a getter in the constants method, and it fetches the value from the selected map that was decided during the build stage. It may seem a bit too much to keep writing all the getters and map values, but it is sort of convenient once setup properly.</p><p>Now let’s take a look at making this work correctly with our usual workflow in Android Studio (or IntelliJ).</p><h3>Android Studio support for custom entry point</h3><p>For android studio build support, just create multiple custom configurations by going to the edit configuration panel.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*uFH7SOiJIeuCncgJ80KdAA.png" /><figcaption>Edit Configuration in Android Studio</figcaption></figure><p>Then create a new flutter configuration by clicking the (+) icon on the top left and selecting flutter from there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SXEza1XwCG0YzcE1h-sQ_Q.png" /><figcaption>Configuration for the dev build</figcaption></figure><p>Create similar configurations for all the environments you have.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*xv65NQoO2lwa6-xPM3ycHA.png" /></figure><p>And now run any. It will work as it used to before, hot reload and everything and now you have support for multiple environments. You can also share the workspace preferences with other people so they do not have to set these up again.</p><p>I’ve not tried this with Visual Studio Code, but if it supports build configurations, then the process should be very similar to this one.</p><h3>Note:</h3><ul><li>If you have shared preferences, or some stored media, or logged in accounts, just clear the app preferences or uninstall it before changing the environment.</li><li>Changing the environment will cause a complete rebuild and will cause the app to restart completely.</li><li>This also modifies the generated.xcconfig file that’s required for iOS release. So before you make a release with XCode, make sure you run flutter build -t lib/main_prod.dart --release at least once separately before archiving the build.</li></ul><p><strong>Tip:</strong> It’s always good to write a small build script that has all the final build steps in it. Mine contains the build commands for both Android and iOS, I run it once, and wait a while, collect the apk and then I start the archive process on XCode to upload to iTunes.</p><p>Here’s the app running using all three configs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*y4sJSV7V1Nd5zwS8dr6voQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*r5b8O3bkzaILVFpYEV8uSA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*uc0dU3TM-go-_CmwjK0EjA.png" /><figcaption>App running on different build environments</figcaption></figure><p>The above method is an amateur attempt at making multiple configs works. It borrows from concepts shared on</p><p><a href="https://stackoverflow.com/questions/47438564/how-do-i-build-different-versions-of-my-flutter-app-for-qa-dev-prod">How do I build different versions of my Flutter app for qa/dev/prod?</a></p><p>and builds upon them. The code may not be quite secure or well written, I’m not very good at dart. But this method does work and requires very little effort to make it so. :)</p><p>And, I’ve used a similar process to setup build for Meeve, the product that I work on and have been able to test and release the app on both platforms using multiple environments.</p><p>Checkout Meeve, a hyperlocal platform to help you discover people nearby through events, available on iOS and Android!</p><p><a href="https://www.meeve.co/">Meeve - Connecting People Through Events</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e139128949b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/meeve/build-variants-in-flutter-for-multiple-backend-environments-7e139128949b">Build variants in Flutter for multiple backend environments</a> was originally published in <a href="https://medium.com/meeve">Meeve</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flutter ❤ GRPC]]></title>
            <link>https://medium.com/flutter-community/flutter-grpc-810f87612c6d?source=rss-587ac0efe474------2</link>
            <guid isPermaLink="false">https://medium.com/p/810f87612c6d</guid>
            <category><![CDATA[http2]]></category>
            <category><![CDATA[grpc]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[protobuf]]></category>
            <dc:creator><![CDATA[Ishaan Bahal]]></dc:creator>
            <pubDate>Thu, 01 Nov 2018 18:19:57 GMT</pubDate>
            <atom:updated>2018-11-04T01:02:42.563Z</atom:updated>
            <content:encoded><![CDATA[<h4>Using flutter with a GRPC backend and living the HTTP/2 life!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*PQg8qyDNQCLySZCE4so_kw.png" /></figure><h4>Preface</h4><p>A few months ago, I decided to write an app for finding people around you with similar interests with the help of events. Having experience only in Android development, I couldn’t attempt to do purely native development for both Android and iOS, so I decided to go with something that targets both and requires the least amount of platform specific code. Choices:</p><ol><li>React Native (Sort of an industry standard right now)</li><li>Flutter (In beta at the time, now RC2)</li></ol><p>I had used react-native for a couple of projects before, and I really loved RN for that, but there were so many issues with RN, and the whole trying to use native components by wrapping them and calling them over a JS bridge. That does bring a lot of inconsistencies with the native platform and react native’s wrapper. So, I decided to pick up Flutter.</p><p>With flutter, things were a bit different:</p><ol><li>First I needed to learn dart, which seemingly isn’t all that bad, and quite easy to pick up. It has a syntax most people are familiar with (ES derived maybe?) and has a bunch on syntactic sugar on top of it, like arrow functions, argument only constructors that don’t require manual wiring etc.</li><li>The structure of flutter apps is something that takes time getting used to, and believe me, I am still not used to it. With android you see things organised into views, controller and models, but with flutter everything is a widget, quite literally, which makes things complex quite easily if you don’t force yourself to cut components out to their separate widgets and create services etc. So it takes a bit of time getting used to.</li><li>And then there is plugins, flutter mostly acts like a view framework of sorts and does lack a lot of basic functionality right now, like geolocation, local-notifications, handling of deep links and everything is handled by either third party plugins or some firebase plugins for now. Even maps lacks a first party plugin, its coming soon, follow this issue if you’re looking for that <a href="https://github.com/flutter/flutter/issues/19030">https://github.com/flutter/flutter/issues/19030</a></li></ol><p>But with flutter, more precisely dart, there was complete support for GRPC, which JS lacked at that time, now there is <a href="https://github.com/grpc/grpc-web">grpc-web</a> by the same team that built GRPC.</p><p>Anyway, let’s get to the main point of discussion, using GRPC with Flutter. When I decided I want to build a flutter app, I was also playing around with GRPC as a backend choice in GoLang. But the internet lacked any such documentation or blogposts of people attempting it. So I though I should do a small tutorial on making a simple app with a GRPC backend. I think I will be covering some more advanced topics like token (JWT) based auth, streams, TLS based authentication from apps etc in upcoming posts, but in this one, let’s build a simple hello world style app that talks to a GRPC server.</p><h4>Protocol buffers</h4><p>To get started with creating your first flutter project, follow the guide here <a href="https://flutter.io/get-started/install/">https://flutter.io/get-started/install/</a></p><p>Flutter code-labs and docs are the best resources to learn about flutter, so pay them a visit if you’re stuck somewhere.</p><p>Let’s first write the proto definitions and a simple Go backend.</p><p>The directory structure is somewhat like this:</p><pre>hello-grpc/<br>    - model/<br>        - gen/<br>            - ship/<br>                - dart/<br>            - hello.pb.go<br>        - protodefs/<br>            - hello.proto<br>        - gen/<br>    - makefile<br>    - main.go</pre><p>The proto file, <strong>hello.proto</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/121a26c54d91636c2c3d85d981ecaf6b/href">https://medium.com/media/121a26c54d91636c2c3d85d981ecaf6b/href</a></iframe><p>So let’s generate the Go and dart proto stubs.</p><p>Note: For GoLang, its pretty straight forward, but for dart, if you use Google’s own proto implementations like I used <strong>google.protobuf.Empty</strong>, then you also need to ship all Google’s implementation stubs along with your own. Related issue: <a href="https://github.com/grpc/grpc-dart/issues/76">https://github.com/grpc/grpc-dart/issues/76</a></p><p>To generate the proto files, I use a simple makefile, to make things easier and forget the actual commands ;)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54f896f54d91ace8a52743e0dd4655e0/href">https://medium.com/media/54f896f54d91ace8a52743e0dd4655e0/href</a></iframe><p>Now simply generate the code using</p><pre>make gen</pre><p>That will create go and dart stubs in `model/gen` and `model/gen/ship/dart` respectively.</p><p>Check out the simple go server here</p><p><a href="https://github.com/ishaanbahal/hello-grpc">ishaanbahal/hello-grpc</a></p><h4>Flutter</h4><p>Moving on to the flutter side of things. Assuming you have it all setup, let’s go ahead and plug GRPC code into it.</p><p>GRPC clients are smart, that means you generally don’t need to either manage connections or pooling for the clients. It automatically decides how many parallel steams/connections it needs over HTTP/2 and one doesn’t need to worry about that. It can even do client side load balancing using look-aside LB or a service discovery, check <a href="https://grpc.io/blog/loadbalancing">https://grpc.io/blog/loadbalancing</a></p><p>To create a client implementation, copy the generated proto stubs for dart to your main project, I try to keep my code structure as follows:</p><pre>helloworld/<br>    - android/<br>    - ios/<br>    - lib/<br>        - assets/<br>        - common/<br>            - grpc_commons.dart # holds the singleton<br>        - model/<br>            - google/<br>            - hello.pb.dart<br>            - hello.pbenum.dart<br>            - hello.pbgrpc.dart<br>            - hello.pbjson.dart<br>        - services/<br>            - hello_service.dart<br>        - views/<br>            - hello_world.dart<br>        - main.dart<br>    - pubspac.yaml</pre><p>I’ll try to explain which file holds what and then we can see the code for them. There are 4 important things here:</p><ol><li>gprc_commons.dart Holds the code for a singleton object to the GRPC client, and the client itself takes care of reconnection, pooling and everything else. For now, to keep it simple, we have just used the client connection in non secure mode and no authentication.</li><li>hello_service.dart I try to do some sort of separation of concern in dart code, I hate everything in one file, so all the network calls go in a services folder, and this service basically calls the RPC SayHello .</li><li>main.dart &amp; views , main.dart should just be the entry point to the app, and then further views or routes should be separately managed. To save some time in this codebase I’ve written everything in main.dart, but as the project grows, it’s always better to just break it into separate view, components (re-usable) and routing logics.</li><li>pubspec.yaml is the main file for dependency management in flutter, and honestly is super smooth when coming from the super complicated dependency hell “Gradle”. This is where you will add dependencies for protobuf and grpc .</li></ol><p>Let’s see some code samples, incoming Github gist!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81a9b917488693b23eddec8c00644a76/href">https://medium.com/media/81a9b917488693b23eddec8c00644a76/href</a></iframe><p>If you’ve managed to get this far, and have a grpc server up and running, let’s see what the amazing hello app looks like</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OmdTQA5ZQEqgezUE30E2xw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nPET4tidU5NMB5BkgPBD3w.png" /><figcaption>Server says hi!</figcaption></figure><p>So, if the above worked for you, I guess it should be a great starting point for you to begin some hacking around with GRPC and flutter.</p><p>I will be posting about other topics ranging from GRPC TLS connection to servers from flutter apps, deploying your backend apps behind K8s and using that as a service discovery for GRPC client side load balancing. I’ve had to figure these things out in a pretty hard fashion, so I’d love to share whatever knowledge I’ve gained over the past few months.</p><p>Flutter documentation is great but sometimes it can be a pain to understand a particular widget which has no examples at all, or is just super hard to understand, and since StackOverflow is still not quite there yet for Flutter, its always better to visit flutter and search your particular issue, just remember to remove the is:open filter from search.</p><p>This has been a long post, and my first actual blog entry at Medium. Will try to keep the next ones and concise.</p><p>You can find the flutter codebase here</p><p><a href="https://github.com/ishaanbahal/hello_grpc_flutter">ishaanbahal/hello_grpc_flutter</a></p><p>As I mentioned, I’ve been working with Flutter &amp; GRPC for a product that we’re now beta testing with public participants. If you’re interested in trying it out, please visit the PlayStore at:</p><ul><li><a href="https://play.google.com/store/apps/details?id=co.meeve.kratos">Meeve - Apps on Google Play</a></li><li><a href="https://twitter.com/FlutterComm">JavaScript is not available.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=810f87612c6d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flutter-community/flutter-grpc-810f87612c6d">Flutter ❤ GRPC</a> was originally published in <a href="https://medium.com/flutter-community">Flutter Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>