<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oleks</title>
    <description>The latest articles on DEV Community by Oleks (@ale_ukr).</description>
    <link>https://dev.to/ale_ukr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F87834%2Fce05be80-16e8-4368-88c6-4281753a5547.webp</url>
      <title>DEV Community: Oleks</title>
      <link>https://dev.to/ale_ukr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ale_ukr"/>
    <language>en</language>
    <item>
      <title>Control Home Assistant with a self-hosted AI bot team (kwant 0.9.21)</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:16:29 +0000</pubDate>
      <link>https://dev.to/ale_ukr/control-home-assistant-with-a-self-hosted-ai-bot-team-kwant-0921-3gha</link>
      <guid>https://dev.to/ale_ukr/control-home-assistant-with-a-self-hosted-ai-bot-team-kwant-0921-3gha</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: I'm Oleksandr, the author of kwant. Links: &lt;a href="https://github.com/alexsergivan/kwant" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; · &lt;a href="https://kwant.si" rel="noopener noreferrer"&gt;kwant.si&lt;/a&gt; · &lt;a href="https://kwant.si/docs/mcp" rel="noopener noreferrer"&gt;MCP docs&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
Originally published on the &lt;a href="https://kwant.si/blog/control-home-assistant-with-a-self-hosted-bot" rel="noopener noreferrer"&gt;kwant blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://github.com/alexsergivan/kwant" rel="noopener noreferrer"&gt;kwant&lt;/a&gt;, an open-source team of AI bots you run yourself. In 0.9.21 it can talk to Home Assistant's official MCP server in two fields — an address and a long-lived access token — and it ships a Home team with a bot called House.&lt;/p&gt;

&lt;p&gt;The interesting part for a Home Assistant setup is the caution around the house: reading state can run without a card, lights still ask in the default approval mode, and locks, doors, garage doors, gates and alarms always ask, even if approvals are off. A Lights out routine is included and starts off, so nothing changes at 22:00 until you turn it on.&lt;/p&gt;

&lt;p&gt;I tested this against Home Assistant 2026.11.1. The connect sheet is straightforward (this test showed "Connected · 21 actions"). For House, a cloud or larger model is the safer pick. A local 7B Ollama model (qwen2.5-home7, from qwen2.5:7b) was fine when I named the room or device — "Turn off the living room lamp" and "What is the living room temperature right now?" worked. Vague asks were not: "Is anything left on?" and "How warm is it inside?" missed devices, and after a confirmation to unlock the front door the 7B bot replied "Unlocking the front door now." with no tool steps. Don't use 3B for this.&lt;/p&gt;

&lt;p&gt;The steps below start on the Home Assistant side (MCP Server, exposing entities to Assist, making the token), then walk through Connect Home Assistant, House, approvals and the routine. The lock-approval screenshot is the official 0.9.21 shot from &lt;a href="https://kwant.si/features#home-locks-always-ask" rel="noopener noreferrer"&gt;kwant.si&lt;/a&gt;; the 7B test never produced its own card.&lt;/p&gt;
&lt;h2&gt;
  
  
  What you need
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A running &lt;a href="https://www.home-assistant.io/" rel="noopener noreferrer"&gt;Home Assistant&lt;/a&gt; instance you can reach from the machine that runs kwant (same computer or your LAN).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/alexsergivan/kwant" rel="noopener noreferrer"&gt;kwant 0.9.21&lt;/a&gt; or later, installed and open. A Mac app, a Linux desktop or server, or Docker all work.&lt;/li&gt;
&lt;li&gt;An administrator Home Assistant user. The main MCP URL requires an admin account when it exposes the full set of tools.&lt;/li&gt;
&lt;li&gt;An AI account for House. Prefer a cloud model, or a local model larger than 7B, if you want House to handle broad questions and lock-style requests reliably. A 7B Ollama model is enough for short, specific asks that name the room or device. Don't use 3B for this: &lt;strong&gt;qwen2.5 3B&lt;/strong&gt; often sent invalid tool arguments and sometimes announced success after a step had already failed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You do not need Home Assistant to be on the public internet. Everything here stays on your machine or your LAN.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Enable Home Assistant's MCP server
&lt;/h2&gt;

&lt;p&gt;Home Assistant is the MCP &lt;em&gt;server&lt;/em&gt; in this setup: your kwant bot is the client that calls its tools. (Home Assistant also has a separate Model Context Protocol &lt;em&gt;Client&lt;/em&gt; integration, which points the other way. You don't need that here.)&lt;/p&gt;

&lt;p&gt;The official integration page is &lt;a href="https://www.home-assistant.io/integrations/mcp_server/" rel="noopener noreferrer"&gt;Model Context Protocol Server&lt;/a&gt;. On current Home Assistant (2026.11 and later) the integration is named &lt;strong&gt;Model Context Protocol&lt;/strong&gt;. Before 2026.11 it was &lt;strong&gt;Model Context Protocol Server&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You can enable it from &lt;strong&gt;Settings → System → AI&lt;/strong&gt;. If the Model Context Protocol card shows &lt;strong&gt;Enable&lt;/strong&gt;, select it. You can also add it by hand: &lt;strong&gt;Settings → Devices &amp;amp; services → Add integration&lt;/strong&gt;, then choose &lt;strong&gt;Model Context Protocol&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;On a fresh install the main URL exposes all AI tool sets (Home Assistant may title this "All LLM APIs" or "Expose all AI tools"). You can later restrict that under &lt;strong&gt;Configure&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;kwant always connects to the main URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://&amp;lt;your-home-assistant-host&amp;gt;:8123/api/mcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, &lt;code&gt;http://homeassistant.local:8123/api/mcp&lt;/code&gt;. You don't have to type &lt;code&gt;/api/mcp&lt;/code&gt; into kwant; the Connect sheet adds it.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Expose the entities the bot should use
&lt;/h2&gt;

&lt;p&gt;Device-control tools (the same ones Assist uses, including &lt;code&gt;HassTurnOn&lt;/code&gt;, &lt;code&gt;HassTurnOff&lt;/code&gt; and &lt;code&gt;GetLiveContext&lt;/code&gt;) only see entities you have exposed to voice assistants.&lt;/p&gt;

&lt;p&gt;In Home Assistant, open &lt;strong&gt;Settings → Voice assistants&lt;/strong&gt; and use the &lt;strong&gt;Expose&lt;/strong&gt; (exposed entities) page. Expose the lights, sensors and other devices you actually want the bot to read or control.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2x1dl71xj43eo7qyzfk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2x1dl71xj43eo7qyzfk6.png" alt="The Home Assistant Expose page, listing Front door, Kitchen light, Living room lamp, Living room temperature, Porch light and Shopping list as exposed to Assist" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can leave locks, garage doors, gates and alarms unexposed if you don't want them in the tool list at all. If you do expose them, kwant still always shows an approval card before a bot can unlock, open or disarm them — even with approvals turned off. That card offers no &lt;strong&gt;Always allow&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If a later prompt fails with "No entities matched", this is the first place to check: the friendly name must be exposed, and the bot must use that name, not the &lt;code&gt;entity_id&lt;/code&gt; (&lt;code&gt;light.living_room_lamp&lt;/code&gt; is not the same as &lt;code&gt;Living room lamp&lt;/code&gt;). A query for the name &lt;code&gt;temperature&lt;/code&gt; will miss a sensor called &lt;strong&gt;Living room temperature&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The Connect sheet also notes that you can limit what bots can do under &lt;strong&gt;Model Context Protocol → Configure&lt;/strong&gt;. That is a second, coarser filter (which AI tool sets the server offers). Exposing individual entities on the Assist page is still what decides which devices &lt;code&gt;HassTurnOn&lt;/code&gt; / &lt;code&gt;HassTurnOff&lt;/code&gt; can see.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Make a long-lived access token
&lt;/h2&gt;

&lt;p&gt;In Home Assistant, open your profile, then &lt;strong&gt;Security&lt;/strong&gt;, then &lt;strong&gt;Long-lived access tokens&lt;/strong&gt;, and create a token. Copy it once; Home Assistant will not show it again.&lt;/p&gt;

&lt;p&gt;Give it a name you'll recognise, like &lt;code&gt;kwant&lt;/code&gt;. Treat it like a password: it lets whoever holds it act as you in Home Assistant.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Connect Home Assistant in two fields
&lt;/h2&gt;

&lt;p&gt;In kwant 0.9.21 you no longer add Home Assistant as a generic MCP server by hand. Open &lt;strong&gt;Connect Home Assistant&lt;/strong&gt; from either place:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tools → Find a tool&lt;/strong&gt;, then choose &lt;strong&gt;Home Assistant&lt;/strong&gt;, or&lt;/li&gt;
&lt;li&gt;Start the &lt;strong&gt;Home&lt;/strong&gt; team (see below) and use its &lt;strong&gt;Connect Home Assistant&lt;/strong&gt; need.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The sheet has two fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Address&lt;/strong&gt; — the address you use to open Home Assistant, like &lt;code&gt;homeassistant.local:8123&lt;/code&gt; or &lt;code&gt;http://192.168.1.20:8123&lt;/code&gt;. The placeholder is &lt;code&gt;homeassistant.local:8123&lt;/code&gt;. Once the address looks valid, the help line reads &lt;strong&gt;kwant will connect to&lt;/strong&gt; &lt;code&gt;http://…:8123/api/mcp&lt;/code&gt;. You can omit the scheme and, on plain http, the port: &lt;code&gt;homeassistant.local&lt;/code&gt; becomes &lt;code&gt;http://homeassistant.local:8123/api/mcp&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access token&lt;/strong&gt; — paste the long-lived token. The help text points at &lt;strong&gt;profile → Security → Long-lived access tokens → Create token&lt;/strong&gt;. The &lt;strong&gt;Make one in Home Assistant&lt;/strong&gt; link opens that Security page once an address is filled in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two notes sit under the fields: bots can reach every device unless you limit them in Home Assistant, and locks, doors, garage doors, gates and alarms always ask you first.&lt;/p&gt;

&lt;p&gt;Press &lt;strong&gt;Connect&lt;/strong&gt;. The sheet tests the connection and reports the result in plain words:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What you see&lt;/th&gt;
&lt;th&gt;What it means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Connected · N actions&lt;/strong&gt;, with a &lt;strong&gt;Tested&lt;/strong&gt; sticker&lt;/td&gt;
&lt;td&gt;The server answered and listed its tools. From the Home team, it also says &lt;strong&gt;House can use it now.&lt;/strong&gt; From Tools, it says the server is saved and you can start a Home team, or turn it on for any bot in that bot's settings. This test connected with &lt;strong&gt;21 actions&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Home Assistant didn't accept this access token.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Make a new token and paste the whole thing.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Home Assistant answered, but its MCP Server is off.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add &lt;strong&gt;Model Context Protocol&lt;/strong&gt; under &lt;strong&gt;Settings → Devices &amp;amp; services → Add integration&lt;/strong&gt;, then try again. The sheet links &lt;strong&gt;Open integrations in Home Assistant&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;kwant can't reach &amp;lt;host&amp;gt;.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Home Assistant is off, or the address is wrong. If kwant runs on another computer or in Docker, use Home Assistant's IP address, like &lt;code&gt;http://192.168.1.20:8123&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;This address answered, but it isn't Home Assistant.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The URL is reachable, but it didn't identify itself as Home Assistant's MCP server. Check the address.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Couldn't connect to Home Assistant.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Something else went wrong. Check that the address opens Home Assistant in a browser, then try again.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6kitsrlur8xchnexkc7o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6kitsrlur8xchnexkc7o.png" alt="The same sheet after a successful connect: &amp;quot;Connected · 21 actions&amp;quot; with a Tested sticker, and &amp;quot;House can use it now.&amp;quot;" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The token field clears after every Connect. Leaving it empty on a later try keeps the saved token (the sheet says &lt;strong&gt;Saved. Paste a new one to replace it.&lt;/strong&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Pick the Home team and meet House
&lt;/h2&gt;

&lt;p&gt;The new &lt;strong&gt;Home&lt;/strong&gt; team is the intended starting point. Open &lt;strong&gt;New team&lt;/strong&gt; in the sidebar (or the &lt;strong&gt;Get started&lt;/strong&gt; card) and look under &lt;strong&gt;From a template&lt;/strong&gt;. The Home tile shows &lt;strong&gt;1 bot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you don't have an AI account in this space yet, the sheet says &lt;strong&gt;Add an AI account first&lt;/strong&gt;. Add one, then come back to the template.&lt;/p&gt;

&lt;p&gt;Open the Home preview and press &lt;strong&gt;Create team · 1 bot&lt;/strong&gt;. The preview lists House, what the team needs, and what it will never do without you:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8fryt53o4f32joqse4xr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8fryt53o4f32joqse4xr.png" alt="The Home team preview: House (Home Helper) as lead, team handle @home, a required Connect Home Assistant need, an optional A local model need, Never without you, the Check the house skill, and Lights out off, every day at 22:00" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;House&lt;/strong&gt; (title: Home Helper), the only bot, with a house avatar and a &lt;strong&gt;Check the house&lt;/strong&gt; skill.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;Lights out&lt;/strong&gt; routine that starts off (see below).&lt;/li&gt;
&lt;li&gt;A required &lt;strong&gt;Connect Home Assistant&lt;/strong&gt; need, which opens the same two-field sheet, and an optional &lt;strong&gt;A local model&lt;/strong&gt; need. The template's own line says "A small Ollama model is enough." For House, a cloud or larger model is the safer pick; a 7B model is fine only for simple, specific asks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;House is instructed to look at the live state before it changes anything, to name each device and the result in plain words, and never to claim a change it didn't see succeed. It will not unlock a door, open a garage or gate, or turn off an alarm on its own — those always ask you. It also won't change automations, scenes, schedules or settings in Home Assistant.&lt;/p&gt;

&lt;p&gt;You can instead connect Home Assistant from Tools and turn the server on for any existing bot. The Home team is just the path that already has the skill, the routine and the right cautions.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Example asks
&lt;/h2&gt;

&lt;p&gt;Once House is connected and an account can run it, name the room or device. These are the asks that actually worked, with Ollama &lt;code&gt;qwen2.5-home7&lt;/code&gt; (qwen2.5 7B):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Turn off the living room lamp&lt;/strong&gt; — House called the turn-off tool with &lt;code&gt;{"name": "Living room lamp"}&lt;/code&gt;. In the default &lt;strong&gt;Ask only when risky&lt;/strong&gt; mode you get an approval card first; after you allow it, the reply was "The living room lamp has been turned off."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What is the living room temperature right now?&lt;/strong&gt; — House called &lt;code&gt;GetLiveContext&lt;/code&gt; and answered &lt;strong&gt;21.4 °C&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use the friendly names your house uses, not entity ids.&lt;/p&gt;

&lt;p&gt;Vague asks were unreliable on this 7B model, even though the Home team suggests them. &lt;strong&gt;Is anything left on?&lt;/strong&gt; called &lt;code&gt;GetLiveContext&lt;/code&gt; with domain &lt;code&gt;switch&lt;/code&gt; (the test house has lights, not switches) and got &lt;code&gt;{"error": "No exposed entities found in domain(s): switch"}&lt;/code&gt;, then replied as if nothing was on — while the kitchen and porch lights were on. Adding "Check the lights" still only looked at the living room and answered "The living room light is off." &lt;strong&gt;How warm is it inside?&lt;/strong&gt; queried a sensor named &lt;code&gt;temperature&lt;/code&gt;, missed &lt;strong&gt;Living room temperature&lt;/strong&gt;, and answered with outside weather for Berlin instead.&lt;/p&gt;

&lt;p&gt;If you want House to handle those broader questions, pin a cloud account or a larger local model.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Approvals, reads, and locks that always ask
&lt;/h2&gt;

&lt;p&gt;A new space starts on &lt;strong&gt;Ask only when risky&lt;/strong&gt;. In that mode:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Home Assistant's reads run without a card. That includes &lt;code&gt;GetLiveContext&lt;/code&gt; (and &lt;code&gt;GetDateTime&lt;/code&gt;) and any other tool the server marks read-only. Home Assistant marks some of those tools both read-only and destructive; since kwant 0.9.20, kwant trusts the read-only mark, as the MCP spec says.&lt;/li&gt;
&lt;li&gt;Turning a light or switch on or off still shows a card. The chat names the device, like &lt;strong&gt;House wants to turn off Living room lamp&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;Allow once&lt;/strong&gt;, or &lt;strong&gt;Always allow&lt;/strong&gt; that tool in this conversation (Always allow now lasts after a restart; take it back under the bot's &lt;strong&gt;Allowed without asking&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Locks, doors, garage doors, gates and alarms always ask&lt;/strong&gt;, in every approval mode, including &lt;strong&gt;Don't ask&lt;/strong&gt;. That card offers no &lt;strong&gt;Always allow&lt;/strong&gt;, and an earlier Always allow for the same tool (for example one you gave while turning off lights) does not cover them. The card's reason is: &lt;em&gt;Locks, doors, garage doors, gates and alarms always ask. Always allow isn't offered for them.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A server counts as Home Assistant by the name it reports when it connects (&lt;code&gt;home-assistant&lt;/code&gt;), never by its address. Reading the house never asks for this reason.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmc965emrlyooqmzjna26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fmc965emrlyooqmzjna26.png" alt="An approval card: &amp;quot;House wants to unlock Front door&amp;quot;, area Hallway, in Home Assistant, with the lock note and only Deny and Allow once" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Official 0.9.21 product shot from the &lt;a href="https://kwant.si/features#home-locks-always-ask" rel="noopener noreferrer"&gt;kwant.si features page&lt;/a&gt;. Our live test with &lt;code&gt;qwen2.5-home7&lt;/code&gt; never called the lock tool, so we didn't capture our own card — see the model notes below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you walk away, a card waits about five minutes. Nothing runs until you allow it. Since 0.9.21, a routine step nobody approved no longer counts as done: the run says a step was waiting for your approval and didn't run, and the reply's &lt;strong&gt;Details&lt;/strong&gt; name the step.&lt;/p&gt;

&lt;p&gt;Test approvals on a lamp you don't mind toggling before you point a bot at anything that matters. Then, with a cloud or larger model, try &lt;strong&gt;Unlock the front door&lt;/strong&gt; and confirm you get the always-ask card, with only &lt;strong&gt;Deny&lt;/strong&gt; and &lt;strong&gt;Allow once&lt;/strong&gt;. On 7B, House refused twice, then after an explicit confirmation replied "Unlocking the front door now." with &lt;strong&gt;no tool steps&lt;/strong&gt; — the door was not unlocked. Open &lt;strong&gt;Details&lt;/strong&gt; before you trust a claim like that.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. The Lights out routine
&lt;/h2&gt;

&lt;p&gt;The Home team comes with a &lt;strong&gt;Lights out&lt;/strong&gt; routine. It starts &lt;strong&gt;off&lt;/strong&gt; (the Routines page shows it paused). When you turn it on, it runs every day at 22:00 (&lt;code&gt;0 22 * * *&lt;/code&gt;) and tells House:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Turn off every light that's still on, then tell me which ones you turned off. Use the Check the house skill.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvvg4izrh6ta155nqmqum.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvvg4izrh6ta155nqmqum.png" alt="The Routines page, with Lights out paused, prompt visible, and the schedule &amp;quot;Custom rule 0 22 * * *&amp;quot;" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The skill reads the live state first, turns off only the lights that are on, leaves doors, locks and alarms alone, and never claims a light is off unless Home Assistant said so. On a 7B model, review the first runs carefully: a vague "what's still on?" step can miss lights, as above.&lt;/p&gt;

&lt;p&gt;Open the team's routines, review the prompt, then switch Lights out on when you're ready. Watch the first few runs. If a step waits for your approval and you aren't there, 0.9.21 records that it didn't run, instead of marking the routine done.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Models for House
&lt;/h2&gt;

&lt;p&gt;The Home team's &lt;strong&gt;A local model&lt;/strong&gt; need is optional; it opens Accounts so you can add Ollama (or another local endpoint). A local model keeps the house on your computer, but &lt;strong&gt;for House, prefer a cloud account or a larger local model&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What we actually saw with Ollama:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;qwen2.5-home7&lt;/strong&gt; (qwen2.5 7B) was fine when the ask named the room or device: it turned off the living room lamp and read the living-room temperature as 21.4 °C.&lt;/li&gt;
&lt;li&gt;The same 7B model was not reliable on vague asks, called &lt;code&gt;GetLiveContext&lt;/code&gt; with the wrong domain or name, and once claimed it was unlocking the front door without calling a tool.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;qwen2.5 3B&lt;/strong&gt; often sent invalid &lt;code&gt;HassTurnOn&lt;/code&gt; slots (empty domain, underscored entity ids, name "light") and once said the lamp was on after a step had already failed. Don't use 3B for this.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you do pin 7B, keep the prompts specific, and open &lt;strong&gt;Details&lt;/strong&gt; whenever the reply claims a change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Troubleshooting
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;What to try&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Home Assistant didn't accept this access token.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Make a new long-lived token in &lt;strong&gt;Profile → Security&lt;/strong&gt; and paste the whole thing. Leaving the field empty keeps a token that was already saved.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Home Assistant answered, but its MCP Server is off.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Add the &lt;strong&gt;Model Context Protocol&lt;/strong&gt; integration, then press &lt;strong&gt;Try again&lt;/strong&gt;. A 404 on &lt;code&gt;/api/mcp&lt;/code&gt; is how this usually shows up.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;kwant can't reach &amp;lt;host&amp;gt;.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Confirm the address opens Home Assistant in a browser from the same machine that runs kwant. From Docker or another computer, use an IP address, not &lt;code&gt;.local&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;This address answered, but it isn't Home Assistant.&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;You're not hitting HA's MCP server. Check the address; don't point it at a different app's &lt;code&gt;/api/mcp&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The server connects but House says it cannot find a device, or &lt;strong&gt;No entities matched&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Expose the entity on the Assist / Expose page. Use the friendly name, not the entity id.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;No exposed entities found in domain(s): switch&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The model asked for switches. Name a light (or the room) in the prompt, or pin a cloud / larger model.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;No exposed entities matched name 'temperature'&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The sensor's friendly name is more specific (ours is &lt;strong&gt;Living room temperature&lt;/strong&gt;). Name that, or use a larger model.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Received invalid slot info&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The model sent empty or underscored slots. Use a larger or cloud model, and ask it to pass only &lt;code&gt;"name"&lt;/code&gt; with the exact friendly name.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;Details · a step failed&lt;/strong&gt;, but the bot claims the light is on&lt;/td&gt;
&lt;td&gt;Open &lt;strong&gt;Details&lt;/strong&gt; and read the tool result. A failed first call can sit next to a later successful one, or next to a guess. Don't trust the prose alone. Since 0.9.21, Details name which step failed or didn't run.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The bot says it is unlocking or turning something off, but Details shows no steps&lt;/td&gt;
&lt;td&gt;Nothing ran. The 7B House bot did this after a confirmation to unlock the front door. Don't trust the claim; pin a cloud or larger model.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A lock, door, garage, gate or alarm ran without a card&lt;/td&gt;
&lt;td&gt;It shouldn't, in any approval mode, if a tool actually ran. Check &lt;strong&gt;Details&lt;/strong&gt;. A sentence with no tool call is a model claim, not a completed action, and is not this always-ask card.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A routine runs but does not turn lights off&lt;/td&gt;
&lt;td&gt;Read the routine reply and &lt;strong&gt;Details&lt;/strong&gt;. If it says a step was waiting for your approval, allow the card or tighten the prompt. Confirm the lights are exposed.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;No account can run bots here yet&lt;/strong&gt; / &lt;strong&gt;Add an AI account first&lt;/strong&gt;
&lt;/td&gt;
&lt;td&gt;Add or re-enable Ollama (or another account) under &lt;strong&gt;Settings → This space → Accounts&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;The model request failed: connection failed&lt;/strong&gt; or &lt;code&gt;model '…' not found&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Ollama is down, or the pinned model name no longer exists. Check &lt;code&gt;ollama list&lt;/code&gt; and the account in that space.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Advanced: adding Home Assistant as a generic MCP server
&lt;/h2&gt;

&lt;p&gt;You shouldn't need this in 0.9.21. The Connect sheet is the supported path: it builds the &lt;code&gt;/api/mcp&lt;/code&gt; URL and the &lt;code&gt;Authorization: Bearer &amp;lt;token&amp;gt;&lt;/code&gt; header, and it checks that the server really is Home Assistant.&lt;/p&gt;

&lt;p&gt;If you do add a server by hand under Tools, use the same URL and a header &lt;code&gt;Authorization&lt;/code&gt; = &lt;code&gt;Bearer &amp;lt;token&amp;gt;&lt;/code&gt;, then &lt;strong&gt;Test connection&lt;/strong&gt;. Don't use OAuth against a plain-http LAN address such as &lt;code&gt;http://homeassistant.local:8123/api/mcp&lt;/code&gt;. kwant only allows http OAuth on loopback (&lt;code&gt;localhost&lt;/code&gt; / &lt;code&gt;127.0.0.1&lt;/code&gt;). On a &lt;code&gt;.local&lt;/code&gt; or &lt;code&gt;.lan&lt;/code&gt; hostname, sign-in fails with a message that the server's sign-in must use https, and that for a server on your home network you should add the Bearer header instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disclaimer
&lt;/h2&gt;

&lt;p&gt;I am the author of kwant. The Home Assistant MCP server is Home Assistant's official integration; this guide only shows how to connect it to a self-hosted kwant bot, including on local models. Test approvals on a lamp you don't mind toggling before you point a bot at anything that matters.&lt;/p&gt;




&lt;p&gt;Repo: &lt;a href="https://github.com/alexsergivan/kwant" rel="noopener noreferrer"&gt;https://github.com/alexsergivan/kwant&lt;/a&gt; · Site: &lt;a href="https://kwant.si" rel="noopener noreferrer"&gt;https://kwant.si&lt;/a&gt; · MCP docs: &lt;a href="https://kwant.si/docs/mcp" rel="noopener noreferrer"&gt;https://kwant.si/docs/mcp&lt;/a&gt;&lt;/p&gt;

</description>
      <category>homeassistant</category>
      <category>selfhosted</category>
      <category>ai</category>
      <category>opensource</category>
    </item>
    <item>
      <title>I hit Grok Bot's usage wall two mornings in a row, so I moved the routine onto a self-hosted team</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Sun, 15 Nov 2026 22:06:27 +0000</pubDate>
      <link>https://dev.to/ale_ukr/i-hit-grok-bots-usage-wall-two-mornings-in-a-row-so-i-moved-the-routine-onto-a-self-hosted-team-52jg</link>
      <guid>https://dev.to/ale_ukr/i-hit-grok-bots-usage-wall-two-mornings-in-a-row-so-i-moved-the-routine-onto-a-self-hosted-team-52jg</guid>
      <description>&lt;p&gt;Thursday 13 November, about ten in the morning in Munich. I sat down with coffee, expecting the daily marketing round to already be waiting: what moved on the repo, which listing pull requests were still open, whether anything was worth a reply. Instead I got a shrug.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;no Grok Bot usage available&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Friday the 14th, same time, same shrug. The work hadn't gone anywhere. The meter had.&lt;/p&gt;

&lt;p&gt;I like Grok Bot. A team of bots you can message, plus routines that run while you sleep, is a nice way to work. Claude Duet and OpenAI Dots live in the same neighborhood. The trouble starts when a hosted usage limit runs out at the exact hour you wanted the routine to run. Two mornings in a row isn't a cute quirk. It's a wall.&lt;/p&gt;

&lt;p&gt;So I moved the round onto a team that runs on my own machine.&lt;/p&gt;

&lt;p&gt;Full disclosure, because hiding this would be a weird way to write the rest: I got annoyed enough at that wall that I built the thing I switched to. It's called &lt;a href="https://kwant.si" rel="noopener noreferrer"&gt;kwant&lt;/a&gt;. It's open source under AGPL-3.0, one Rust binary, and it runs as a Mac app, on Linux, on a VPS, or as a Windows beta. I point it at local models (Ollama is fine) or at API keys and CLIs I already pay for. There's no quota that belongs to someone else's plan. If one account runs out, the turn can move to the next.&lt;/p&gt;

&lt;p&gt;The part I didn't expect to care about this much is spaces. I keep a work space on company AI accounts, and a private one on local models for anything that shouldn't wander. They don't leak into each other. Switching is a click, or a shortcut, and the bots, routines and approval rules stay where I put them.&lt;/p&gt;

&lt;p&gt;The marketing round lives in the work space. I didn't write a five-page runbook for it. I gave the team one line, on a daily schedule at 10:00 Europe/Berlin:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Run the morning marketing round for kwant. Check what changed since yesterday, draft anything I might want to post or open, and stop before anything leaves this machine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's the whole prompt. "The morning marketing round" is deliberately vague. The bots have to work out what I actually want.&lt;/p&gt;

&lt;p&gt;You can even set the schedule that way. Ask in a sentence, and a bot will create the routine, show you the time and the channel, and leave an Undo on the card. Nothing is locked in until you're happy with it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb1s6xpex8dfgtiuq28np.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fb1s6xpex8dfgtiuq28np.png" alt="A kwant chat where someone asks, in one sentence, for a morning check, and the QA bot creates a weekday routine with an Undo button on the card." width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A one-line request becoming a scheduled routine, with Undo, Edit and Pause on the card. This is kwant's product UI, in the Trailhead sample space from the docs, not a screenshot of my marketing run.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What the bots actually do with that one line
&lt;/h2&gt;

&lt;p&gt;A typical run looks like this.&lt;/p&gt;

&lt;p&gt;The scout treats "what changed" as a real question, not a vibe. It checks the GitHub repo: stars and forks, which listing pull requests are still waiting, whether anything merged overnight, whether a new list or a thread is worth a reply. Then it @mentions the writer with a short brief, instead of trying to draft everything itself.&lt;/p&gt;

&lt;p&gt;That @mention is the whole handoff. The writer sees the brief in the same channel, does the drafting, and the work shows up as its own card. When the writer finishes, the bot that handed the work out reviews the result. A pass can verify the card. A fail sends it back with the reason. The teammate who did the work still can't approve its own card.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxgi1o2bwda8vpng58y4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxgi1o2bwda8vpng58y4z.png" alt="Designer answers a question about an empty state, then @mentions QA, and QA checks the layout on a 360 pixel screen." width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bots handing work to each other with an @mention, then checking it. Again, the Trailhead sample space from the docs, so you can see the shape of a handoff.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The writer comes back with draft replies, and, if there's a list worth joining, a draft pull request blurb. Nothing is posted. Nothing is opened. The last clause in the prompt does a lot of work here: &lt;em&gt;stop before anything leaves this machine.&lt;/em&gt; In kwant, a risky action waits on an approval card anyway: the exact command or tool call, with Allow once, Always allow, or Deny. Only I can press those buttons. A bot can't approve its own move, and a bot calling the API can't either.&lt;/p&gt;

&lt;p&gt;That's the adult in the room I wanted. The hosted routine failing was annoying. A bot opening a pull request because it felt helpful would have been worse.&lt;/p&gt;

&lt;h2&gt;
  
  
  "Done" is a check, not a vibe
&lt;/h2&gt;

&lt;p&gt;Meanwhile the run shows up as a card on the Mission board. A task isn't done because a bot said "done." It becomes Verified only when its checks pass, and a bot can't verify its own work. For this routine I attached a boring, visible set:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a file check: the morning notes file exists and contains today's date&lt;/li&gt;
&lt;li&gt;a URL check: the GitHub repo still answers&lt;/li&gt;
&lt;li&gt;a review by a teammate who isn't the owner of the task, looking at the drafts&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the writer owns the task, another bot has to do the review. If I want the last word, I press Verify myself. Optional checks can sit on the card without holding it back, but the ones above all count.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faoluricqd8ytz0nq857d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Faoluricqd8ytz0nq857d.png" alt="A Mission board task with an evidence receipt: a file-contains check has passed, and the card is Verified." width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What Verified looks like: a receipt with the check, the result, and the evidence. This one is a sample "file contains" check from the docs, the same kind I use for the morning notes.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When that's green, I get a short brief and a pile of drafts with my coffee. I still decide what goes out. The difference is I'm no longer the person who has to notice that the hosted routine never started.&lt;/p&gt;

&lt;p&gt;If I need the bots to look at GitHub or a mailbox, I hook the account up in that space, or use the built-in Composio connector. The important part isn't the connector. It's that the routine still runs when a hosted plan would have shrugged, and that nothing noisy happens until I say so.&lt;/p&gt;

&lt;p&gt;I still use hosted tools for plenty of things. I just don't want a daily routine to depend on a meter I can't refill at ten on a Thursday.&lt;/p&gt;

&lt;p&gt;If you want to poke at it, the code is at &lt;a href="https://github.com/alexsergivan/kwant" rel="noopener noreferrer"&gt;github.com/alexsergivan/kwant&lt;/a&gt;. It's a little over 600 GitHub stars as of 15 November 2026, and a star helps other people find it. The site and docs are at &lt;a href="https://kwant.si" rel="noopener noreferrer"&gt;kwant.si&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>selfhosted</category>
      <category>opensource</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Still Typing URLs? Level Up Your Workflow with a Modern QR Code Generator! (And a Hot Tip 😉)</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Fri, 09 May 2025 10:31:08 +0000</pubDate>
      <link>https://dev.to/ale_ukr/still-typing-urls-level-up-your-workflow-with-a-modern-qr-code-generator-and-a-hot-tip--5bcf</link>
      <guid>https://dev.to/ale_ukr/still-typing-urls-level-up-your-workflow-with-a-modern-qr-code-generator-and-a-hot-tip--5bcf</guid>
      <description>&lt;p&gt;Hey Devs!&lt;/p&gt;

&lt;p&gt;In a world of instant everything, QR codes are still one of the slickest ways to bridge the physical and digital. Whether you're sharing your latest project link, a Wi-Fi password, contact details, or even pointing users to app downloads, a good QR code generator is an essential tool in your arsenal.&lt;/p&gt;

&lt;p&gt;But with so many options out there, how do you pick one that doesn't just spit out a pixelated square, but actually works &lt;em&gt;for&lt;/em&gt; you?&lt;/p&gt;

&lt;h2&gt;
  
  
  What Makes a QR Code Generator Truly Useful in 2025?
&lt;/h2&gt;

&lt;p&gt;Beyond just encoding a URL, a top-notch QR code generator should offer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Ease of Use:&lt;/strong&gt; Nobody wants to fight with a clunky interface. A clean, intuitive experience is key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customization:&lt;/strong&gt; The ability to add logos, change colors, and even play with shapes can make your QR codes more engaging and on-brand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Variety of Data Types:&lt;/strong&gt; From simple text and URLs to vCards, Wi-Fi access, calendar events, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic QR Codes:&lt;/strong&gt; This is a big one! The ability to change the destination URL &lt;em&gt;after&lt;/em&gt; the QR code has been generated and printed is a lifesaver. No more reprinting everything for a tiny link change!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics:&lt;/strong&gt; Knowing how many times your QR code is scanned, and where, can be incredibly valuable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reliability &amp;amp; Speed:&lt;/strong&gt; It should just &lt;em&gt;work&lt;/em&gt;, every time, and generate codes quickly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Friendliness (Bonus!):&lt;/strong&gt; For us devs, an API for programmatic QR code generation can be a massive plus for integrating into our own apps and workflows.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cut Through the Noise: Give QRCodia.com a Spin!
&lt;/h2&gt;

&lt;p&gt;Now, I know what you're thinking: "Another tool?" But hear me out. I've been on the lookout for a QR code solution that ticks all the right boxes without unnecessary complexity, and &lt;strong&gt;&lt;a href="https://qrcodia.com" rel="noopener noreferrer"&gt;https://qrcodia.com&lt;/a&gt;&lt;/strong&gt; has really caught my attention as a fantastic contender that you should definitely check out.&lt;/p&gt;

&lt;p&gt;While many generators offer a piece of the puzzle, QRCodia.com appears designed with a focus on providing a smooth and efficient experience for creating the QR codes you need, quickly. If you're looking for a platform that potentially offers a great balance of powerful features (likecustomization options we talked about) with a user-friendly approach, QRCodia.com is shaping up to be a real winner.&lt;/p&gt;

&lt;p&gt;Think about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Need to quickly generate a QR for your GitHub repo at a hackathon?&lt;/li&gt;
&lt;li&gt;Want to create a custom-branded QR for your freelance business card?&lt;/li&gt;
&lt;li&gt;Looking to share a link to your latest deployed app for easy mobile testing?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;QRCodia.com is built for exactly these kinds of scenarios. While I'm still exploring all its nooks and crannies, the initial impression is that it aims to strip away the friction often found in other tools, letting you get to that useful QR code faster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why might QRCodia.com be &lt;em&gt;your&lt;/em&gt; winner?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Focus on Simplicity &amp;amp; Power:&lt;/strong&gt; It looks to provide the robust features you need without a steep learning curve.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modern Interface:&lt;/strong&gt; A clean and intuitive UI is always a breath of fresh air.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versatility:&lt;/strong&gt; Likely supports a good range of QR code types for various applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best way to know if a tool is right for you is to try it. So, if you're in the market for a QR code generator that could simplify your life and boost your projects, I strongly suggest heading over to &lt;strong&gt;&lt;a href="https://qrcodia.com" rel="noopener noreferrer"&gt;https://qrcodia.com&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Give it a try, and I'd love to hear your thoughts! What are your go-to QR code tools? What features do you find indispensable? Drop a comment below!&lt;/p&gt;

&lt;p&gt;Happy coding (and QR-ing)!&lt;/p&gt;

</description>
      <category>qrcode</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Go middleware example. How to alter a HTTP handler result?</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Thu, 24 Oct 2019 13:39:29 +0000</pubDate>
      <link>https://dev.to/ale_ukr/go-middleware-example-how-to-alter-a-handler-result-499c</link>
      <guid>https://dev.to/ale_ukr/go-middleware-example-how-to-alter-a-handler-result-499c</guid>
      <description>&lt;p&gt;Let's imagine a situation when you want to alter a result, returned by some http handler to the client. Fortunately, Golang provides an easy mechanism for that, called a middleware.&lt;/p&gt;

&lt;p&gt;I will dive directly into the source code, to save your time.&lt;/p&gt;

&lt;p&gt;Imagine, we have this simple webserver (here I'm using a chi router):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="s2"&gt;"bytes""github.com/go-chi/chi""io""log""net/http"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myFirstHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

   &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":3000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myFirstHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"This is a main page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we run this application and visit a frontpage htttp://localhost:3000/, you can see this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1kp8e3t3vo68htv8mn8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1kp8e3t3vo68htv8mn8p.png" alt="golang golang" width="650" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now we got a new requirement to create another handler which should get all response data from myFirstHandler and add some modification on top.&lt;/p&gt;

&lt;p&gt;We can do it easily in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Adds a new router handler with a middleware myMiddleware.  &lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;With&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myMiddleware&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/other"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;myFirstHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be able to read a response from another handler, we have to implement our own ResponseWriter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="nc"&gt;MyResponseWriter&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;
   &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Here we are implementing a Write() function from ResponseWriter with our custom instructions. &lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myrw&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;MyResponseWriter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;myrw&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, let's write our middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;myMiddleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HandlerFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Create a response writer:&lt;/span&gt;
      &lt;span class="n"&gt;myResponseWriter&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;MyResponseWriter&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;            &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Buffer&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="c1"&gt;// Here we are pssing our custom response writer to the next http handler.&lt;/span&gt;
      &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="c1"&gt;// Here we are adding our custom stuff to the response, which we received after http handler execution. &lt;/span&gt;
      &lt;span class="n"&gt;myResponseWriter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WriteString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;" and some additional modifications"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

      &lt;span class="c1"&gt;// And, finally, we are copiing everything back to the original response writer.  if _, err := io.Copy(w, myResponseWriter.buf); err != nil {&lt;/span&gt;
         &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Failed to send out response: %v"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we run our server again and go to /other path, we will see this:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhw6qwbl5ll17bs80puvh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhw6qwbl5ll17bs80puvh.png" alt="golang golang" width="800" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was a silly example, which will never happen in real life, but, I hope you got an overview of how you can play with HTTP handlers and middlewares.&lt;/p&gt;

&lt;p&gt;The source code you can find in this repository: &lt;a href="https://github.com/alexsergivan/blog-examples/tree/master/middleware" rel="noopener noreferrer"&gt;https://github.com/alexsergivan/blog-examples/tree/master/middleware&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>middleware</category>
      <category>handler</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to make Nginx cache cookie aware?</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Tue, 18 Jun 2019 14:16:20 +0000</pubDate>
      <link>https://dev.to/ale_ukr/how-to-make-nginx-cache-cookie-aware-2ffl</link>
      <guid>https://dev.to/ale_ukr/how-to-make-nginx-cache-cookie-aware-2ffl</guid>
      <description>&lt;p&gt;In this post, I'm going to describe how we can configure nginx to be able to cache responses based on the specific cookie value. &lt;/p&gt;

&lt;p&gt;Let's imagine a situation when you want to do an A/B test on your website, where 50% of users should see new headline text on the page. Other 50% of visitors will continue see the old page. In this case, all your server-side manipulation about splitting users to different versions will be ignored by nginx cache (of course, if you have it).&lt;/p&gt;

&lt;p&gt;It happens because nginx, by default, has this configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;proxy_cache_key $scheme$proxy_host$request_uri;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, it will use the same cache key for all users, who requests page with the same url.&lt;/p&gt;

&lt;p&gt;Luckily, nginx allows us easily to customize proxy_cache_key! What we need to do, it's just to add a specific cookie to this key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;proxy_cache_key $scheme$proxy_host$request_uri$cookie_MY_COOKIE_NAME;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! After this, if userA has MY_COOKIE_NAME=A and userB has MY_COOKIE_NAME=B they will receive different versions of page by the same url.&lt;/p&gt;

&lt;p&gt;If you need more complex behaviour, where you won't use hardcoded cookie name in your nginx config, you can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if ($http_cookie ~* "ab_(.*?)=([\w-]+)" ) {
  set $abcookie $1$2;
}

proxy_cache_key $scheme$proxy_host$request_uri$abcookie;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, you can use $http_cookie and generate proxy_cache_key based on specific cookie patterns. In this concrete example we check if http cookies contains cookie with regex pattern "ab_(.*?)=([\w-]+)" and if this cookie exists, we generate new variable for proxy_cache_key.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nginx</category>
      <category>cookie</category>
      <category>cache</category>
    </item>
    <item>
      <title>How to control router access permissions in Golang web application</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Fri, 17 May 2019 07:38:51 +0000</pubDate>
      <link>https://dev.to/ale_ukr/how-to-control-router-access-permissions-in-golang-web-application-1ofo</link>
      <guid>https://dev.to/ale_ukr/how-to-control-router-access-permissions-in-golang-web-application-1ofo</guid>
      <description>&lt;p&gt;In this post I'm going to describe how can we limit user access to the specific url in golang web application. I will use &lt;a href="https://github.com/go-chi/chi" rel="noopener noreferrer"&gt;chi&lt;/a&gt; router - a lightweight, idiomatic and composable router for building Go HTTP services.&lt;/p&gt;

&lt;p&gt;Let's create our main package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"net/http"&lt;/span&gt;
    &lt;span class="s2"&gt;"github.com/go-chi/chi"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;homePageHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/admin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;adminPageHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ListenAndServe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;":3000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;homePageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"This is home page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;adminPageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"This is admin page"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, if we go to the &lt;code&gt;/admin&lt;/code&gt; page, we will see "This is admin page".&lt;/p&gt;

&lt;p&gt;Now, let's make this path accessible only for admin.&lt;/p&gt;

&lt;p&gt;We have to replace&lt;/p&gt;

&lt;p&gt;&lt;code&gt;r.Get("/admin", adminPageHandler)&lt;/code&gt;&lt;br&gt;
With&lt;br&gt;
&lt;code&gt;r.Mount("/admin", adminRouter())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Mount attaches another http.Handler or chi Router as a subrouter along a routing path.&lt;/p&gt;

&lt;p&gt;Then, we have to attach middleware inside adminRouter() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;adminRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;chi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// Middleware with access rules for router.&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AdminOnly&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;adminPageHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this middleware we have a simple check is user authorized to access this page or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;AdminOnly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;HandlerFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// If user is admin, allows access.&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nc"&gt;IsLoggedInAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ServeHTTP&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Otherwise, 403.&lt;/span&gt;
            &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StatusText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;403&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In sake of demonstration, I'm going just to use a random bool function to decide is used admin or not. You can modify this function according to your user authentication model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;IsLoggedInAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Float32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. Looks really simple, Isn't it?&lt;/p&gt;

&lt;p&gt;Let's go to to the &lt;code&gt;/admin&lt;/code&gt; page again.&lt;/p&gt;

&lt;p&gt;As you see, now, sometimes (depends on our random decider), user has no access to this page anymore.&lt;/p&gt;

&lt;p&gt;You can find source code &lt;a href="https://github.com/alexsergivan/blog-examples/blob/master/route-auth/main.go" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>webdev</category>
      <category>router</category>
    </item>
    <item>
      <title>How to test database interactions in golang applications</title>
      <dc:creator>Oleks</dc:creator>
      <pubDate>Thu, 09 May 2019 08:12:33 +0000</pubDate>
      <link>https://dev.to/ale_ukr/how-to-test-database-interactions-in-golang-applications-3041</link>
      <guid>https://dev.to/ale_ukr/how-to-test-database-interactions-in-golang-applications-3041</guid>
      <description>&lt;p&gt;Testing of functions with database interactions always was challenging. Recently, I found a wonderful library &lt;a href="https://github.com/DATA-DOG/go-sqlmock" rel="noopener noreferrer"&gt;go-sqlmock&lt;/a&gt; which will simplify writing tests and mocking database queries in golang applications a lot. And I want to share a short example of how to work with it.&lt;/p&gt;

&lt;p&gt;First, we have to install it&lt;/p&gt;

&lt;p&gt;&lt;code&gt;go get github.com/DATA-DOG/go-sqlmock&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We have this function with SQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;MenuByNameAndLanguage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sql&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;langcode&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Menu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT id, langcode, title, link__uri, view_sidemenu FROM menu_link_content_data WHERE menu_name=? AND langcode=?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;langcode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;menuLinks&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MenuLink&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="n"&gt;menuLink&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MenuLink&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
         &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Langcode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SideMenu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="n"&gt;menuLinks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menuLinks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;menuLink&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;menu&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Menu&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="nc"&gt;Links&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;menuLinks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function just getting menu links by menu name and language. &lt;/p&gt;

&lt;p&gt;And now let's test it.&lt;/p&gt;

&lt;p&gt;We are going to test that MenuByNameAndLanguage function will return correct Menu structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;menu&lt;/span&gt;


&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"context"&lt;/span&gt;
    &lt;span class="s2"&gt;"testing"&lt;/span&gt;


    &lt;span class="s2"&gt;"github.com/DATA-DOG/go-sqlmock"&lt;/span&gt;
    &lt;span class="s2"&gt;"github.com/stretchr/testify/assert"&lt;/span&gt;
    &lt;span class="s2"&gt;"gitlab.mfb.io/user/graphql_server/models"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nc"&gt;TestShouldReturnCorrectMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;testing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Creates sqlmock database connection and a mock to manage expectations.&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sqlmock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fatalf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"an error '%s' was not expected when opening a stub database connection"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// Closes the database and prevents new queries from starting.&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Here we are creating rows in our mocked database.&lt;/span&gt;
    &lt;span class="n"&gt;rows&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sqlmock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NewRows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"langcode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"link__uri"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"view_sidemenu"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="nc"&gt;AddRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"enTitle"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/en-link"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="nc"&gt;AddRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"enTitle2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"/en-link2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// This is most important part in our test. Here, literally, we are altering SQL query from MenuByNameAndLanguage&lt;/span&gt;
﻿    &lt;span class="c1"&gt;// function and replacing result with our expected result. &lt;/span&gt;
    &lt;span class="n"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ExpectQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"^SELECT (.+) FROM menu_link_content_data*"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="nc"&gt;WithArgs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
        &lt;span class="nc"&gt;WillReturnRows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


    &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;TODO&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Calls MenuByNameAndLanguage with mocked database connection in arguments list. &lt;/span&gt;
    &lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nc"&gt;MenuByNameAndLanguage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Here we just construction our expecting result.&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;menuLinks&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MenuLink&lt;/span&gt;
    &lt;span class="n"&gt;menuLink1&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MenuLink&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;       &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="s2"&gt;"enTitle"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Langcode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="s2"&gt;"/en-link"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;SideMenu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;menuLinks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menuLinks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;menuLink1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;menuLink2&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MenuLink&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="no"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;       &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="s2"&gt;"enTitle2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Langcode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"en"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;      &lt;span class="s2"&gt;"/en-link2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;SideMenu&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;menuLinks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;menuLinks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;menuLink2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;expectedMenu&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Menu&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="s2"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;Links&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;menuLinks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// And, finally, let's check if result from MenuByNameAndLanguage equal with expected result.// Here I used Testify library (https://github.com/stretchr/testify).&lt;/span&gt;
    &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expectedMenu&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see everything in this example was pretty straightforward.&lt;/p&gt;

&lt;p&gt;For mo details, you can refer to &lt;a href="https://godoc.org/github.com/DATA-DOG/go-sqlmock" rel="noopener noreferrer"&gt;GoDocs&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>go</category>
      <category>testing</category>
      <category>sql</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
