<?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 Chung Khanh Duy on Medium]]></title>
        <description><![CDATA[Stories by Chung Khanh Duy on Medium]]></description>
        <link>https://medium.com/@chungkhanhduy?source=rss-bc4bc3daf71e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*u4BKCxKkcOSsdTxw.</url>
            <title>Stories by Chung Khanh Duy on Medium</title>
            <link>https://medium.com/@chungkhanhduy?source=rss-bc4bc3daf71e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:42:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chungkhanhduy/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[Deploy your application with k8s, ingress and certificate]]></title>
            <link>https://medium.com/@chungkhanhduy/deploy-your-application-with-k8s-ingress-and-certificate-3bd2929c9f11?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3bd2929c9f11</guid>
            <category><![CDATA[ingress]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[cert-manager]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Thu, 02 Jan 2025 07:30:30 GMT</pubDate>
            <atom:updated>2025-01-02T07:44:22.624Z</atom:updated>
            <content:encoded><![CDATA[<h3>I will use AKS (Azure Kubernetes Service) to demonstrate how. If you’re using Kubernetes in AWS or GCP that would be the same</h3><p>This is reference from <a href="https://viblo.asia/p/bao-mat-nginx-ingress-voi-cert-manager-tren-kubernetes-MkNLrbYwLgA">https://viblo.asia/p/bao-mat-nginx-ingress-voi-cert-manager-tren-kubernetes-MkNLrbYwLgA</a> but modify a bit to use with AKS.</p><p>All k8s manifest files mentioned in this story was published here: <a href="https://github.com/duymap/devops/tree/main/k8s-ingress-cert">https://github.com/duymap/devops/tree/main/k8s-ingress-cert</a></p><ol><li><strong>Login to azure</strong></li></ol><pre>az login</pre><p><strong>2. Login to k8s cluster</strong></p><pre>az aks get-credentials - resource-group &lt;your-resource-group&gt; - name &lt;your-k8s-cluster&gt;</pre><p><strong>3. Create a secret (for the environment of docker to deploy in k8s)</strong></p><pre>kubectl apply -f ./secret.yml</pre><p><strong>4. Deploy apps</strong></p><pre>kubectl apply -f ./deployment.yml</pre><p>- Then verify pods: <em>kubectl get pods</em>. The result will looks like:</p><pre>NAME READY STATUS RESTARTS AGE<br>backend-6997b6d44-lbqmn 1/1 Running 1 (76m ago) 77m<br>db-6d99dfccdc-rzgsv 1/1 Running 0 77m<br>frontend-68f56f8cf-ggwcn 1/1 Running 0 77m</pre><ul><li>Then veriry service: <em>kubectl get svc</em>. The result will looks like:</li></ul><pre>NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE<br>backend ClusterIP 10.0.96.33 &lt;none&gt; 80/TCP 79m<br>db ClusterIP 10.0.15.148 &lt;none&gt; 80/TCP 79m<br>frontend LoadBalancer 10.0.211.214 20.28.47.135 80:30398/TCP 79m<br>kubernetes ClusterIP 10.0.0.1 &lt;none&gt; 443/TCP 106m</pre><ul><li>Then we can verify by accessing external IP 20.28.47.135:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZB4IiaHCrzIwV1DcutjCvQ.png" /><figcaption>access directly by IP</figcaption></figure><p><strong>5. Install ingress-nginx controller</strong></p><pre>kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.0-beta.0/deploy/static/provider/cloud/deploy.yaml</pre><ul><li>Then check ingress-nginx controller installed: <em>kubectl get svc -n ingress-nginx</em>. The result will looks like:</li></ul><pre>NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE<br>ingress-nginx-controller LoadBalancer 10.0.170.192 20.28.40.188 80:31685/TCP,443:32703/TCP 75m<br>ingress-nginx-controller-admission ClusterIP 10.0.127.65 &lt;none&gt; 443/TCP 75m</pre><ul><li>Then create the subdomain for IP “20.28.40.188”. You can access Azure portal, search for Public IP, select public IP “20.28.40.188”, find out “Configuration” section, then input the subdomain “todo.australiacentral.cloudapp.azure.com”. This step we can re-use thesubdomain created by azure. But you can create your own subdomain by creating A record in your DNS site management.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNq2SrO7CM2EVv3CS-D7Tw.png" /><figcaption>configure a subdomain using domain from azure</figcaption></figure><p><strong>6. Update ingress.yml by replace <em>&lt;your-domain&gt;</em> with “todo.australiacentral.cloudapp.azure.com”, save it</strong></p><p><strong>7. Apply ingress.yml:</strong></p><pre>kubectl apply -f ./ingress.yml</pre><p><strong>8. You need to install cert-manager (</strong><a href="https://cert-manager.io/"><strong>https://cert-manager.io/</strong></a><strong>) that is a plugin of k8s to issue cert and auto re-new it every 3 months. It uses letsencrypt:</strong></p><pre>kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.2/cert-manager.yaml</pre><ul><li>Then confirm if cert-manager installed successfully by: <em>kubectl get pods -n cert-manager</em>. The result will looks like:</li></ul><pre>NAME READY STATUS RESTARTS AGE<br>cert-manager-57d855897b-gcdpb 1/1 Running 0 74m<br>cert-manager-cainjector-5c7f79b84b-dsrbs 1/1 Running 0 74m<br>cert-manager-webhook-657b9f664c-qwzmd 1/1 Running 0 74m</pre><p><strong>9. Open issuer.yml, at `email` section, update `&lt;your-email&gt;` by your real email. Then apply it:</strong></p><pre>kubectl apply -f ./issuer.yml</pre><ul><li>Then verify if the issuer is ready: <em>kubectl get issuer</em>. The result will looks like:</li></ul><pre>NAME READY AGE<br>letsencrypt-prod True 58s</pre><p><strong>10. Update ingress.yml again, add/modify as below section:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*ydVpQCTLEK86b0j4TqsGsQ.png" /><figcaption>Update ingress with tls and cert</figcaption></figure><ul><li>Then apply it again:</li></ul><pre>kubectl apply -f ./ingress.yml</pre><ul><li>Then wait a bit and verify ingress by <em>kubectl get ing</em>, the result will looks like:</li></ul><pre>NAME CLASS HOSTS ADDRESS PORTS AGE<br>todo nginx todo.australiacentral.cloudapp.azure.com 20.28.40.188 80, 443 81m</pre><ul><li>Then you will see now ingress can be accessible via port 443 which means SSL</li></ul><p><strong>11. Run migration of backend. That is because of we’re using existing backend image. For your own backend, you can handle it by bootstrap application itself, so you might don’t need to run this step if your backend already cover it during bootstraping application:</strong></p><pre>kubectl exec -it &lt;pod-backend-name&gt; - sh<br>yarn prisma migrate deploy</pre><p>Whew, now you finally get the application running and access via https:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gyLRcTd3rrjGW5C3cR4VQg.png" /><figcaption>application can be accessible via https and subdomain configured</figcaption></figure><p>Don’t forget to clap it if you like this post :). Happy devops!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bd2929c9f11" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Export csv with python is never easy than ever]]></title>
            <link>https://medium.com/@chungkhanhduy/export-csv-with-python-is-never-easy-than-ever-78c14716e660?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/78c14716e660</guid>
            <category><![CDATA[csv]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Mon, 25 May 2020 01:47:52 GMT</pubDate>
            <atom:updated>2020-05-25T01:47:52.832Z</atom:updated>
            <content:encoded><![CDATA[<h3>Export csv using python with a few lines of code.</h3><p>Please take a look video to see how easy to export CSV with python and do not forget to subscribe channel if you like this video. I’m a little bit lazy to write so recording video would be easier to follow, I think.</p><p>The video is the demo with the database is MySQL, you can find out appropriate connector if using a different database.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FhMiJlr7wmbM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DhMiJlr7wmbM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FhMiJlr7wmbM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/05384bd51add7c827488e5fe25330c53/href">https://medium.com/media/05384bd51add7c827488e5fe25330c53/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=78c14716e660" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using pm2 instead of creating service for linux]]></title>
            <link>https://medium.com/@chungkhanhduy/using-pm2-instead-of-creating-service-for-linux-eb09d7c9eb8b?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb09d7c9eb8b</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[nuxtjs]]></category>
            <category><![CDATA[pm2]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Thu, 21 Nov 2019 15:30:35 GMT</pubDate>
            <atom:updated>2019-11-21T15:30:35.361Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I had a chance to work with <a href="https://nuxtjs.org/">Nuxt.js</a> which is great front-end framework written by VueJS. The interesting in Nuxt it offers a great SEO feature when allow application run as SSR but still wrote as Vue App which seems more convenient comparing with Angular Universal.</p><p>The problem I faced when the first time deploy nuxt application into production is I was not able to keep “nuxt start” as running as service. I can open terminal, run “npm start” , it will kick “nuxt start” running at port 3000, then everything looks great.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4szhEemRJh1LjNkezCT0EQ.png" /></figure><p>But as soon as I closed terminal, it shut downed server too :(</p><p>So I need to find the way to keep server still running even I close/exit terminal. The traditional way is we can create a service to let “npm start” as below:</p><pre>[Unit]<br>After=network.target</pre><pre>[Service]</pre><pre>ExecStart=/usr/local/bin/your-script.sh</pre><pre>[Install]</pre><pre>WantedBy=multi-user.target</pre><p>Then implement your .sh file, register your service to linux, enable it, restart, checking status.. Tooooo… many steps, I’m lazy :( . But..luckily <a href="https://pm2.keymetrics.io/">pm2</a> will help you to make it easy. <a href="https://pm2.keymetrics.io/">Pm2</a> is process manager which help to you to run application as service. And here how I used that to replace traditional service for my case at least :)</p><p>First you need to install pm2 via npm:</p><pre>npm install -g pm2</pre><p>Then init it:</p><pre>pm2 init</pre><p>pm2 will create ecosystem-conf.js as template, but you can edit as your goal, e.g: in this case I want to run “npm start” at specific folder, my folder will contains nuxt app:</p><pre>module.exports = {<br>  apps : [{<br>    name      : &#39;your-app&#39;, <br>    cwd       : &#39;/path-to-your-app&#39;, <br>    script    : &#39;npm&#39;<br>    args      : &#39;start&#39;<br>}]<br>};</pre><p>Then you just run:</p><pre>pm2 start</pre><p>And it will run “npm start” as service. You also reload service by using :</p><pre>pm2 reload all</pre><p>Have fun :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb09d7c9eb8b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to deal with multiple NodeJS version in your dev environment.]]></title>
            <link>https://medium.com/@chungkhanhduy/how-to-deal-with-multiple-nodejs-version-in-your-dev-environment-2befc2a646e?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/2befc2a646e</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[nvm]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Fri, 26 Apr 2019 16:07:06 GMT</pubDate>
            <atom:updated>2019-04-26T16:08:37.400Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to deal with multiple NodeJS versions in your dev environment.</h3><p>Nowadays, SPA seems become a trend for web development. A lot of front-end framework or UI framework were born to make life of developer is easier. A lot of popular names we can hear such as AngularJS, ReactJS, Ember and recently is VueJS. As a developer, you can not avoid to work in multiple projects and unfortunately they’re not always the same version of framework or same framework.</p><p>Any front-end/UI frameworks need dependencies and “npm” looks like necessary tool to pull out all those dependencies that you need. And…someday you have a chance to work with project using Angular 7, what ?? That’s really awesome, “I will work with most modern front-end framework backed by Google.”, something be proud in your head. And the first thing you will do is setting up your workspace with all necessary tools include NodeJS. But… Angular latest version just work with Node latest version. Okay! you go ahead to install NodeJS latest version in your machine. At the time I wrote this article, latest version of NodeJS is 10.x.</p><p>A next days after you’re enjoying to play with something call latest technologies :)</p><p>Again, someday you have been assigned to new project to support. The bad thing is new project you’re going to be working not using latest technologies, it used Angular 1.x ^_^. You’re also eager — aha, I have a chance to work with multiple technologies ;).</p><p>You start setting up workspace for new project, try to do some “npm install” to pull out dependencies but…. WTF ? Errors happened all the time :(. What you can think is to re-install nodeJS with lower version to adapt with new project, but how about if you switch back to previous project which being used Angular 7 ? Do I need to re-install nodeJS again ? That ‘s— really — annoying.</p><p>So no worries, nvm (Node version manager) can help you do that. It will allow you to install multiple nodeJS versions in your machine with very easy command line “nvm install &lt;version&gt;” to install and “nvm use &lt;version&gt;” to use version that you want.</p><p>Sounds good, right ?</p><p>I made 2 videos how to use it in Window (<a href="https://www.youtube.com/watch?v=aVTD6wnMqoY">https://www.youtube.com/watch?v=aVTD6wnMqoY</a>) and Mac (<a href="https://www.youtube.com/watch?v=BG33IjOtmp8">https://www.youtube.com/watch?v=BG33IjOtmp8</a> ) for you reference. Hope that can help.</p><p>Sometime the solution is quite easy, don’t think so hard. Happy coding :)</p><p>Clap if you like this post @_@</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2befc2a646e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug PHP in Mac Mojave]]></title>
            <link>https://medium.com/@chungkhanhduy/debug-php-in-mac-mojave-724b21b088b?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/724b21b088b</guid>
            <category><![CDATA[php-programming]]></category>
            <category><![CDATA[php-debug]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Sat, 13 Apr 2019 04:49:27 GMT</pubDate>
            <atom:updated>2020-07-06T03:30:58.831Z</atom:updated>
            <content:encoded><![CDATA[<p>Well, if you google “install php debugger” , there were a lot of results. However not all of them works for me at least because I’m using Mac Mojave. Some links mentioned how to install XDebug in Mac but that does not work at all. So I have to do some research and finally sort it out. So if you’re using Mac Mojave, want to debug with PHP and have problems with it, I think this is right article for you :-). However if you’re lazy person to read, here is full video for you to view: <a href="https://youtu.be/JDrK1SvVj_Y">https://youtu.be/JDrK1SvVj_Y</a></p><p>Steps:</p><ol><li>Install Visual Studio Code, I will use that for PHP development and debug. Download the link here: <a href="https://code.visualstudio.com/">https://code.visualstudio.com/</a>. It’s totally free</li><li>Install PHP Debug plugin for Visual Studio code. You can click on market place button and search “PHP Debug” and install it</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PuErUEjGwMkMboeCcY0XPw.png" /></figure><p>3. Install XDebug first ( bottle neck here )</p><ul><li>Download <a href="http://xdebug.org/files/xdebug-2.7.1.tgz">http://xdebug.org/files/xdebug-2.7.1.tgz</a> and extract in your machine. Then following commands below:</li></ul><pre>&gt; cd xdebug-2.7.1</pre><pre>&gt; phpize</pre><pre>&gt; ./configure</pre><pre>&gt; make</pre><pre>&gt; cp modules/xdebug.so /usr/lib/php/extensions/no-debug-non-zts-20160303</pre><ul><li>Then adding below lines into /etc/php.ini and save it:</li></ul><pre><em>zend_extension = /usr/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so</em></pre><pre><em>xdebug.remote_enable = 1</em></pre><pre><em>xdebug.remote_autostart = 1</em></pre><pre><em>xdebug.remote_port=9000</em></pre><ul><li>Probably you will have problem with “make” step with error message “php.h was not found” , if so please do the following steps and run “make” step again.</li></ul><pre>&gt; xcode-select --install<br>&gt; sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /</pre><p>4. Go back to Visual Studio Code and try to debug</p><ul><li>Write a simple php page such as phpinfo</li><li>Click on “Bug” button</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/86/1*vIl0-Md_dR9MmwnWIzCzCQ.png" /><figcaption>Bug button</figcaption></figure><ul><li>You will see dropdown “No configuration” , select “Add configuration”, select PHP, then it will apprear “Listen to XDebug”, click on “Play” button</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2WO6uOYZfWJUHlvMg3KD-Q.png" /></figure><ul><li>Ok, so now let’s try to debug, open your php page ( I assume this is index.php ), set breakpoint and hit your site. I tested with my index.php and here is result. Your breakpoint was highlighted proves that XDebug works. Now you can start debugging your PHP project</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fgXNWs_FGcA6awgcOEd9gw.png" /></figure><ul><li>You can download xdebug.so which I already built here: <a href="https://app.box.com/s/m9unmo1sipyn7jpq02v6a0s9gp29n4ux">https://app.box.com/s/m9unmo1sipyn7jpq02v6a0s9gp29n4ux</a></li></ul><p>Happy coding ^_^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=724b21b088b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How we overcome the challenges with PDF report]]></title>
            <link>https://medium.com/@chungkhanhduy/how-we-overcome-the-challenges-with-pdf-report-a90c5dc44b0a?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a90c5dc44b0a</guid>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[wkhtmltopdf]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[thymleaf]]></category>
            <category><![CDATA[pdf]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Thu, 08 Nov 2018 02:38:21 GMT</pubDate>
            <atom:updated>2018-11-08T02:38:21.533Z</atom:updated>
            <content:encoded><![CDATA[<p>Nowadays report seems become important part in any application. Almost system need reports and reports are available in many kind of format such as a pdf, a html page or a document (doc, xls)…. In the limitation of this post, I just would like to focus on common use case that is “export pdf report” which is usually available in almost enterprise application.</p><p>I was Java developer so the first thing in my mind looks like JasperReport which is very popular library to produce report in many kind of formats. We’re done some projects which using it but someday we ran into the problem:</p><blockquote>“We have web application written by Spring, we also have a very complex layout html page implemented already and <strong>now we need to generate pdf report exactly match with that page</strong>”.</blockquote><p>Time is going and we have another problem “we don’t have enough time to look at if feasible to make it works with JasperReport”. OMG !!!</p><p>It made me to think about a few another options how to transform whole html to pdf and luckily I found 2 promising candidates : flyingsourcer (<a href="https://github.com/flyingsaucerproject/flyingsaucer">https://github.com/flyingsaucerproject/flyingsaucer</a>_ and wkhtmltopdf (<a href="https://github.com/wkhtmltopdf/wkhtmltopdf">https://github.com/wkhtmltopdf/wkhtmltopdf</a>) which can help us to generate html into pdf and support css for styling template html. After considering a few factors with a lot of experiments, I decided to go with wkhtmltopdf finally because of 2 things 1) higher start in github 2) seems better community 3) it’s written by C so hopefully can process pixel better if comparing with flying source which implemented by pure java.</p><p>And here how we did…</p><p>Step 1: wkhtmltopdf tool needs a static html content (with css ) to generate to pdf. In the real case, you need to binding your data before producing html content, so we need to something to generate html content first before calling wkhtmltopdf. Spring offered the great template engine called ‘thymeleaf’ and we can move forward with that.</p><pre><a href="http://twitter.com/Configuration">@Configuration</a><br>public class TemplateConfigure {<br>  <a href="http://twitter.com/Bean">@Bean</a><br>  public ClassLoaderTemplateResolver templateResolver() {<br>    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();<br>    templateResolver.setPrefix(&quot;templates/&quot;);<br>    templateResolver.setSuffix(&quot;.html&quot;);<br>    templateResolver.setCharacterEncoding(CharEncoding.UTF_8);<br>    templateResolver.setOrder(1);<br>    return templateResolver;<br>  }<br>  <a href="http://twitter.com/Bean">@Bean</a><br>  public SpringTemplateEngine templateEngine() {<br>     SpringTemplateEngine engine = new SpringTemplateEngine();<br>     engine.setTemplateResolvers(Sets.newHashSet(templateResolver()));<br>     return engine;<br>  }<br>}</pre><p>Above code would tell Spring look for template with extension .html in template folder. “template folder” should be located under resources folder in Spring app. So let’s try to create a test.html in there.</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html xmlns:th=&quot;<a href="http://www.thymeleaf.org/">http://www.thymeleaf.org</a>&quot; xmlns:xsi=&quot;<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>&quot;<br>      xsi:schemaLocation=&quot;<a href="http://www.thymeleaf.org/">http://www.thymeleaf.org</a> &quot;&gt;<br>&lt;head&gt;<br>&lt;meta charset=&quot;UTF-8&quot;&gt;<br>&lt;title&gt;&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br> &lt;div&gt;This is test of pdf exporting&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*7OTKbV-floGJ7YpyFM3JVw.png" /><figcaption>location of test.html</figcaption></figure><p>Step 2: Writing sample code to try:</p><pre><a href="http://twitter.com/Autowired">@Autowired</a><br>private SpringTemplateEngine templateEngine;</pre><pre>public void createPdf() {<br>  <br>  try {<br>   String[] cmd = { &quot;&lt;your-path&gt;/wkhtmltopdf&quot;, &quot;--image-quality&quot;, &quot;100&quot;, &quot;--page-size&quot;, &quot;A4&quot; };<br>   <br>   ProcessBuilder pBuilder = new ProcessBuilder(cmd);  <br>   pBuilder.redirectErrorStream(false);</pre><pre>   // write stream<br>   Process process = pBuilder.start();<br>   String templateFile = &quot;test&quot;;<br>   try (BufferedOutputStream in = new  BufferedOutputStream(process.getOutputStream())) {;<br>   Context context = new Context(Locale.ENGLISH);<br>   String content = templateEngine.process(templateFile, context);<br>   in.write(content.getBytes(&quot;UTF-8&quot;))};<br>   <br>   // read stream from input<br>   try(BufferedInputStream out = new BufferedInputStream(process.getInputStream()); <br>   ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {<br>   <br>   while (true) {<br>     int x = out.read();<br>     if (x == -1) {<br>      break;<br>     }<br>     outputStream.write(x);<br>   }<br>   byte[] data = outputStream.toByteArray();<br>   process.waitFor();</pre><pre>   // save bytes to pdf<br>   FileOutputStream fileOutputStream = new FileOutputStream(&quot;test.pdf&quot;);<br>   fileOutputStream.write(data);</pre><pre>}      <br>  } catch (Exception e) {<br>   e.printStackTrace();<br>  }<br>}</pre><p>Notice that you have to generate pdf from by array after line code “process.waitFor()” because it would take a time for wkhtmltopdf running , so we need to ensure converting html to pdf completely first.</p><p>You can try above method to any Service class or your controller/api and see the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4yr2XIGATaePPe_56U4xQg.png" /></figure><p>Let’s style text by modify test.html and run again:</p><pre>&lt;style&gt;<br>  .format-text {<br>    font-size: 50px;<br>    color: &#39;red&#39;;<br>  }<br>&lt;/style&gt;<br>&lt;body&gt;<br> &lt;div class=&quot;format-text&quot;&gt;This is test of pdf exporting&lt;/div&gt;<br>&lt;/body&gt;</pre><p>See:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*x1E7D_c-WA5X2-OWHOUOvw.png" /><figcaption>after changing style</figcaption></figure><p>Step 3: In real case, we usually need to bind data at run-time, so let’s try to print text dynamically. You need to edit both java and html code:</p><p>html:</p><pre>&lt;div class=&quot;format-text&quot; th:text=&quot;${text}&quot;&gt;&lt;/div&gt;</pre><p>java: I simply bind a text value here but you can bind anything (number, string, object ) depend your situation</p><pre>context.setVariable(&quot;text&quot;, &quot;This is test of pdf exporting&quot;);</pre><p>That’s it. I hope that this article is helpful for you to have an one more option for pdf export. Happy coding :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a90c5dc44b0a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Alternative way for github]]></title>
            <link>https://medium.com/@chungkhanhduy/alternative-way-for-github-df05202777bc?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/df05202777bc</guid>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Sat, 29 Sep 2018 16:17:34 GMT</pubDate>
            <atom:updated>2018-09-29T16:25:37.053Z</atom:updated>
            <content:encoded><![CDATA[<p>Since github was acquired by Microsoft, some people think probably it won’t be free anymore. For me I’m not pretty care about that, but a few people said what is alternative way for github.com in case it’s not free anymore ? Well the answer probably is bitbucket.com. Bitbucket allow you free with un-limited repositories up to 5 users. However… this topic does not mention about bitbucket, that is AWS CodeCommit.</p><p>I actually don’t know AWS CodeCommit till my friend asked me for help when he want to use AWS CodeCommit for his project and he are in trouble to connect to it. The first my thought is why he used that and did not use bitbucket instead. AWS is best but it can be more expensive than other solution if you’re not careful in usage. However, I found that it offered the same as bitbucket did, free up to 5 users and unlimited repositories. If you’re team in crease, the cost is also cheap as well 1$ / user / month. So it’s really a secondary choice after bitbucket.com if your team is 5 or less. When the first time using it to help my friend, I ran into issues when can not connect repository even though following aws tutorial, so I&#39;m posting my experience here for you can refer. Note : this guidance used for Mac because I&#39;m using Mac :)</p><p>Step 1: Register</p><ul><li>First you need to create aws account , it’s free.</li><li>For other AWS services it offered tier free, but for CodeCommit you can use forever free if your team is less than or equal 5.</li><li>Once you registered account, please login and access CodeCommit</li></ul><p>Step 2: Create repository, I created repository “test&quot;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RqczWaClDewadPaHWPT4cg.png" /><figcaption>Create repository</figcaption></figure><p>Step 3: Install git. This step is super easy and I assumed that everyone already installed it</p><p>Step 4: Install AWS command line</p><ul><li>Install python 3.x first</li><li>Then run command lines:</li></ul><p><em>$ </em><strong><em>curl -O https://bootstrap.pypa.io/get-pip.py</em></strong></p><p><em>$ </em><strong><em>python3 get-pip.py --user</em></strong></p><p><em>$ </em><strong><em>pip3 install awscli --upgrade --user</em></strong></p><p><em>$ </em><strong><em>aws --version</em></strong></p><p>Step 5: Check out repository</p><ul><li><strong><em>git clone &lt;your-repo-https-link&gt;</em></strong></li><li>You can copy your repo https link from amazon console CodeCommit. In my example it was:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nd7DPIBlBRYQHFMu72kLrA.png" /></figure><p>Step 6: Now you should see your repository folder. In this case that is “test” folder. Then you can create new file README.md, and commit it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8l9EOTjie8w3wdNva4v0SA.png" /></figure><p>Step 7: This is __very__ important step:</p><ul><li>AWS CodeCommit does not ask you user/name or password. Instead it will asked you account key and secret Key, so you have to configure them in profile using aws command line. In terminal, type “aws configure” and enter account key and secret key of your aws account.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GAFJl8HktRJfTCmnYoq4FQ.png" /></figure><ul><li>Note that your region name must be matched with region that you’re created your repository. Otherwise you can not connect your repo :)</li></ul><p>Step 8: Again, this is __So__ important step too:</p><ul><li>AWS CodeCommit won’t use key chain for credential.helper to remember your credential. So you have to run :</li><li><strong><em>cd &lt;your-folder&gt;</em></strong></li><li><strong><em>Open .git/config file and add below info:</em></strong></li></ul><pre>[credential]<br>   helper = !aws codecommit credential-helper $@<br>   UseHttpPath = true</pre><ul><li>DISABLE your keychain for git.&lt;region&gt;.codecomit…. by:</li></ul><blockquote><strong><em>1. Open Your key chain access in Mac</em></strong></blockquote><blockquote><strong><em>2. Search for “git.&lt;region&gt;.codecomit…”</em></strong></blockquote><blockquote><strong><em>3. Select Access Control tab, select “git-credential-oskeychain” and click (-) button to remove it.</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5QEKWIm9BrA0O9fCTcO8GA.png" /><figcaption>Dialog for remove keychain</figcaption></figure><p>Step 9: You looks passing important things and now it’s time to try a push</p><ul><li>git push -u origin master</li></ul><p>And… You will see popup to appear, click “Deny”, then it will push successfully</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSbZOG05UvzjMxGrm7s3KQ.png" /><figcaption>popup to show</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h-9Ov7Cs6JLEwgeqFDj2vQ.png" /><figcaption>Pushed successfully</figcaption></figure><p>This a little bit tough since you have to click “Deny” every time to push/pull because it won’t use keychain to look for credential anymore and use aws command line instead.</p><p>So now you can have another option for team collaboration working ;-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df05202777bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building web api application entirely with Elixir (Phoenix framework ) and AngularJS — Part 1 …]]></title>
            <link>https://medium.com/@chungkhanhduy/building-web-api-application-entirely-with-elixir-phoenix-framework-and-angularjs-part-1-4989547b5fbd?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/4989547b5fbd</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Tue, 21 Aug 2018 08:37:09 GMT</pubDate>
            <atom:updated>2018-08-21T08:37:09.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building web api application entirely with Elixir (Phoenix framework ) and AngularJS — Part 1 : Building back-end app with phoenix</h3><p>Why I chose Elixir as back-end ?</p><ul><li>OOP was born long time ago and now it&#39;s seems trend of functional programming, so I’d like to learn more in functional programming in practice. Java Lambda probably is not enough (I worked mostly with Java)</li><li>Elixir is one of candidate of function programming languages</li><li>Some topics talked about Elixir, it based on Erlang, a great combination between beautiful syntax ( similar Ruby ) and fast ( similar Java due to it run on Erlang VM ), I’ve actually not know how fast and nice syntax it is, just simple try something new (new language, new functional programming style)</li></ul><p><strong>Build back-end api:</strong></p><ul><li>A few framework to support build web api using Elixir, after quick review, Phoenix is looks like more popular , so I decided to start with it</li></ul><ol><li>Install Erlang</li></ol><pre>brew install erlang</pre><p>2. Install Elixir</p><pre>brew install elixir</pre><p>3. Install PostgreSQL ( phoenix default to use PostgreSQL )</p><pre>brew install postgresql</pre><p>4. Install Phoenix (for more information about <a href="https://phoenixframework.org/">phoenix</a> ). Note that installing hex is required because hex is package manager to help to pull dependencies ( my thought it’s similar maven or gradle in java )</p><pre>mix local.hex<br>mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez</pre><p>5. Install node</p><pre>brew install node</pre><p>6. Create your back-end app:</p><pre>mix phoenix.create &lt;your-app&gt;<br>cd &lt;your-app&gt;<br>sudo npm install<br>mix ecto.create</pre><p>You probably got this error when creating ecto “(Mix) The database for ElixirApiSample.Repo couldn’t be created: ERROR 22023 (invalid_parameter_value): new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)”, if so please open &lt;your-app&gt;/config/dev.exs and adding below row (before poolsize ):</p><pre>template: &quot;template0&quot;</pre><p>7. Then go ahead</p><pre>mix deps.get<br>mix compile</pre><p>8. Finally run back-end server:</p><pre>mix phoenix.server</pre><p>Result will looks like as below and try to browse at localhost:4000</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lsT9-EumYthjC8i1jG2vxg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KDejSzsmmziABAaIA6SZIQ.png" /></figure><p>Part 2: Restful api to demo GET/POST — coming soon</p><p>Part 3: Generate front-end app , integrate with GET/POST api— coming soon</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4989547b5fbd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create work(s) queue with SpringBoot and RabbitMQ]]></title>
            <link>https://medium.com/@chungkhanhduy/create-work-s-queue-with-spring-and-rabbitmq-978cad149672?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/978cad149672</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[rabbitmq]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Wed, 04 Jul 2018 09:34:10 GMT</pubDate>
            <atom:updated>2019-07-18T09:52:32.805Z</atom:updated>
            <content:encoded><![CDATA[<p>Some time you need to design a work(task) queue to processing your dataset. The task queue approach is useful when your system probably receive a lot of requests at the same time and you don’t need to response them immediately, then you put each request in queue and process them sequently. There are a lot use cases that you can apply task queue in real projects. So hopefully the article will help to get you understand and basically set up the task queue for your different purposes.</p><p>But wait… If you’re lazy to read, here is full video to demonstrate how to do <a href="https://www.youtube.com/watch?v=gGL0e0KvREE&amp;feature=youtu.be">https://www.youtube.com/watch?v=gGL0e0KvREE&amp;feature=youtu.be</a>, you can take a look without continue reading :)</p><ol><li>Install RabbitMQ, this installation guidance used for MacOS only, depend on your OS , you can find out how to install RabbitMQ. Here is the official site: <a href="https://www.rabbitmq.com/download.html">https://www.rabbitmq.com/download.html</a></li></ol><blockquote>brew install rabbitmq</blockquote><p>Default rabbitMQ server was not in your class path , so you need to set it up manually in order to easily to kick it off. RabbitMQ was installed in /usr/local/sbin folder and you need to set path for it in ~/.bash_profile. Please remember refresh your bash profile after setting.</p><blockquote>PATH=$PATH:/usr/local/sbin</blockquote><p>Then run rabbitMQ server by command line:</p><blockquote>rabbitmq-server</blockquote><p>The screenshot of terminal looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tSL18V9lKk4V5KLnmsWuxA.png" /></figure><p>2. Add springboot-starter-amqp library in your project, I’m using gradle to build project so here is what I added in .gradle file ( you can easily find it in maven repository with using maven instead )</p><blockquote>compile “org.springframework.boot:spring-boot-starter-amqp”</blockquote><p>3. Create a Configuration for SpringBoot, I named it as ‘RabbitConfiguration’:</p><pre>package com.example.spring.rabbitmq.config;<br><br>import org.springframework.amqp.core.Queue;<br>import org.springframework.beans.factory.annotation.Configurable;<br>import org.springframework.context.annotation.Bean;<br>import org.springframework.context.annotation.Configuration;<br>import org.springframework.context.annotation.Profile;<br><br>import com.example.spring.rabbitmq.service.TaskReceiver;<br>import com.example.spring.rabbitmq.service.TaskSender;<br><br>@Configuration<br>public class RabbitMQConfiguration {<br><br>    @Bean<br>    public Queue hello() {<br>        return new Queue(&quot;hello&quot;);<br>    }<br>	<br>    private static class ReceiverConfig {<br><br>        @Bean<br>        public TaskReceiver receiver() {<br>            return new TaskReceiver();<br>        }<br><br>    }<br><br>}</pre><p>In this configuration we create a queue ‘hello’ and used this queue to persist your waiting task. Then TaskReceiver will pick up the task from queue and process. In this example I just demonstrate sending a few message ‘hello’ task and check if receiver can listen and pick up message and process message. The method doWork() sleep for awhile to demo processing something when receiving message. Note that you have to mention correctly queue name in @RabbitListener</p><p>TaskReceiver.java</p><pre>package com.example.spring.rabbitmq.service;<br><br>import org.springframework.amqp.rabbit.annotation.RabbitHandler;<br>import org.springframework.amqp.rabbit.annotation.RabbitListener;<br>import org.springframework.util.StopWatch;<br><br>@RabbitListener(queues = &quot;hello&quot;)<br>public class TaskReceiver {<br>	<br>    @RabbitHandler<br>    public void receive(String in) throws InterruptedException {<br>        StopWatch watch = new StopWatch();<br>        watch.start();<br>        System.out.println(&quot; [x] Received &#39;&quot; + in + &quot;&#39;&quot;);<br>        doWork(in);<br>        watch.stop();<br>        System.out.println(&quot; [x] Done in &quot; + watch.getTotalTimeSeconds() + &quot;s&quot;);<br>    }<br><br>    private void doWork(String in) throws InterruptedException {<br>        for (char ch : in.toCharArray()) {<br>            if (ch == &#39;.&#39;) {<br>                Thread.sleep(1000);<br>            }<br>        }<br>    }<br>}</pre><p>TaskSender.java: This service used to demonstrate send some tasks (message ) to queue and listener will pickup later. @PostContruct will help to execute send() method automatically when init service.</p><pre>@Service<br>public class TaskSender {<br><br>    @Autowired<br>    private RabbitTemplate template;<br><br>    @Autowired<br>    private Queue queue;<br><br>    @PostConstruct<br>    public void send() {<br>    		try {<br>	    		for (int i = 0; i &lt; 5; i++) {<br>		        String message = &quot;hello&quot; + i;<br>		        template.convertAndSend(queue.getName(), message);<br>		        System.out.println(&quot; [x] Sent &#39;&quot; + message + &quot;&#39;&quot;);<br>	    		}<br>    		} catch (Exception e) {<br>				e.printStackTrace();<br>		}<br>    }<br>	<br>}</pre><p>4. You also need to provide connection information in your .yml file to help springboot-start-amqp connect to RabbitMQ:</p><pre>rabbitmq:<br>        host: 127.0.0.1<br>        port: 5672<br>        username: guest<br>        password: guest</pre><p>Now you can run your SpringBoot app and result should looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SKLs4cI3wyFvWUMsEc_Rjg.png" /></figure><p>Demo sent 5 messages which represent for 5 tasks and listener will pick up 5 tasks from queue and process. Note that you can send any Object instead depend your complexity of the task.</p><p>Full source code here: <a href="https://github.com/duymap/spring-rabbitmq-workqueue">https://github.com/duymap/spring-rabbitmq-workqueue</a></p><p>Please clap if you like this post ^_^</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=978cad149672" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Install multiple nodejs version for your environment development]]></title>
            <link>https://medium.com/@chungkhanhduy/install-multiple-nodejs-for-your-environment-development-8231848c5892?source=rss-bc4bc3daf71e------2</link>
            <guid isPermaLink="false">https://medium.com/p/8231848c5892</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Chung Khanh Duy]]></dc:creator>
            <pubDate>Thu, 28 Jun 2018 02:24:11 GMT</pubDate>
            <atom:updated>2018-06-28T02:25:22.373Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I ran into the issue when working with multiple projects using nodejs to build. Each project using different nodejs version and unfortunately few of versions are not compatible, it makes me crazy to downgrade and upgrade again when switching and you can imagine that how much frustrated I am…damm. So I was thinking about whether there is a way to install multiple nodejs versions and select appropriate one when you need, that would be much helpful for my job. And … nvm can answer my question, like it said, this is nodejs manager tool to allow you install how many version that you want and use it when you need. I tried it and that works, so I think that would be worth to record steps here in case anyone who want to do this. You can watch video how to do here: <a href="https://t.umblr.com/redirect?z=http%3A%2F%2Fbit.ly%2F2fVhtv7&amp;t=MDRiMzc4Mjc4OTIzNjJlMWE4ZTk2ZWZhY2Q4MTE0YzM0ZTc3M2U0MSw5dUlIT1l1Tw%3D%3D&amp;b=t%3AYFpY-p-wXD2xyWt_1HIWpQ&amp;p=http%3A%2F%2Fduymap.tumblr.com%2Fpost%2F164369020827%2Finstall-multiple-nodejs-versions-in-development">http://bit.ly/2fVhtv7</a>. Good luck !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8231848c5892" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>