<?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: Mikiya Ichino</title>
    <description>The latest articles on DEV Community by Mikiya Ichino (@michinoins).</description>
    <link>https://dev.to/michinoins</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1077559%2Fed777722-77f3-4bf1-8a07-47bef78b330f.jpeg</url>
      <title>DEV Community: Mikiya Ichino</title>
      <link>https://dev.to/michinoins</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michinoins"/>
    <language>en</language>
    <item>
      <title>Single-Threaded vs Multi-Threaded Servers: An Experiment with Node.js and Java</title>
      <dc:creator>Mikiya Ichino</dc:creator>
      <pubDate>Thu, 18 May 2023 02:14:54 +0000</pubDate>
      <link>https://dev.to/michinoins/single-threaded-vs-multi-threaded-servers-an-experiment-with-nodejs-and-java-3183</link>
      <guid>https://dev.to/michinoins/single-threaded-vs-multi-threaded-servers-an-experiment-with-nodejs-and-java-3183</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a fullstack developer who started my career with Java and SpringBoot, I've become familiar with how Apache servers handle multiple requests using a multi-threading mechanism. &lt;/p&gt;

&lt;p&gt;Each request received by the server initiates a new process, allowing simultaneous handling of multiple requests. &lt;br&gt;
However, this traditional approach led to a known issue called the C10K problem, a constraint that makes it challenging for servers to efficiently manage ten thousand concurrent connections.&lt;/p&gt;

&lt;p&gt;In contrast, event-driven, single-threaded servers like Node.js have introduced a new way of handling high concurrent connections more effectively. In this article, I aim to compare the behavior of single-threaded (Node.js) and multi-threaded (Apache) servers when dealing with CPU-intensive requests under high load conditions.&lt;/p&gt;
&lt;h2&gt;
  
  
  Theoretical Expectations
&lt;/h2&gt;

&lt;p&gt;Predicting the outcome, I anticipate that when a server receives a CPU-intensive request that takes a considerable amount of time to process, the single-threaded server will struggle more than a multi-threaded server. A multi-threaded server can process multiple CPU-intensive tasks concurrently, whereas a single-threaded server can only handle one task at a time.&lt;/p&gt;
&lt;h2&gt;
  
  
  Single-Threaded vs Multi-Threaded
&lt;/h2&gt;

&lt;p&gt;Before we dive into the experiment, let's briefly distinguish between single-threaded and multi-threaded servers.&lt;/p&gt;

&lt;p&gt;Single-threaded servers like Node.js operate on a single thread, executing one operation at a time. Node.js, being non-blocking and event-driven, can handle many concurrent connections with minimal overhead.&lt;/p&gt;

&lt;p&gt;Conversely, multi-threaded servers like Java can execute multiple operations concurrently by initiating additional threads. This ability makes them more suitable for CPU-intensive tasks. However, the overhead associated with creating and managing threads can reduce efficiency when dealing with a large number of concurrent connections.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Experiment
&lt;/h2&gt;

&lt;p&gt;To illustrate the difference in performance between Node.js and Apache servers under high load, I've created simple servers using both languages. &lt;br&gt;
These servers feature a single endpoint that calculates the 40th Fibonacci number, a CPU-intensive task.&lt;br&gt;
We will use a load testing tool to simulate a high number of concurrent requests.&lt;/p&gt;
&lt;h2&gt;
  
  
  High Load Testing
&lt;/h2&gt;

&lt;p&gt;High load testing is a form of performance testing that exposes a server or system to a workload nearing or exceeding its limit. It helps identify potential bottlenecks or performance issues under high stress conditions.&lt;/p&gt;

&lt;p&gt;In our experiment, we'll use a benchmarking tool called Autocannon, capable of generating a high load by making numerous HTTP requests to a server.&lt;/p&gt;
&lt;h2&gt;
  
  
  The tools
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;autocannon&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Autocannon is a Node.js-based benchmarking tool, supporting HTTP pipelining, HTTPS, and providing detailed performance statistics. Here's how to install Autocannon using npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g autocannon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To simulate a high load, we'll run Autocannon as follows:&lt;/p&gt;

&lt;p&gt;This command  means "send as many requests as possible to localhost:8080/ for 10 seconds, using 50 concurrent connections"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autocannon -c 50 -d 10 http://localhost:3000/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The number of request is depending on several factors , capacity of the network,response time of the server ,etc ... &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node.js Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here's the code for our Node.js server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fibonacci.js 

const http = require('http');
const os = require('os');

function fibonacci(n) {
  if (n &amp;lt; 2)
    return 1;
  else
    return fibonacci(n-2) + fibonacci(n-1);
}

const server = http.createServer((req, res) =&amp;gt; {
  const fibNumber = fibonacci(40); // This will take some time
  res.end(`Fibonacci result: ${fibNumber}`);
});

server.listen(3000, () =&amp;gt; {
  console.log('Server listening on port 3000');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this script, we create an Express server with a single route /fibonacci that calculates the 40th Fibonacci number.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;run the server&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node fibonacci.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;execute this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autocannon -c 50 -d 10 http://localhost:3000/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.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%2F88yjswfey69yojyq0fjl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F88yjswfey69yojyq0fjl.png" alt="nodeResult" width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is &lt;br&gt;
&lt;strong&gt;Sent 100 requests , but it causes 41 timeouts.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Apache Server&lt;/strong&gt;&lt;br&gt;
FibonacciController.java&lt;/p&gt;

&lt;p&gt;Here's the corresponding Java code using Spring Boot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FibonacciController {
    @GetMapping("/")
    public String calculateFibonacci() {
        int fibNumber = fibonacci(40);
        return "Fibonacci result: " + fibNumber;
    }

    private int fibonacci(int n) {
        if (n &amp;lt;= 1) return n;
        else return fibonacci(n-1) + fibonacci(n-2);
    }
}

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

&lt;/div&gt;



&lt;p&gt;run the server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./gradlew bootrun
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;execute this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autocannon -c 50 -d 10 http://localhost:8080/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.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%2F1od1u9ypd342jbfcmbtp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1od1u9ypd342jbfcmbtp.png" alt="JavaResult" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is &lt;br&gt;
&lt;strong&gt;Sent 162 requests , no timeouts.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Result
&lt;/h2&gt;

&lt;p&gt;Here is the result of conducting this command for each server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;autocannon -c 50 -d 10 http://localhost:8080/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result(Node) is &lt;br&gt;
&lt;strong&gt;100 requests , but it causes 41 timeouts.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The result(Apache) is &lt;br&gt;
&lt;strong&gt;162 requests , no timeouts.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From these result, we can draw conclusions in terms of 3 aspects.&lt;/p&gt;

&lt;p&gt;Concurrency Handling: The single-threaded Node.js server struggled to handle the concurrent requests as efficiently as the multi-threaded Apache server. This is indicated by the 41 timeouts out of 100 requests, meaning 41% of the requests were not fulfilled within the given timeframe. This is likely due to the blocking nature of the CPU-intensive Fibonacci calculation, which prevents Node.js from efficiently handling other incoming requests concurrently.&lt;/p&gt;

&lt;p&gt;Throughput: The multi-threaded Apache server was able to handle a higher number of requests (162 requests) within the same timeframe, and without any timeouts. This demonstrates that for CPU-intensive tasks, a multi-threaded server like Java can potentially have higher throughput compared to a single-threaded server like Node.js.&lt;/p&gt;

&lt;p&gt;Resiliency: The Apache server appeared to be more resilient under high load, given that it didn't experience any timeouts. This suggests that for applications where high resiliency under load is important (especially for CPU-bound tasks), a multi-threaded server might be a more appropriate choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This experiment provides a clear illustration of how single-threaded and multi-threaded servers handle high load differently. However, it's important to note that neither model is universally better than the other. Node.js, with its event-driven, non-blocking model, excels at handling many concurrent connections, making it a great choice for real-time applications. Java, with its multi-threading capabilities, is often better suited for CPU-intensive tasks.&lt;/p&gt;

&lt;p&gt;When choosing a technology stack for a server application, consider the specific requirements of our project, the expertise of your development team, and the strengths and weaknesses of the available technologies.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building a CRUD App with Clean Architecture in Go</title>
      <dc:creator>Mikiya Ichino</dc:creator>
      <pubDate>Sat, 06 May 2023 04:47:11 +0000</pubDate>
      <link>https://dev.to/michinoins/building-a-crud-app-with-mysql-gorm-echo-and-clean-architecture-in-go-h6d</link>
      <guid>https://dev.to/michinoins/building-a-crud-app-with-mysql-gorm-echo-and-clean-architecture-in-go-h6d</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;In this article, I will create an API with CRUD (Create, Read, Update, Delete) functionality using Clean Architecture. I will use MySQL as our database, Echo as our framework, and GORM as our ORM.&lt;/p&gt;

&lt;h4&gt;
  
  
  What I Will Build
&lt;/h4&gt;

&lt;p&gt;I will create an API with Create, Read, (Update), and Delete functionality. The Update function is not implemented yet, so feel free to add it yourself!&lt;/p&gt;

&lt;h4&gt;
  
  
  Target Audience
&lt;/h4&gt;

&lt;p&gt;People who have set up a Go environment and want to create a simple API.&lt;/p&gt;

&lt;h4&gt;
  
  
  Technologies Used
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.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%2F6rtq5i34a752de2t3f96.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6rtq5i34a752de2t3f96.png" alt="Table of tech stack and type" width="167" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Clean Architecture?
&lt;/h1&gt;

&lt;p&gt;Clean Architecture is well known for the following diagram:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fg9xxd161q1s5qkdoo6pl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fg9xxd161q1s5qkdoo6pl.png" alt="Diagram of Clean Architecture" width="769" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The purpose of Clean Architecture is the separation of concerns, which is achieved by paying attention to the dependencies between layers. This separation leads to improved code readability and a more robust design. For more information on the benefits and details of Clean Architecture, please refer to the reference articles.&lt;br&gt;
In the above diagram, arrows point from the outer layers to the inner layers, indicating the direction of dependencies. Dependencies are allowed from the outer layers to the inner layers, but not vice versa.&lt;br&gt;
In other words, you can call items declared in the inner layers from the outer layers, but you cannot call items declared in the outer layers from the inner layers.&lt;br&gt;
In this article, I will introduce the code while paying attention to the direction of dependencies.&lt;/p&gt;
&lt;h4&gt;
  
  
  Endpoints
&lt;/h4&gt;

&lt;p&gt;The endpoints for each function are as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;GET: /users&lt;br&gt;
POST: /users&lt;br&gt;
DELETE: /users/:id&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Directory Structure
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flc2qvo4n5wbzy5u1ol5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flc2qvo4n5wbzy5u1ol5i.png" alt="Directory Structure" width="781" height="588"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The layers of Clean Architecture can be aligned with the directory structure as shown below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F6eg32uuavtl9d2gt9cre.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6eg32uuavtl9d2gt9cre.png" alt="Table of Directory name and layer" width="316" height="186"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  domain
&lt;/h4&gt;

&lt;p&gt;In the domain layer, the Entity is defined. As it is at the center, it can be called from any layer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fipdx0mcnyc1kh67xiqxo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fipdx0mcnyc1kh67xiqxo.png" width="754" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/src/domain/user.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package domain

type User struct {
    ID   int    `json:"id" gorm:"primary_key"`
    Name string `json:"name"`
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, I will create a User with an ID and Name column, with the ID set as the primary key.&lt;/p&gt;

&lt;p&gt;Regarding json:"id" gorm:"primary_key", json:"id" is for JSON mapping, and gorm:"primary_key" is for tagging models in GORM. You can also define other SQL table properties such as not null, unique, and default.&lt;br&gt;
Reference: GORM Declaring Model&lt;/p&gt;
&lt;h4&gt;
  
  
  Infrastructure
&lt;/h4&gt;

&lt;p&gt;The outermost layer, Infrastructure, deals with the parts of the application that interact with external components. In this case, I define the database connection and router here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F7vmvjl607i8hd7qz2icx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F7vmvjl607i8hd7qz2icx.png" width="756" height="557"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since it is the outermost layer, it can be called without being aware of any other layers.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/src/infrastructure/sqlhandler.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package infrastructure

import (
    "gorm.io/driver/mysql"
    "gorm.io/gorm"

    "echoSample/src/interfaces/database"
)

type SqlHandler struct {
    db *gorm.DB
}

func NewSqlHandler() database.SqlHandler {
    dsn := "root:password@tcp(127.0.0.1:3306)/go_sample?charset=utf8mb4&amp;amp;parseTime=True&amp;amp;loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &amp;amp;gorm.Config{})
    if err != nil {
        panic(err.Error)
    }
    sqlHandler := new(SqlHandler)
    sqlHandler.db = db
    return sqlHandler
}

func (handler *SqlHandler) Create(obj interface{}) {
    handler.db.Create(obj)
}

func (handler *SqlHandler) FindAll(obj interface{}) {
    handler.db.Find(obj)
}

func (handler *SqlHandler) DeleteById(obj interface{}, id string) {
    handler.db.Delete(obj, id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the database connection, the official documentation was helpful:&lt;br&gt;
gorm.io&lt;/p&gt;

&lt;p&gt;Next is routing. In this example, I use the Echo web framework. The official documentation was also helpful for this, and it is where I define our API methods and paths:&lt;br&gt;
echo.labstack&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/src/infrastructure/router.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package infrastructure

import (
    controllers "echoSample/src/interfaces/api"
    "net/http"

    "github.com/labstack/echo"
)

func Init() {
    // Echo instance
    e := echo.New()
    userController := controllers.NewUserController(NewSqlHandler())

    e.GET("/users", func(c echo.Context) error {
        users := userController.GetUser() 
        c.Bind(&amp;amp;users) 
        return c.JSON(http.StatusOK, users)
    })

    e.POST("/users", func(c echo.Context) error {
        userController.Create(c)
        return c.String(http.StatusOK, "created")
    })

    e.DELETE("/users/:id", func(c echo.Context) error {
        id := c.Param("id")
        userController.Delete(id)
        return c.String(http.StatusOK, "deleted")
    })

    // Start server
    e.Logger.Fatal(e.Start(":1323"))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Interfaces
&lt;/h4&gt;

&lt;p&gt;In the Controllers and Presenters layers, We need to be aware of dependencies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fib9gorqjyeuy5v4icpx3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fib9gorqjyeuy5v4icpx3.png" width="752" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Calling the domain and usecase layers from the interface layer is not a problem, but we cannot call the infrastructure layer directly. Instead, we define an interface (the sqlHandler interface defined in the infrastructure layer).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/src/interfaces/api/user_controller.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package controllers

import (
    "echoSample/src/domain"
    "echoSample/src/interfaces/database"
    "echoSample/src/usecase"

    "github.com/labstack/echo"
)

type UserController struct {
    Interactor usecase.UserInteractor
}

func NewUserController(sqlHandler database.SqlHandler) *UserController {
    return &amp;amp;UserController{
        Interactor: usecase.UserInteractor{
            UserRepository: &amp;amp;database.UserRepository{
                SqlHandler: sqlHandler,
            },
        },
    }
}

func (controller *UserController) Create(c echo.Context) {
    u := domain.User{}
    c.Bind(&amp;amp;u)
    controller.Interactor.Add(u)
    createdUsers := controller.Interactor.GetInfo()
    c.JSON(201, createdUsers)
    return
}

func (controller *UserController) GetUser() []domain.User {
    res := controller.Interactor.GetInfo()
    return res
}

func (controller *UserController) Delete(id string) {
    controller.Interactor.Delete(id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controller calls the usecase and domain layers, which is not a problem.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/interfaces/api/context.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package controllers

type Context interface {
    Param(string) string
    Bind(interface{}) error
    Status(int)
    JSON(int, interface{})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Regarding the database:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/interfaces/database/user_repository.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package database

import (
    "echoSample/src/domain"
)

type UserRepository struct {
    SqlHandler
}

func (db *UserRepository) Store(u domain.User) {
    db.Create(&amp;amp;u)
}

func (db *UserRepository) Select() []domain.User {
    user := []domain.User{}
    db.FindAll(&amp;amp;user)
    return user
}
func (db *UserRepository) Delete(id string) {
    user := []domain.User{}
    db.DeleteById(&amp;amp;user, id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the repository, the sqlHandler is called, but instead of directly calling the infrastructure layer's sqlHandler, we call it through the sqlHandler interface defined in the same layer.&lt;br&gt;
This is known as the Dependency Inversion Principle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/interfaces/db/sql_handler.go&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;package database&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type SqlHandler interface {
    Create(object interface{})
    FindAll(object interface{})
    DeleteById(object interface{}, id string)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we can call the sql_handler's functions.&lt;/p&gt;

&lt;h4&gt;
  
  
  usecase
&lt;/h4&gt;

&lt;p&gt;Lastly, we have the usecase layer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fwf0bevk7mpe8y0peoj57.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fwf0bevk7mpe8y0peoj57.png" width="750" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;src/usecase/user_interactor.go&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package usecase

import "echoSample/src/domain"

type UserInteractor struct {
    UserRepository UserRepository
}

func (interactor *UserInteractor) Add(u domain.User) {
    interactor.UserRepository.Store(u)
}

func (interactor *UserInteractor) GetInfo() []domain.User {
    return interactor.UserRepository.Select()
}

func (interactor *UserInteractor) Delete(id string) {
    interactor.UserRepository.Delete(id)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, as before, we need to apply the Dependency Inversion Principle, so we define user_repository.go.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;src/usecase/user_repository.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package usecase

import (
    "echoSample/src/domain"
)

type UserRepository interface {
    Store(domain.User)
    Select() []domain.User
    Delete(id string)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, our implementation is complete.&lt;br&gt;
Now, by running MySQL using docker-compose.yml and starting the server, the API should work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3.6"
services:
  db:
    image: mysql:5.7
    container_name: go_sample
    volumes:
      # mysql configuration
      - ./mysql/conf:/etc/mysql/conf.d
      - ./mysql/data:/var/lib/mysql
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: go_sample
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: root
      TZ: "Asia/Tokyo"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;src/server.go&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "echoSample/src/domain"
    "echoSample/src/infrastructure"

    "github.com/labstack/echo/v4"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

var (
    db  *gorm.DB
    err error
    dsn = "root:password@tcp(127.0.0.1:3306)/go_sample?charset=utf8mb4&amp;amp;parseTime=True&amp;amp;loc=Local"
)

func main() {
    dbinit()
    infrastructure.Init()
    e := echo.New()
    e.Logger.Fatal(e.Start(":1323"))
}

func dbinit() {
    db, err = gorm.Open(mysql.Open(dsn), &amp;amp;gorm.Config{})
    if err != nil {
    }
    db.Migrator().CreateTable(domain.User{})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Run Mysql
&lt;/h4&gt;

&lt;p&gt;docker-compose up -d&lt;/p&gt;

&lt;h4&gt;
  
  
  Run Server
&lt;/h4&gt;

&lt;p&gt;go run serve.go&lt;/p&gt;

&lt;h2&gt;
  
  
  Call API
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;POST: /users&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --location --request POST 'localhost:1323/users' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name":"J.Y Park"
}'
curl --location --request POST 'localhost:1323/users' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name":"Eron Mask"
}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;GET: /users&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl --location --request GET localhost:1323/users'

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

&lt;/div&gt;



&lt;p&gt;It works! &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Conclusion&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By not only reading articles about Clean Architecture but also actually creating a simple API and testing its functionality, your understanding will deepen.&lt;br&gt;
However, to be honest, the advantage of Clean Architecture might not be fully appreciated when working on a project of the CRUD app scale.&lt;br&gt;
Clean Architecture not only improves code readability and productivity but also has the characteristic of being resistant to change. So, it would be nice to experience its benefits by adding various features to the app I created this time…!&lt;/p&gt;

</description>
      <category>cleanarchitecture</category>
      <category>go</category>
      <category>crud</category>
      <category>app</category>
    </item>
  </channel>
</rss>
