<?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: Dom</title>
    <description>The latest articles on DEV Community by Dom (@dompolzer).</description>
    <link>https://dev.to/dompolzer</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%2F823177%2Fe98e6835-a9ba-4eb1-afad-c59900c7f051.jpeg</url>
      <title>DEV Community: Dom</title>
      <link>https://dev.to/dompolzer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dompolzer"/>
    <language>en</language>
    <item>
      <title>Rate limiting API calls in cloud</title>
      <dc:creator>Dom</dc:creator>
      <pubDate>Fri, 10 Jun 2022 19:07:14 +0000</pubDate>
      <link>https://dev.to/dompolzer/rate-limiting-service-api-calls-in-cloud-4g17</link>
      <guid>https://dev.to/dompolzer/rate-limiting-service-api-calls-in-cloud-4g17</guid>
      <description>&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; This post covers the process of setting up &lt;a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-key-concepts"&gt;API Management&lt;/a&gt; (Layer 7 Load balancer) in Azure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt; It will also cover how to setup Subscription Key rate limited endpoint which would set you up for providing API SAAS.&lt;/p&gt;

&lt;p&gt;I wanted to document this process when I was setting up a custom rate limited search cluster that was called from My fronted App while providing 2 endpoints.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1st endpoint being free.&lt;/li&gt;
&lt;li&gt;2nd endpoint being available only with valid API key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post assumes you have already created &lt;a href="https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/manage-resource-groups-portal"&gt;Azure resource group&lt;/a&gt; and also deployed your backend service as &lt;a href="https://docs.microsoft.com/en-us/azure/app-service/overview"&gt;Azure app service&lt;/a&gt; inside it.&lt;/p&gt;

&lt;p&gt;After you deploy your App Service and API Management service to your Resource group, it will look similar to this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TAQlpBz7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ftccohsfq32swfw6t3t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TAQlpBz7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ftccohsfq32swfw6t3t.png" alt="Image description" width="880" height="161"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Importing and publishing your first API is explained &lt;a href="https://docs.microsoft.com/en-us/azure/api-management/import-and-publish"&gt;HERE&lt;/a&gt;
&lt;/h4&gt;


&lt;h2&gt;
  
  
  Start by importing an Azure Web App as an API.
&lt;/h2&gt;

&lt;p&gt;Which is explained &lt;a href="https://docs.microsoft.com/en-us/azure/api-management/import-app-service-as-api"&gt;HERE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ReYDePHP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0ml48ls4pmjp1f1sylb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ReYDePHP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0ml48ls4pmjp1f1sylb.png" alt="Image description" width="880" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that you can click on your added API endpoint  and provision your desired policies.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d02hV5Po--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lsu80fkatatb9ydoh27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d02hV5Po--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lsu80fkatatb9ydoh27.png" alt="Image description" width="880" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that you can set if API Key is needed to Access your endpoint.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XMgaBtmh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxhdvxs0ngm2u5mvbozn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XMgaBtmh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxhdvxs0ngm2u5mvbozn.png" alt="Image description" width="880" height="424"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Endpoint policies exported as XML
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Paid endpoint LB policy:
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;"set-backend-service" is set by default when we added New API attached to the App service.&lt;/li&gt;
&lt;li&gt;"quota-by-key" here is set to track successful 200 OK responses and also to allow max 1000 API calls every 29 days (2505600 sec)&lt;/li&gt;
&lt;li&gt;"set-header" is appended in Load balancer before request reaches backend service as this custom header is required by my service&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Free endpoint LB policy:
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;"rate-limit-by-key" is actually IP rate limit allowing 5 requests every 300sec &lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  CORS Policy on all endpoints:
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;here we set which HTTP methods are allowed&lt;/li&gt;
&lt;li&gt;which domains are allowed to access our endpoints&lt;/li&gt;
&lt;li&gt;"preflight-result-max-age="300"" caches our CORS preflight (for  300sec) request so that each subsequent request from Frontend App has one less network hop to do.&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  With this setup we would block immediately on Load balancer endpoint (even before it reaches App service endpoints).
&lt;/h5&gt;




&lt;p&gt;&lt;strong&gt;Beware&lt;/strong&gt; by default APIM puts CORS policy on Load balancer endpoint(effects all API Endpoints)… so check and update default if needed.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAWabpvJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fon76itbkofwczqvdrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAWabpvJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6fon76itbkofwczqvdrg.png" alt="Image description" width="880" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Policies documentation:
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-cross-domain-policies"&gt;Cross domain policies&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-policies#advanced-policies"&gt;Advanced Policies&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  CORS caching : &lt;a href="https://httptoolkit.tech/blog/cache-your-cors/"&gt;Learn more&lt;/a&gt;
&lt;/h4&gt;

&lt;h4&gt;
  
  
  Preflight OPTIONS request example
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jwIt-yyk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgvrvcj7lwzivdhflnvn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jwIt-yyk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wgvrvcj7lwzivdhflnvn.png" alt="Image description" width="523" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Managing API key subscriptions
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fKC2Isbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms8n9nr2tvgo6bg243kx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fKC2Isbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms8n9nr2tvgo6bg243kx.png" alt="Image description" width="880" height="421"&gt;&lt;/a&gt;&lt;br&gt;
MORE About it &lt;a href="https://docs.microsoft.com/en-us/azure/api-management/api-management-subscriptions"&gt;HERE&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Contact
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://twitter.com/dompolzer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/dominik-polzer-hi-o/"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>azure</category>
      <category>api</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Generating HMTL and MD files from .TXT in GO</title>
      <dc:creator>Dom</dc:creator>
      <pubDate>Sun, 13 Mar 2022 16:53:09 +0000</pubDate>
      <link>https://dev.to/dompolzer/generating-hmtl-and-md-files-from-txt-in-go-59lh</link>
      <guid>https://dev.to/dompolzer/generating-hmtl-and-md-files-from-txt-in-go-59lh</guid>
      <description>&lt;p&gt;In this post I'll be describing the code which I wrote to generate HTML (which can be hosted anywhere) and Markdown files from simple .txt file.&lt;/p&gt;

&lt;h3&gt;
  
  
  Idea for project
&lt;/h3&gt;

&lt;p&gt;Idea for this project came from a need to quickly generate and share some basic documentation about task or bug needed to be solved by team member.&lt;/p&gt;

&lt;p&gt;Working mostly as backend engineer, naturally my 1st idea was to build CLI tool that could easily do this by inputting file path and letting the code do the rest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why GO
&lt;/h3&gt;

&lt;p&gt;I never wrote Go code before but I watched few Go conf's and talks that sparked interest so this was me trying it out.&lt;/p&gt;

&lt;p&gt;I really loved how simple it is to execute code in parallel in GO with &lt;a href="https://go.dev/tour/concurrency/1"&gt;goroutines&lt;/a&gt;, &lt;a href="https://go.dev/tour/concurrency/2"&gt;channels&lt;/a&gt; &amp;amp; &lt;a href="https://gobyexample.com/waitgroups"&gt;wait groups&lt;/a&gt;, so that was the reason for doing this in GO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Main idea
&lt;/h3&gt;

&lt;p&gt;Main idea I followed for this project was to read from .txt file line by line and at the same time write to MD &amp;amp; HTML files using Go's goroutines &amp;amp; channels.&lt;br&gt;
This worked out fine for the most part, but I had to make some workarounds for 3rd party libraries which required code blocks and tables syntax to be passed all at the same time instead of line by line.&lt;br&gt;
So I had to come up with a way to detect code blocks and tables, than concatenate them into single block using Go's &lt;a href="https://pkg.go.dev/strings#Builder"&gt;strings.Builder&lt;/a&gt; package.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3rd party libraries:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/yuin/goldmark"&gt;goldmark&lt;/a&gt;&lt;br&gt;
for converting MD syntax to HTML&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/alecthomas/chroma/quick"&gt;quick&lt;/a&gt;&lt;br&gt;
for generating Html and syntax highlighting code blocks&lt;/p&gt;
&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;p&gt;Entry point to CLI tool:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
In above code I'm declaring map/dictionary of all available custom mappings I will be exposing and parsing from .txt file to MD &amp;amp; HTML format.

&lt;p&gt;Than I'm validating user input Args from CLI in 'shouldExit' func.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Than I'm validating file path and extension.

&lt;p&gt;After that I'm creating 2 channels.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;One 'writerChannel' that I'm reading/writing from.&lt;/li&gt;
&lt;li&gt;And 'filesCreated' that I'm using to signal when reading/writing is done to main goroutine so it can stop blocking and continue executing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Reading/writing to files
&lt;/h4&gt;

&lt;p&gt;In separate goroutines&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h4&gt;
  
  
  Detecting Table and Code blocks
&lt;/h4&gt;

&lt;p&gt;This is the code that handles writing to MD &amp;amp; html formats.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
'htmlStleBuilder' function handles what theme and styling to construct from static files based on passed CLI Args.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;blockquote&gt;
&lt;p&gt;err := quick.Highlight(...)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;line handles Html syntax highlighting generation for code blocks.&lt;/p&gt;

&lt;p&gt;And finally signaling to main goroutine by reading 'done' from 'filesCreated' channel that it can proceed executing after writing and reading is complete.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;lt;-filesCreated&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://github.com/dommyrock/go-txtToMD/blob/b540ed468738b168dfecf61dc496b30a2c638177/library/files/fileProcessing.go#L135"&gt;Full method on Github&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Source code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/dommyrock/go-txtToMD"&gt;Github Repo&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Running code locally: (with test files form ./testData directory)
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Generate Html/md files to %HOMEDIR%/Downloads directory:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run . ./testData/test.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;selecting custom theme:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run . ./testData/test.txt mid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Getting all currently available mappings:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run . -options (or -o)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Currently line break/newline is expected between each mapping element to be parsed into MD/HTML correctly!&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h4&gt;
  
  
  Running CLI command globally:
&lt;/h4&gt;

&lt;p&gt;If you want to run this tool globally you can place txtToMD.exe file to 'Path' in windows environment variables.&lt;br&gt;
that way you can use it from anywhere in your system&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;txtToMD {txt File Path} [options:light(default),dark,mid]&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;txtToMD D:\Desktop\test.txt dark
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Getting all currently available mappings:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;txtToMD -options
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5_N6vyBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/noukkyfp0nsq5axm710x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5_N6vyBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/noukkyfp0nsq5axm710x.png" alt="Available mappings" width="234" height="325"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Comments
&lt;/h3&gt;

&lt;p&gt;Feel free to leave comments if you see some critical antipattern or mistake.&lt;br&gt;
As I said this was my 1st GO project made for fun so it can probably be improved.&lt;/p&gt;

&lt;h3&gt;
  
  
  About me
&lt;/h3&gt;

&lt;p&gt;I currently work as backend/fullstack engineer mostly in C#,JS,TS.&lt;br&gt;
With focus on distributed systems area at the moment.&lt;/p&gt;

&lt;p&gt;In free time I have been exploring and experimenting with Go and Rust because they seemed fun and enjoyable to work with.&lt;/p&gt;

&lt;h4&gt;
  
  
  Contact
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://twitter.com/dompolzer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/dominik-polzer-hi-o/"&gt;Linkedin&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>markdown</category>
      <category>productivity</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
