Sign In

 

 

.NET Core ve Golang Birlikte Nasıl Durur?.NET Core ve Golang Birlikte Nasıl Durur?3652017-10-05T12:25:00Z 5.10.2017 12:25:00Gencebay Demir<div class="ExternalClassC2704CCC340449B8ABC83A88B638656F"><p>.NET Core; bana göre, bulut teknolojileri, modern web uygulamaları ve mobil servisler geliştirme rekabetine dahil olmuş, Ruby, Python, NodeJs vb. dil ve platformlara ilgi-ihtiyaç duyan .NET geliştiriciler için açık kaynak kodlu ve cross-platform destekli bir ortama olan gereksinimi karşılar.</p><p>Böylelikle; C# veya F# programlama dilleri ile Linux, MAC OSx ve Windows işletim sistemlerinde çalışabilen, performanslı, derlenip dağıtılabilir programlar geliştirebilir.</p><p>Bir diğer tarafta Google tarafından 2007 yılında duyurulan Golang programlama dili; hızla gelişen, her geçen gün artan popülerliği ve etkin bir topluluk dayanışması ile performanslı sistemler, web uygulamaları, dağıtık servisler vb. geliştirebilmek için birçok alanda tercih edilmektedir. Docker, moby, kubernetes vb. açık kaynak kodlu projeler ve bulut altyapısı sağlayacıları (DigitalOcean, Azure, Amazon) Golang'ın gelişmesine katkı sağlamaya devem ediyor.</p><p>Burada ön plana çıkan bir farktan bahsetmek isterim. .NET Core çalışma zamanında CoreCLR olarak adlandırılan bir çalıştırıcıya ihtiyaç duyup C# veya F# ile yazılan programların derlenip bu host ile çalıştırılmasını ister. Golang ise bağımsız olarak derleme sonucu oluşturulan çalıştırılabilir bir program üretir. Golang ile yazılan programın; işletim sistemi tarafından sunulan SDK, API, alt seviye işlemleri ve uyumluluk durumlarını kendiniz işler ve idare edersiniz. (Makefile, cross compiling)</p><p>Her iki ortamın Github hesapları (<a href="https://github.com/dotnet/core" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">.NET Core</span></a>, <a href="https://github.com/golang/go" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">Golang</span></a>), açık kaynak kodlu projeleri, detaylı dokümantasyon içerikleri (<a href="https://docs.microsoft.com/en-us/dotnet/core/" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">Docs Microsoft</span></a>, <a href="https://golang.org/doc/" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">Golang Docs</span></a>) çok zengin ve kesinlikle incelemeye değer.</p><p>Basit bir giriş yapmak istemiştim ama sanırım bu iki platformun özelliklerini anlatmak için ayrı bir yazı dizisi gerektiği aşikar.</p><p>Teknolojileri lehimize kullanarak; ihtiyaç duyduğumuz çözümleri üretmek, fanatik yaklaşımlardan uzaklaşarak şartları lehimize çevirebilecek basit ve kestirme yolları tercih etmenin, işlerimizi kolaylaştırdığını çoğu zaman tecrübe ettiğimi söyleyebilirim. Buradan yola çıkarak bu iki ortamın farklı özelliklerinden nasıl yararlandığımıza, .NET Core ve Golang'in birlikte gerçek bir senaryo üzerinden nasıl işlediğine bakalım.</p><p> <strong>Deneyim:</strong> İç ağımızda hizmet veren bir dosya sunucusunun belirli dizin ve içeriklerinin servisler, web uygulamaları (ASP.NET Core MVC Web Application) vb. hizmetler tarafından kullanılması isteniyor. Bu sunucuda .NET veya .NET Core framework kurulumu veya yükseltmesi yapamadığımızı varsayıyorum. Burada görevi Golang devralıyor.</p><p>​<br></p><p> <strong>​Bölüm Golang​</strong></p></div><div class="ExternalClassC2704CCC340449B8ABC83A88B638656F"><pre class=" language-go" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-go" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;"><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">package</span> main <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">import</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"log"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"net/http"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"os"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"path"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"path/filepath"</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">func</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">main</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> dir<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> filepath<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Abs</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>filepath<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Dir</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>os<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Args<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">[</span><span class="token number" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">0</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">]</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> log<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Fatal</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>err<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> combined <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> path<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Join</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>dir<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"wwwroot"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">panic</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ListenAndServe</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">":1444"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">FileServer</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Dir</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>combined<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span></code></pre><br> <div><p>​​Yukarıda​ki Golang programı, çalıştırıldığı dizinde belirtilen klasörü (wwwroot) ve tüm dizin içeriklerini http://*:1444 HTTP protokolü üzerinden erişilebilir yapar. Bir kaç satır kod ile statik içerik host edebilen, cross-platform bir sunucu programı yazmış olduk.</p><p>Bu programı sunucuya atıp terminalden - konsoldan çalıştırabilir ve hizmeti başlatabiliriz. Ben bu programı Windows Server 2012 R2 üzerinde bir windows servis olarak çalıştırıp, işletim sistemi tarafından gerekli durumlarda yeniden başlatılmasını sağlamak istiyorum (restart, exception, power vb.). Bunun için Github' dan <a href="https://github.com/kardianos/service" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">Service</span></a> isimli açık kaynak kodlu bir kütüphaneden yararlanıyorum. Varsayılan olarak derlenen Golang programını bu haliyle windows servis olarak <em>Service Control Manager</em> aracılığıyla kayıt - register edemiyoruz. Bu sebeple programı şu şekilde güncelliyorum:​</p><pre class=" language-go" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-go" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;"><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">package</span> main <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">import</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"log"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"net/http"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"os"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"path"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"path/filepath"</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"github.com/kardianos/service"</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> logger service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Logger <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">type</span> program <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">struct</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">func</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>p <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">*</span>program<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Start</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>s service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token builtin" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">error</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token comment" spellcheck="true" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#708090;">// Start should not block. Do the actual work async.</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">go</span> p<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">run</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">return</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">func</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>p <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">*</span>program<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">run</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> dir<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> filepath<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Abs</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>filepath<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Dir</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>os<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Args<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">[</span><span class="token number" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">0</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">]</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> log<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Fatal</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>err<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> combined <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> path<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Join</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>path<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Dir</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>dir<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"../wwwroot"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">panic</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ListenAndServe</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">":1444"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">FileServer</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>http<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Dir</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>combined<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">func</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>p <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">*</span>program<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Stop</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>s service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token builtin" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">error</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token comment" spellcheck="true" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#708090;">// Stop should not block. Return with a few seconds.</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">return</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">func</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">main</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> svcConfig <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">&</span>service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Config<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> Name<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">:</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"GoStaticServer"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> DisplayName<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">:</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"Go Static File Server"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> Description<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">:</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"Static file server"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> prg <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">&</span>program<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> s<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">:=</span> service<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">New</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>prg<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> svcConfig<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> log<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Fatal</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>err<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> logger<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> s<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Logger</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> log<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Fatal</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>err<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> s<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Run</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> err <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token boolean" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;">nil</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> logger<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Error</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>err<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span></code></pre>​​Böylelikle powershell veya sc.exe ile *.exe yi arka planda çalışan bir servis olarak kaydediyor ve hizmeti başlatıyorum.​​</div><div><pre class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;">powershell New-Service -Name "GoStaticServer" -BinaryPathName "<span class="token tag" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#990055;"><span class="token tag" style="text-rendering:optimizelegibility;box-sizing:border-box;"><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;"><</span>path-of-goserver.exe</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">></span></span>" -StartupType Automatic Start-Service -Name "GoStaticServer"</code>​</pre><br></div><div><p> <strong>​Bölüm .NET Core</strong></p><p>ASP.NET Core Mvc web uygulamamız içerisinden belli tip isteklerin dosya (image, pdf vb.) talebi olduğunu çözmek için; uygulamamızda dosya adreslerinin (URL)​</p><pre class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;">/public-fs</code></pre><p>segmenti ile başladığını varsayıyorum. Örneğin:</p><pre class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-markup" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;">http(s)://hostname/public-fs/sample.pdf http(s)://hostname/public-fs/sub/tree/webworld.png</code></pre><p>​​Bu adımdan sonra ASP.NET Core <strong>Middleware</strong> özelliğini kullanarak HTTP GET isteklerini Golang ile yazdığımız dosya sunucusuna şu şekilde aktarabiliriz:</p> ​​​<pre class=" language-csharp" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:16px;margin-top:0.5em;margin-bottom:0.5em;color:#000000;background:#f5f2f0;text-shadow:#ffffff 0px 1px;word-break:normal;word-wrap:normal;line-height:1.5;padding:1em;overflow:auto;max-width:100%;"><code class=" language-csharp" style="text-rendering:optimizelegibility;box-sizing:border-box;font-family:consolas, monaco, "andale mono", "ubuntu mono", monospace;font-size:inherit;background:0px 0px;text-shadow:#ffffff 0px 1px;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;max-width:100%;"><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">public</span> Task <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Invoke</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>HttpContext context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> method <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Request<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Method<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>method <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">==</span> <span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"GET"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> path <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Request<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Path<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>path<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">StartsWithSegments</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"/public-fs"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">return</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">HandleFileServerHttpRequest</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">return</span> <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">_next</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">private</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">async</span> Task <span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">HandleFileServerHttpRequest</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>HttpContext context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> requestMessage <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">new</span> <span class="token class-name" style="text-rendering:optimizelegibility;box-sizing:border-box;">HttpRequestMessage</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> requestMethod <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Request<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Method<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token comment" spellcheck="true" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#708090;">// Copy the request headers</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">foreach</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> header <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">in</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Request<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">if</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!</span>requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">TryAddWithoutValidation</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Key<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Value<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ToArray</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">&&</span> requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Content <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">!=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">null</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Content<span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">?</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">TryAddWithoutValidation</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Key<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Value<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ToArray</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> proxyServer <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">new</span> <span class="token class-name" style="text-rendering:optimizelegibility;box-sizing:border-box;">Uri</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>_settings<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>FileServerProxyAddress<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>RequestUri <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">new</span> <span class="token class-name" style="text-rendering:optimizelegibility;box-sizing:border-box;">Uri</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>proxyServer<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> $<span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"{context.Request.Path}{context.Request.QueryString}"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Method <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">new</span> <span class="token class-name" style="text-rendering:optimizelegibility;box-sizing:border-box;">HttpMethod</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>requestMethod<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> responseMessage <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">await</span> HttpFactory<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Client<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">SendAsync</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>requestMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> HttpCompletionOption<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>ResponseHeadersRead<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">,</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>RequestAborted<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> statusCode <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">int</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span>responseMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>StatusCode<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Response<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>StatusCode <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> statusCode<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">foreach</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> header <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">in</span> responseMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Response<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">[</span>header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Key<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">]</span> <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Value<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ToArray</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">foreach</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">var</span> header <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">in</span> responseMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Content<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">{</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Response<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">[</span>header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Key<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">]</span> <span class="token operator" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#a67f59;">=</span> header<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Value<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">ToArray</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span> <span class="token comment" spellcheck="true" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#708090;">// SendAsync removes chunking from the response. This removes the header so it doesn't expect a chunked response.</span> context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Response<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Headers<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">Remove</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span><span class="token string" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#669900;">"transfer-encoding"</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token keyword" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#0077aa;">await</span> responseMessage<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Content<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span><span class="token function" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#dd4a68;">CopyToAsync</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">(</span>context<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Response<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">.</span>Body<span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">)</span><span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">;</span> <span class="token punctuation" style="text-rendering:optimizelegibility;box-sizing:border-box;color:#999999;">}</span></code>​</pre> <p>​Burada <strong>"public-fs"</strong> segmenti ile HTTP isteğinin dosya sunucusu tarafından karşılanacağı kararını verip işlemi <strong>Middleware</strong> de yakalayıp sonlandırıyoruz.</p><p>Proxy gibi çalışan bu istek yönlendirme yöntemi ile istemci tarafına gelmiş olan HTTP isteğine ait tüm bilgileri (Headers, QueryString, Path vb.) Golang sunucusuna aktarıp bu sunucudan dönen cevabı gerçek isteğin cevabı olarak atıyoruz.</p><p>Böylelikle HTTP 200 (Ok), HTTP 304 (Not Modified) veya HTTP 404 (Not Found) vb. istek ve cevapları doğru kanal ve yöntemlerle dosya sunucumuz ve istemci uygulaması arasında paylaşmış oluruz.</p><p>Bana keyif veren bu çözümü geliştirdiğim ortamdan da biraz bahsetmek isterim. Visual Studio Code; Microsoft tarafından geliştirilen açık kaynak kodlu çok yönlü ve kaliteli bir editör. Küçük ve orta ölçekli .NET Core ve Golang projelerini ana bir dizin altında rahatlıkla yönetip, derleyip debug modda çalıştırdım ve tüm süreçleri kolaylıkla test edebildim.</p><p>​<a href="/blog/PublishingImages/Lists/Posts/AllPosts/GolangVsCodeDebug.png" target="_blank"><img src="/blog/PublishingImages/Lists/Posts/AllPosts/GolangVsCodeDebug.png" alt="GolangVsCodeDebug.png" style="margin:5px;width:575px;height:470px;" /></a><br></p><p>Bu yazıda derinlemesine değinilmesi gereken bir çok konu var. Özellikle çözüm ararken alternatif yollarında göz önünde bulundurulması, tek bir ortamda takılı kalmamak, daha çok şey öğrenmek, keyifle çalışmak... Bunun içinde diğer programlama dillerinin, kütüphanelerin ve hatta mobil geliştirme ortamlarının yüzeyselde olsa farkında olmak, yöntemlerini incelemek sizi bambaşka çözümlere götürüp daha üretken olmanızı sağlayabilir.</p><p>Uygulamaların kaynak kodlarına <a href="https://github.com/gencebay/NetCoreAndGolang" target="_blank" style="text-decoration:underline;"><span class="ms-rteForeColor-8">buradan (Github)</span></a> erişebilirsiniz.​</p></div></div>net-core-ve-golang-birlikte-nasil-dururhttp://www.bilgeadam.com/blog/Lists/Photos/PostImages/net-core-and-golang.png.NET Core ve Golang'in farklı özelliklerinden nasıl yararlandığımıza, bu iki ortamın birlikte gerçek bir senaryo üzerinden nasıl işlediğine bakalım.bluePROYazılım

Yazara Soru Sor




SORU GÖNDER