<?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 K Srijeyanthan on Medium]]></title>
        <description><![CDATA[Stories by K Srijeyanthan on Medium]]></description>
        <link>https://medium.com/@srijeyanthan?source=rss-7965a096ce48------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*FF33ilGyaosp4aO3FYMfEQ.jpeg</url>
            <title>Stories by K Srijeyanthan on Medium</title>
            <link>https://medium.com/@srijeyanthan?source=rss-7965a096ce48------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:17:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srijeyanthan/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[Implement abstract raw query execution in Golang]]></title>
            <link>https://srijeyanthan.medium.com/implement-abstract-raw-query-execution-in-golang-e0732687da27?source=rss-7965a096ce48------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0732687da27</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[orm]]></category>
            <category><![CDATA[interfaces]]></category>
            <dc:creator><![CDATA[K Srijeyanthan]]></dc:creator>
            <pubDate>Fri, 28 May 2021 18:02:32 GMT</pubDate>
            <atom:updated>2021-05-28T18:02:32.689Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang is becoming a de facto standard to implement more advanced application backends like distributed microservices, streaming services, etc. For instance, Docker is fully crafted in Go programming. This article is not mainly explaining the features of Go, rather it explains writing SQL abstraction layer to generalize raw query execution.</p><p>There are various libraries available to work with RDBMS systems in Go, like gorm(<a href="https://gorm.io/index.html">https://gorm.io/index.html</a>) or xorm (<a href="https://xorm.io/xorm/">https://xorm.io/xorm/</a>), these libraries are providing full-featured ORM to work with RDBMS systems, but there are few use cases where you need to have raw SQL access and process data.</p><p>For example, assume that we need to execute a random query that is constructed from user HTTP request dynamic parameters like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ksaKudekWDGQc2-w7YT2Lg.png" /><figcaption>The query was constructed based on a request</figcaption></figure><p>How can we use these ORM libraries to process queries like this, and execute them in a general way, here I am going to explain two different functions that provide convert row results in a Go map and an array of the map?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*czWyYl4kT0xhhU0QNb9Y2w.png" /><figcaption>Convert entire table result into an array of map</figcaption></figure><blockquote>Here is the idea is to execute the raw query and iterate the results to build the array of interface maps, then that can be marshaled using JSON encoder libraries to build JSON response.</blockquote><pre>type DbLayer struct{</pre><pre>         dbConnection          *gorm.DB</pre><pre>}</pre><pre>//  since we don&#39;t know the result structure, we will keep it in map of interface structure</pre><pre><strong>type </strong>Params <strong>map</strong>[string]<strong>interface</strong>{}</pre><pre><strong>func </strong>(dl *DbLayer) QueryRowsToListOfMap(rawQuery string) (int64, error, []Params) {<br><br>   <strong>var </strong>arrayOfMaps []util.Params<br>//execute the raw query<br>   rs, err := dl.dbConnection.Raw(rawQuery).Rows()<br><br>   <strong>if </strong>err != nil {<br>      <strong>return </strong>0, err, arrayOfMaps<br>   }<br><br>   <strong>var </strong>(<br>      refs []<strong>interface</strong>{}<br>      cnt  int64<br>   )<br><br>   <strong>for </strong>rs.Next() {<br>      <strong>var </strong>rowMapData util.Params<br>      rowMapData = make(util.Params)<br>      columns, _ := rs.Columns()<br>      refs = make([]<strong>interface</strong>{}, len(columns))<br>      <strong>for </strong>i := <strong>range </strong>refs {<br>         <strong>var </strong>ref sql.NullString<br>         refs[i] = &amp;ref<br>      }<br><br>      <strong>if </strong>err := rs.Scan(refs...); err != nil {<br>         <strong>return </strong>0, err, arrayOfMaps<br>      }<br>      <strong>for </strong>i := <strong>range </strong>refs {<br>         value := reflect.Indirect(reflect.ValueOf(refs[i])).Interface().(sql.NullString)</pre><pre>        // keep the results in key value  <br>        rowMapData[columns[i]] = value.String<br><br>      }<br>// constructing the array of map<br>      arrayOfMaps = append(arrayOfMaps, rowMapData)<br>      cnt++<br>   }<br><br>   <strong>return </strong>cnt, nil, arrayOfMaps<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qksXBjsW_XUfOn3DQMhUtg.png" /><figcaption>Assume that we need to get the only col and value only</figcaption></figure><blockquote>In this function, the user can define the interesting column they need, then it will be queried using a raw query to build the response map.</blockquote><pre><strong>func </strong>(dl *DbLayer) queryRowsToMap(rawQuery string, container <strong>interface</strong>{}, keyCol, valueCol string) (int64, error) {<br>   <strong>var </strong>(<br>      maps util.Params<br>      ind  *reflect.Value<br>   )<br><br>   <strong>var </strong>typ int<br>   <strong>switch </strong>container.(<strong>type</strong>) {<br>   <strong>case </strong>*util.Params:<br>      typ = 1<br>   <strong>default</strong>:<br>      typ = 2<br>      vl := reflect.ValueOf(container)<br>      id := reflect.Indirect(vl)<br>      <strong>if </strong>vl.Kind() != reflect.<strong><em>Ptr </em></strong>|| id.Kind() != reflect.<strong><em>Struct </em></strong>{<br>         panic(fmt.Errorf(<strong>&quot;&lt;RawSeter&gt; RowsTo unsupport type `%T` need ptr struct&quot;</strong>, container))<br>      }<br><br>      ind = &amp;id<br>   }<br><br>   rs, err := dl.dbConnectio.Raw(rawQuery).Rows()<br><br>   <strong>if </strong>err != nil {<br>      <strong>return </strong>0, err<br>   }<br><br>   <strong>var </strong>(<br>      refs []<strong>interface</strong>{}<br>      cnt  int64<br>      cols []string<br>   )<br><br>   <strong>var </strong>(<br>      keyIndex   = -1<br>      valueIndex = -1<br>   )<br><br>   <strong>for </strong>rs.Next() {<br>      <strong>if </strong>cnt == 0 {<br>         columns, err := rs.Columns()<br>         <strong>if </strong>err != nil {<br>            <strong>return </strong>0, err<br>         }<br>         cols = columns<br>         refs = make([]<strong>interface</strong>{}, len(cols))<br>         <strong>for </strong>i := <strong>range </strong>refs {<br>            fmt.Println(<strong>&quot;cols :&quot;</strong>, cols[i])<br>            <strong>if </strong>keyCol == cols[i] {<br>               keyIndex = i<br>            }<br>            <strong>if </strong>typ == 1 || keyIndex == i {<br>               <strong>var </strong>ref sql.NullString<br>               refs[i] = &amp;ref<br>            } <strong>else </strong>{<br>               <strong>var </strong>ref <strong>interface</strong>{}<br>               refs[i] = &amp;ref<br>            }<br>            <strong>if </strong>valueCol == cols[i] {<br>               valueIndex = i<br>            }<br>         }<br>         <strong>if </strong>keyIndex == -1 || valueIndex == -1 {<br>            panic(fmt.Errorf(<strong>&quot;&lt;RawSeter&gt; RowsTo unknown key, value column name `%s: %s`&quot;</strong>, keyCol, valueCol))<br>         }<br>      }<br><br>      <strong>if </strong>err := rs.Scan(refs...); err != nil {<br>         <strong>return </strong>0, err<br>      }<br><br>      <strong>if </strong>cnt == 0 {<br>         <strong>switch </strong>typ {<br>         <strong>case </strong>1:<br>            maps = make(util.Params)<br>         }<br>      }<br>      key := reflect.Indirect(reflect.ValueOf(refs[keyIndex])).Interface().(sql.NullString).String<br>      <strong>switch </strong>typ {<br>      <strong>case </strong>1:<br>         value := reflect.Indirect(reflect.ValueOf(refs[valueIndex])).Interface().(sql.NullString)<br>         <strong>if </strong>value.Valid {<br>            maps[key] = value.String<br>         } <strong>else </strong>{<br>            maps[key] = nil<br>         }<br><br>      <strong>default</strong>:<br>         <strong>if </strong>id := ind.FieldByName(util.CamelString(key)); id.IsValid() {<br>            ab.setFieldValue(id, reflect.ValueOf(refs[valueIndex]).Elem().Interface())<br>         }<br>      }<br><br>      cnt++<br>   }<br><br>   <strong>if </strong>typ == 1 {<br>      v, _ := container.(*util.Params)<br>      *v = maps<br>   }<br><br>   <strong>return </strong>cnt, nil<br>}</pre><pre>// helper function to set the field value based on data type</pre><pre><strong>func </strong>(ab *AssetBase) setFieldValue(ind reflect.Value, value <strong>interface</strong>{}) {<br>   <strong>switch </strong>ind.Kind() {<br>   <strong>case </strong>reflect.<strong><em>Bool</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.SetBool(<strong><em>false</em></strong>)<br>      } <strong>else if </strong>v, ok := value.(bool); ok {<br>         ind.SetBool(v)<br>      } <strong>else </strong>{<br>         v, _ := util.StrTo(ToStr(value)).Bool()<br>         ind.SetBool(v)<br>      }<br><br>   <strong>case </strong>reflect.<strong><em>String</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.SetString(<strong>&quot;&quot;</strong>)<br>      } <strong>else </strong>{<br>         ind.SetString(ToStr(value))<br>      }<br><br>   <strong>case </strong>reflect.<strong><em>Int</em></strong>, reflect.<strong><em>Int8</em></strong>, reflect.<strong><em>Int16</em></strong>, reflect.<strong><em>Int32</em></strong>, reflect.<strong><em>Int64</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.SetInt(0)<br>      } <strong>else </strong>{<br>         val := reflect.ValueOf(value)<br>         <strong>switch </strong>val.Kind() {<br>         <strong>case </strong>reflect.<strong><em>Int</em></strong>, reflect.<strong><em>Int8</em></strong>, reflect.<strong><em>Int16</em></strong>, reflect.<strong><em>Int32</em></strong>, reflect.<strong><em>Int64</em></strong>:<br>            ind.SetInt(val.Int())<br>         <strong>case </strong>reflect.<strong><em>Uint</em></strong>, reflect.<strong><em>Uint8</em></strong>, reflect.<strong><em>Uint16</em></strong>, reflect.<strong><em>Uint32</em></strong>, reflect.<strong><em>Uint64</em></strong>:<br>            ind.SetInt(int64(val.Uint()))<br>         <strong>default</strong>:<br>            v, _ := util.StrTo(ToStr(value)).Int64()<br>            ind.SetInt(v)<br>         }<br>      }<br>   <strong>case </strong>reflect.<strong><em>Uint</em></strong>, reflect.<strong><em>Uint8</em></strong>, reflect.<strong><em>Uint16</em></strong>, reflect.<strong><em>Uint32</em></strong>, reflect.<strong><em>Uint64</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.SetUint(0)<br>      } <strong>else </strong>{<br>         val := reflect.ValueOf(value)<br>         <strong>switch </strong>val.Kind() {<br>         <strong>case </strong>reflect.<strong><em>Int</em></strong>, reflect.<strong><em>Int8</em></strong>, reflect.<strong><em>Int16</em></strong>, reflect.<strong><em>Int32</em></strong>, reflect.<strong><em>Int64</em></strong>:<br>            ind.SetUint(uint64(val.Int()))<br>         <strong>case </strong>reflect.<strong><em>Uint</em></strong>, reflect.<strong><em>Uint8</em></strong>, reflect.<strong><em>Uint16</em></strong>, reflect.<strong><em>Uint32</em></strong>, reflect.<strong><em>Uint64</em></strong>:<br>            ind.SetUint(val.Uint())<br>         <strong>default</strong>:<br>            v, _ := util.StrTo(ToStr(value)).Uint64()<br>            ind.SetUint(v)<br>         }<br>      }<br>   <strong>case </strong>reflect.<strong><em>Float64</em></strong>, reflect.<strong><em>Float32</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.SetFloat(0)<br>      } <strong>else </strong>{<br>         val := reflect.ValueOf(value)<br>         <strong>switch </strong>val.Kind() {<br>         <strong>case </strong>reflect.<strong><em>Float64</em></strong>:<br>            ind.SetFloat(val.Float())<br>         <strong>default</strong>:<br>            v, _ := util.StrTo(ToStr(value)).Float64()<br>            ind.SetFloat(v)<br>         }<br>      }<br><br>      <strong>case </strong>sql.NullString, sql.NullInt64, sql.NullFloat64, sql.NullBool:<br>         indi := reflect.New(ind.Type()).Interface()<br>         sc, ok := indi.(sql.Scanner)<br>         <strong>if </strong>!ok {<br>            <strong>return<br>         </strong>}<br>         err := sc.Scan(value)<br>         <strong>if </strong>err == nil {<br>            ind.Set(reflect.Indirect(reflect.ValueOf(sc)))<br>         }<br>      }<br><br>   <strong>case </strong>reflect.<strong><em>Ptr</em></strong>:<br>      <strong>if </strong>value == nil {<br>         ind.Set(reflect.Zero(ind.Type()))<br>         <strong>break<br>      </strong>}<br>      ind.Set(reflect.New(ind.Type().Elem()))<br>      ab.setFieldValue(reflect.Indirect(ind), value)<br>   }<br>}</pre><p>We can execute the above function like below</p><pre>responseMap Params<br>numberOfRecords, err := dbLayer.queryRowsToMap(&quot;select A,B from Table 1&quot;,responseMap, &quot;A&quot;,&quot;B&quot;)<br>fmt.Println(responseMap)</pre><p>as shown in this example, we can use these functions to generalize our raw queries apart from ORM functionalities.</p><p>References : <a href="https://gorm.io/docs/sql_builder.html">https://gorm.io/docs/sql_builder.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0732687da27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[CORS handling using a reverse proxy]]></title>
            <link>https://srijeyanthan.medium.com/cors-handling-using-a-reverse-proxy-873e56a03941?source=rss-7965a096ce48------2</link>
            <guid isPermaLink="false">https://medium.com/p/873e56a03941</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[cors]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[K Srijeyanthan]]></dc:creator>
            <pubDate>Wed, 12 May 2021 07:53:14 GMT</pubDate>
            <atom:updated>2021-05-12T07:53:14.102Z</atom:updated>
            <content:encoded><![CDATA[<p>When we are developing a client application that connects to the backend using APIs always experiencing CORS problem in various stages such as development, testing, production.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xHEVD4rD4CAtmXFc8pIhig.png" /><figcaption>typical CORS policy issue</figcaption></figure><p>Not all the backend libraries are providing the CORS handling mechanism to solve this, but understanding the fundamental cause of this error can give us a hint to solve. For example, in the above case, simply says that <a href="http://my-site.local:8088/">http://my-site.local:8088</a> access has been blocked, the browser was expecting the <strong><em>Access-Control-Allow-Origin </em></strong>header approved for this domain, but it was not present.</p><p>Normally if we are developing API services we should be knowing these facts on how to secure our service using CORS though it is annoying during the development stage, this article helps you to create a CORS handling layer at a proxy level rather than programming. Once the API service is ready, you will be probably ending up deploying behind any proxy and expecting some heavy lifting on service management by the proxy server</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/863/1*rEkrjTUf-tYUwnqCWwp69w.png" /><figcaption>typical API deployment</figcaption></figure><p>Nginx is a preferred choice when it comes to reverse proxy enabling increase security, performance, and reliability to our the service we are implementing. Now assume that our API service doesn’t have any CORS handling mechanisms, so</p><blockquote>How do we make it work and handle all the cases?</blockquote><p>a. First we need to make sure our reverse proxy is handling HTTP OPTIONS requests, normally it is called a pre-flight request from a client application (Angular or Vue or React) to ask the server before initializing actual API service call. More detailed example of pre-flight request can be found here (<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS">https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS</a>)</p><pre>if ($request_method = OPTIONS ) {<br> add_header ‘Content-Type’;<br> <strong>add_header ‘Access-Control-Allow-Credentials’ ‘true’ always;</strong><br><strong> add_header Access-Control-Allow-Origin </strong><a href="http://localhost:4200/"><strong>http://localhost:4200</strong></a><strong> always;</strong><br> add_header Access-Control-Allow-Headers ‘DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range&#39;;<br> <strong>add_header Access-Control-Allow-Methods ‘GET, POST,PUT,PATCH’;</strong><br> <strong>return 200;</strong><br> }</pre><p>You can add the above block to handle the HTTP OPTIONS header whenever it requested from your client applications.</p><pre><strong>add_header ‘Access-Control-Allow-Credentials’ ‘true’ always;</strong></pre><p>This is necessary when your application is talking to your backend using <strong>cookies</strong>, so we should tell back to the client application saying the server is allowing credentials, and it should be always sent to the server. Normally if you are developing an application that keeps talking to the backend using APIs using cookies, then we should enable this flag, otherwise, the browser won’t send a cookie header like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*O7mItLgeHC0aTJ1E2X5dZg.png" /></figure><pre><strong>add_header Access-Control-Allow-Origin </strong><a href="http://localhost:4200/"><strong>http://localhost:4200</strong></a><strong> always;</strong></pre><p>This header is needed when your backend service is deployed on a different domain or a different cloud or outside of your local network. If you don’t mention this header, then local development API execution will be blocked by the browser saying cross-origin as it is not matched with <strong>Host and Domain header</strong> like following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*J8mVRXYR9NMrnwRnbOsyRg.png" /></figure><p>If you are not using cookies for your API authorization, then we can specify <strong>Access-Control-Allow-Origin * , </strong>instead of specifying some particular domain. Because, <strong>Access-Control-Allow-Origin * </strong>is not allowed when cookies are used.</p><pre><strong>add_header Access-Control-Allow-Methods ‘GET, POST,PUT,PATCH’;</strong></pre><p>Here we are saying what are methods are allowed to perform API execution, If only GET is allowed, then we need to specify GET only, so other requests are blocked from the browser.</p><p>For example, the following is the Nginx site configuration for <strong>dev.example.com</strong> API service</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m0zLuPTNTr-12QV9kIUS5w.png" /><figcaption>API service from dev.example.com/api/v2/login</figcaption></figure><pre>server{<br>    server_name dev.example.com;</pre><pre><strong>location /api/v2/</strong> {<br>                if ($request_method = OPTIONS ) {<br>                       <br>                        add_header    &#39;Content-Type&#39;;<br>               add_header &#39;Access-Control-Allow-Credentials&#39; &#39;true&#39; always;<br>                        add_header Access-Control-Allow-Origin <a href="http://localhost:4200/">http://localhost:4200</a> always;<br>                        add_header Access-Control-Allow-Headers &#39;DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range&#39;;<br>                        add_header Access-Control-Allow-Methods &#39;GET, POST,PUT,PATCH&#39;;<br>                        return 200;<br>                }<br>  <br>          add_header Access-Control-Allow-Headers &#39;DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range&#39;;<br>                add_header Access-Control-Allow-Origin <a href="http://localhost:4200/">http://localhost:4200</a> always;<br>                add_header &#39;Access-Control-Allow-Credentials&#39; &#39;true&#39; always;<br>                add_header &#39;Access-Control-Allow-Methods&#39; &#39;GET, POST, PUT, DELETE, OPTIONS&#39; always;<br>                client_max_body_size 200M;<br>                proxy_pass  <a href="http://127.0.0.1:5623/">http://127.0.0.1:5623/</a>;</pre><pre>}</pre><pre>listen 443 ssl; <br>    ssl_certificate /etc/letsencrypt/live/dev.example.com/fullchain.pem; <br>    ssl_certificate_key /etc/letsencrypt/live/dev.example.com/privkey.pem; <br>    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot<br>    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot</pre><pre>}</pre><pre>server{<br>    if ($host = dev.example.com) {<br>       return 301 <a href="https://$host$request_uri/">https://$host$request_uri</a>;<br>    }</pre><pre>server_name dev.example.com;<br>    listen 80;</pre><pre>}</pre><h3>Conclusion</h3><p>Though we can implement a CORS handling from the programming level, this approach is not well-advised as we need to focus on all the CORS specifications and corner cases. We should handle this by using reverse proxies to safeguard your application API backend services.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=873e56a03941" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>