<?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 Vitalii Kolesov on Medium]]></title>
        <description><![CDATA[Stories by Vitalii Kolesov on Medium]]></description>
        <link>https://medium.com/@kvs1904?source=rss-b0be76cb0a83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bcEMYAvFYdrIAXvrlmQRIQ.png</url>
            <title>Stories by Vitalii Kolesov on Medium</title>
            <link>https://medium.com/@kvs1904?source=rss-b0be76cb0a83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 11:26:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kvs1904/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[Part 2: Dockerize SPA based on Nginx +VueJs+Python-Sanic.]]></title>
            <link>https://medium.com/@kvs1904/part-2-dockerize-spa-based-on-nginx-vuejs-python-sanic-ff10386228d7?source=rss-b0be76cb0a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff10386228d7</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[nginx]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Vitalii Kolesov]]></dc:creator>
            <pubDate>Wed, 27 Feb 2019 18:37:58 GMT</pubDate>
            <atom:updated>2019-03-19T21:16:47.012Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vaykiTgCdOobSvHE" /><figcaption>Photo by <a href="https://unsplash.com/@nosaka?utm_source=medium&amp;utm_medium=referral">nikko osaka</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a previous <a href="https://medium.com/@kvs1904/article-1-dockerize-spa-based-on-nginx-vuejs-python-sanic-c135d264c2f9">article</a>, we have built the “skeleton” for our future SPA. We have already installed PostgreSQL, Redis, Nginx, and Adminer (tool for DB management). In this part, we will develop container with python code for /api/* routes and cover it with tests.</p><h4>TL;DR</h4><p>If you have no time to read, just exec next bash commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01c63024cb282189f006461dde195ed1/href">https://medium.com/media/01c63024cb282189f006461dde195ed1/href</a></iframe><p>and run a browser with the <a href="http://localhost/">http://localhost</a>.</p><h4>Let’s talk about what we will do in this part:</h4><ol><li>Our back-end should accept POST request for <em>/api/v1.0/user/auth</em> route with data contains <strong><em>username</em></strong> and <strong><em>password</em></strong>. If the user exists in the database, it should create unique token key save <strong><em>user.id</em></strong> to <strong><em>Redis</em></strong> and give this token back in JSON response.</li><li>If requested data is invalid back-end should return a proper error(s) in JSON format.</li></ol><h3>Stage 1: Before we will start…</h3><p>On developing stage there are a lot of things that can make you “bored”. As an instance when you have a deal with docker you should type mass of commands that is a bit “talkative”. I have found useful to use <strong>make</strong> utility for making short alias for long commands. In the root directory of the project I have placed next Makefile:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c0c89cb010869737581ebf299141b67/href">https://medium.com/media/8c0c89cb010869737581ebf299141b67/href</a></iframe><p>So with that commands it is possible to save time:</p><ul><li><strong><em>make up </em></strong>— just start all containers, whereas <strong>make upb </strong>—start and rebuild them;</li><li><strong>make stop</strong> — obviously will stop containers;</li><li><strong>make db</strong> — the way how to connect to working “<strong>db”</strong> container and automatically connect to the database with the <strong><em>psql</em></strong> utility. We include all variables from the .env file, so we can use all of them in instructions;</li><li><strong>make r</strong> — quickest way to get access to Redis storage with<strong><em> redis-cli</em></strong>;</li><li><strong>make test</strong> — runs all <em>unit/integrity tests directly </em>inside the container;</li><li><strong>make b</strong> — is a way quickly connect to bash console for a specific container. (an example: <strong><em>make b c=test_api</em></strong> will connect to bash console of the container with name “test_api”).</li></ul><h3>Stage 2: Creating minimal API</h3><p>So create api/requirements.txt with next content:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95dd4d1e7b7160f879d9b635f47ae816/href">https://medium.com/media/95dd4d1e7b7160f879d9b635f47ae816/href</a></iframe><p>Before we start to make the logic of API, we should create a minimal working Sanic application put it into the container and setting up route proxy for Nginx. I have got the “hello world” example <a href="https://sanic.readthedocs.io/en/latest/">here</a> make some minor changes and save it to app/run.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7785cf6c2d134f7304abafcbc8b92cd/href">https://medium.com/media/b7785cf6c2d134f7304abafcbc8b92cd/href</a></iframe><p>Also, we need a container for our API. So place the Dockerfile</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/635379601e27e6826333f9a42ece9d41/href">https://medium.com/media/635379601e27e6826333f9a42ece9d41/href</a></iframe><p>into “/api” directory.</p><p>In Dockerfile we have got the latest official stable image of python3.6 and added some software that would be used by some python packages. Inside the container, we have defined the “/app” directory as “workdir”. Also, we have given instruction to docker to execute command “<strong><em>python run.py</em></strong>” at the moment when the container will start.</p><h3>Stage 3: add new API container to the docker-compose.yml</h3><p>We should add some lines into the section “services” of our main docker-compose.yml file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b91b705c349df1b9e99fdc7a0b6208a/href">https://medium.com/media/0b91b705c349df1b9e99fdc7a0b6208a/href</a></iframe><p>In the instructions above we:</p><ul><li>“share” content of “api” directory on host machine with /app directory on the container</li><li>make available the environment variables from .env file to the application inside the container;</li><li><strong>tty: true </strong>— it makes possible for us in the future to be attached and detached (with CTRL+C) from main docker process (<strong><em>python run.py</em></strong>) for debugging purpose.</li></ul><h3>Stage 4: Tuning Nginx for API.</h3><p>Add next block of nginx configuration into nginx/server.conf file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab208e0f731044054928c754765280dd/href">https://medium.com/media/ab208e0f731044054928c754765280dd/href</a></iframe><p>Now restart and rebuild containers with “<strong>make upb</strong>” command and go to the <a href="http://localhost/api"><strong>http://localhost/api</strong></a> in a browser. You should see: <em>{“hello”: ”world”}.</em></p><h3>Stage 5: Developing API and database migration hook.</h3><p>Into api directory place application.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ed58702aa9e87b3d7e63cb65c027def/href">https://medium.com/media/0ed58702aa9e87b3d7e63cb65c027def/href</a></iframe><p>In application.py was defined “create” function (pattern fabric). This function returns an instance of <em>Sanic</em> application. Pay attention for lines 20–23. In those lines, we concatenate the Data Source Name (DSN) string and use some container’s environment variables. As you can see, we hard-coded “db” as an alias for the host database. It should be the same name as PostgreSQL container was named in docker-compose.yml file in section “<em>services</em>”. Check that host for Redis connection is also hard-coded in the same way (line 26).</p><blockquote>If docker instances are belong to the same network (in our case both is in “internal” network) docker automatically resolve “DSN” to proper IP address.</blockquote><p>Also, pay attention to <em>db_migrate</em> function. Of course, there is a lot of solution for database migration purpose out there. Most popular is <a href="https://alembic.sqlalchemy.org/en/latest/">Alembic</a>. But when we get to deal with microservice architecture, I think simpler solutions are better and faster. That is why I don’t have used Sqlalchemy in this case. So, We have migration.py:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc50e8aa5ff0a904e7959fd0b2827e69/href">https://medium.com/media/dc50e8aa5ff0a904e7959fd0b2827e69/href</a></iframe><p>A key feature of the migration mechanism is the value of LATEST_VERSION. When our application will start LATEST_VERSION value would be compared to the previous version that stored in the database table. If it greater then the application runs all SQL-instructions from key “up” and, runs all SQL-instructions from key “down” otherwise.</p><p>The logic of authorization is plain:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/353fefcd983f4f5d553ea4a87045d69c/href">https://medium.com/media/353fefcd983f4f5d553ea4a87045d69c/href</a></iframe><p>Some integration tests expose here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/370a52913f0963c8196924048a5a5486/href">https://medium.com/media/370a52913f0963c8196924048a5a5486/href</a></iframe><p>It is hard to place each piece of code here, so I have created a <a href="https://github.com/v-kolesov/vue-sanic-spa/pull/1/files">pull request for all changes that were done for Part 2</a>.</p><p><strong><em>To be continued …</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff10386228d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Article 1: Dockerize SPA based on Nginx +VueJs+Python-Sanic.]]></title>
            <link>https://medium.com/@kvs1904/article-1-dockerize-spa-based-on-nginx-vuejs-python-sanic-c135d264c2f9?source=rss-b0be76cb0a83------2</link>
            <guid isPermaLink="false">https://medium.com/p/c135d264c2f9</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[sanic]]></category>
            <category><![CDATA[single-page-applications]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Vitalii Kolesov]]></dc:creator>
            <pubDate>Tue, 26 Feb 2019 19:41:22 GMT</pubDate>
            <atom:updated>2019-03-19T21:10:12.045Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 1: Dockerize SPA based on Nginx +VueJs+Python-Sanic.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9NilpY8yGuYc8nnV" /><figcaption>Photo by <a href="https://unsplash.com/@frankiefoto?utm_source=medium&amp;utm_medium=referral">frank mckenna</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Motivation: In this article, we will build a bunch of Docker containers and go step by step through the process of building the chat SPA (<a href="https://en.wikipedia.org/wiki/Single-page_application">single page application</a>) with the basic authorization. We will be using:</p><ul><li><a href="http://vue.js/"><strong>Vue</strong></a> — JavaScript front-end framework;</li><li><a href="https://sanic.readthedocs.io/en/latest/"><strong>Python Sanic</strong></a><strong> </strong>— asynchronous framework for back-end (authorization API and WebSocket server);</li><li><strong>PostgreSQL</strong> as a database (in our case store for users);</li><li><strong>Redis</strong> for session storage;</li><li><strong>Nginx</strong> as the proxy server for routing of incoming requests between our services.</li><li><a href="https://www.adminer.org/"><strong>Adminer</strong></a> — a tool for working with database (personally I prefer console, but who knows).</li></ul><p>To push all those things to work together, we will use <a href="https://www.docker.com/">Docker</a> and “docker-compose”.</p><h4>TL;DR</h4><p>If you have no time to read, just exec next bash commands:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01c63024cb282189f006461dde195ed1/href">https://medium.com/media/01c63024cb282189f006461dde195ed1/href</a></iframe><p>and run a browser with the <a href="http://localhost/">http://localhost</a>.</p><h3>Stage 0: Preparing</h3><p>Briefly about my PC (host machine):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07bca78c0be2213583d3b47915752545/href">https://medium.com/media/07bca78c0be2213583d3b47915752545/href</a></iframe><p>return:</p><blockquote>PRETTY_NAME=”Ubuntu 18.04.2 LTS”<br>Docker version 18.09.2, build 6247962<br>docker-compose version 1.23.2, build 1110ad01<br>git version 2.17.1<br>GNU Make 4.1</blockquote><p>Also for developing purpose we need some code editor. Personally, I prefer <a href="https://code.visualstudio.com/">VSCode</a> with a bunch of plugins for Python and JavaScript, but you are free to choose any other.</p><h3>Stage 0: How does SPA would be work?</h3><p>Our application should work on <a href="http://localhost/">http://localhost</a>. There are 4 main routing rules:</p><ol><li>“/ “ — here should be load our Chat SPA. For this route, we will create VueJs application in “app” directory. If no “token” key in localStorage, SPA should redirect to /#/login page for authorization. Login form send email/password to some API URL (for instance /api/v1.0/user/auth).</li><li>“/api” — all requests that started with this, should be redirected to API microservice that build with a Sanic;</li><li>“/db” — all requests that started with this, relate to “adminer” tool;</li><li>“/ws” — route for WebSocket server. It is a dedicated instance through that chat will send and receive messages.</li></ol><p>To realize 1–4 we will use Nginx web-server.</p><h3>Stage 1: Project Initialization.</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a67686a9e3ba5a2fd744594689ef2fb7/href">https://medium.com/media/a67686a9e3ba5a2fd744594689ef2fb7/href</a></iframe><h3>Stage 2: Containerization</h3><p>In stage 1 we have just created the empty .env. So fill it with default environment variables. When we will build container we pass all these variables into all of them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd93d1b09866d277c6eab78e828b244a/href">https://medium.com/media/dd93d1b09866d277c6eab78e828b244a/href</a></iframe><blockquote>It is good idea to have different .env file for development, production and testing mode and of course, it shouldn&#39;t be inside the git repository for security reason.</blockquote><p>I think the defined variables no need to be explained. Next step is creating the docker-compose.yml file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7472bf30c574784baf1ed85251aad5d6/href">https://medium.com/media/7472bf30c574784baf1ed85251aad5d6/href</a></iframe><p>In 2–6 lines we defined 2 different networks: “web” and “internal”. The “web” will be used for exchange data between the client’s browser and web server (Nginx). The “internal” network will link “back-end services” between each other. More theory about networks can read <a href="https://docs.docker.com/network/">here</a>.</p><h4>redis</h4><p>In 9–15 lines we have created “redis” container (based on official docker image) and shared “./data/redis” directory between host machine and container. In line #13 we have used variable {PWD} that is containing the value of the same environment variable of the host machine.</p><h4>db</h4><p>This service was built on a top of official PostgreSQL docker image. In lines 23–25 we have defined several environment variables for the container with values from the .env file. For this purpose, there are 2 ways. First one is through the “environment” section of current service where it is necessary to set the key-value pair. 2nd way is through the env_file variable when we forward all the variables of the .env file directly to the container (see how it has done in the file for Nginx service).</p><h4>adminer</h4><p>In 33–39 lines we have created a container based on the <a href="https://hub.docker.com/_/adminer/">official adminer image</a>. As you can see adminer has the same “network” as “db” has. In adminer’s docker documentation is described that throw environment variable named ADMINER_DEFAULT_SERVER it is possible to set up default driver, so we have placed it to .env file and attach it in line with the number 37.</p><h4>nginx</h4><p>In lines 41–55 we are creating Nginx container. Most important instruction is in line #47 (volumes section). On a host machine, we have created nginx/server.conf with</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/740bbd25e7b5e26f92c84713f6ac191c/href">https://medium.com/media/740bbd25e7b5e26f92c84713f6ac191c/href</a></iframe><p>The instruction “./nginx:/etc/nginx/conf.d” makes all nginx directory content available for the container on his own special path (the path is described on <a href="https://hub.docker.com/_/nginx">nginx docker documentation</a>).</p><h3>Stage 3: run it all</h3><p>If we have done all properly just run “<strong>docker-compose up -d</strong>”.</p><p>This command should run all containers in a background mode. After that in the browser go to http://localhost/db you should see the auth page of adminer . Use data from the .env file to connect to database cause this data was also used by building the db container.</p><p><a href="https://medium.com/@kvs1904/part-2-dockerize-spa-based-on-nginx-vuejs-python-sanic-ff10386228d7"><strong><em>Part 2</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c135d264c2f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>