<?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: HICHOMA</title>
    <description>The latest articles on DEV Community by HICHOMA (@haithambh).</description>
    <link>https://dev.to/haithambh</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%2F1240869%2F6ea8ce4f-29ff-46b0-989d-68b65d0a5a75.jpeg</url>
      <title>DEV Community: HICHOMA</title>
      <link>https://dev.to/haithambh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haithambh"/>
    <language>en</language>
    <item>
      <title>Golang REST API boilerplate</title>
      <dc:creator>HICHOMA</dc:creator>
      <pubDate>Tue, 04 Jun 2024 20:58:49 +0000</pubDate>
      <link>https://dev.to/haithambh/golang-rest-api-boilerplate-165k</link>
      <guid>https://dev.to/haithambh/golang-rest-api-boilerplate-165k</guid>
      <description>&lt;p&gt;Upon meticulous investigation into the top Golang RESTful API project architectures, I have discovered that the Goffers community shares a number of common structures. I'll share my decision with you along with a boilerplate that makes use of &lt;strong&gt;MongoDB&lt;/strong&gt; and the &lt;strong&gt;Golang Echo framework&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Project initialization
&lt;/h2&gt;

&lt;p&gt;First we need to initialize golang project with :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go mod init github.com/HichOoMa/golang-boilerplate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this command will init go modules and help you to manage your project dependencies&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Files Structure
&lt;/h2&gt;

&lt;p&gt;This the folder structure of the code that I used to work with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
.
└── golang-boilerplate
    ├── cmd
    │ ├── api
    │ │ └── main.go
    │ └── ...
    ├── api
    │ ├── handlers
    │ ├── middlewares
    │ ├── routes
    │ └── init.go
    ├── assets
    │ └── ...
    ├── internal
    │ ├── app
    │ │ ├── config
    │ │ ├── database
    │ │ ├── models
    │ │ ├── enums
    │ │ ├── business
    │ │ └── ...
    │ └── lib
    │ └── ...
    ├── pkg
    │ ├── cloud
    │ ├── jwt
    │ ├── mailer
    │ └── ...
    ├── test
    │ └── ...
    ├── go.mod
    ├── go.sum
    └── Makerfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  a. &lt;code&gt;/cmd&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;In a Go project, the "cmd" folder is a commonly used convention for organizing the entry points or main packages of your application. It stands for "command" and is typically used to house executable commands or programs within your project.&lt;/p&gt;

&lt;p&gt;The purpose of organizing your main packages under "cmd" is to separate them from the reusable packages stored in the "pkg" or "internal" directories. This makes it clearer which parts of your code base are intended to be standalone commands or programs versus shared libraries or packages.&lt;/p&gt;

&lt;p&gt;Using the "cmd" folder structure also aligns well with the Go ecosystem's conventions for package organization and helps keep your project modular and maintainable.&lt;/p&gt;

&lt;h3&gt;
  
  
  b. &lt;code&gt;/api&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The api folder contain the necessary element of the owr API such as handlers, middelware and routes&lt;/p&gt;

&lt;h4&gt;
  
  
  Server Creation
&lt;/h4&gt;

&lt;p&gt;In the first place we need to create our TCP server to listen for the HTTP requests coming to own local port. As we already mentioned we will use Echo framework.&lt;/p&gt;

&lt;p&gt;We need first to install our dependencies :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ go get github.com/labstack/echo/v4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we can start our server by creating our &lt;code&gt;init()&lt;/code&gt; function in &lt;code&gt;api/init.go&lt;/code&gt;&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;api&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"net/http"&lt;/span&gt;
    &lt;span class="s"&gt;"github.com/labstack/echo/v4"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;echo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c"&gt;// add server routes and middleware&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Logger&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Fatal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":1323"&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 we are listning to encoming request on &lt;code&gt;localhost:1323&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;/handlers&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In REST API projects, the "handlers" folder is commonly used to organize HTTP request handlers or controllers. This folder typically contains Go files that define the logic for handling incoming HTTP requests, parsing request parameters, validating input, interacting with the database, and returning responses.&lt;/p&gt;

&lt;p&gt;This is a base example of an echo framework handler&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="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;echo&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="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// you can do some business here&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusOK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello, World!"&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;h4&gt;
  
  
  &lt;code&gt;/middlewares&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Also the middlewares is commonly used to organize HTTP Request and add layers to specific routes for different reasons such as security, presentations, error handling, ...&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="n"&gt;helloMiddleware&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;echo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerFunc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;echo&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HandlerFunc&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&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;c&lt;/span&gt; &lt;span class="n"&gt;echo&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="kt"&gt;error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c"&gt;// do some business here&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&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;h4&gt;
  
  
  &lt;code&gt;/routes&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Routes is the part of code that attach path ( "/example", "/order/edit", ... ) with middlewares and handlers to process HTTP request requests.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;it can be just a file or part of the init function if own app contain a small number of routes&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/status"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;getOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/order"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;createOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PUT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updateOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DELETE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/hello"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;deleteOrder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  c. `/assets
&lt;/h3&gt;

&lt;p&gt;You can store any assets or resources you need it in you application such as images, csv files, logos, etc&lt;/p&gt;

&lt;h3&gt;
  
  
  d. &lt;code&gt;/internal&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This folder contain mainly private application and library code. This is the code you don't want others importing in their applications or libraries. Note that this layout pattern is enforced by the Go compiler itself. See the Go 1.4 &lt;a href="https://golang.org/doc/go1.4#internalpackages"&gt;&lt;code&gt;release notes&lt;/code&gt;&lt;/a&gt; for more details. Note that you are not limited to the top level &lt;code&gt;internal&lt;/code&gt; directory. You can have more than one &lt;code&gt;internal&lt;/code&gt; directory at any level of your project tree.&lt;/p&gt;

&lt;p&gt;You can optionally add a bit of extra structure to your internal packages to separate your shared and non-shared internal code. It's not required (especially for smaller projects), but it's nice to have visual clues showing the intended package use. Your actual application code can go in the &lt;code&gt;/internal/app&lt;/code&gt; directory (e.g., &lt;code&gt;/internal/app/myapp&lt;/code&gt;) and the code shared by those apps in the &lt;code&gt;/internal/pkg&lt;/code&gt; directory (e.g., &lt;code&gt;/internal/pkg/myprivlib&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/hashicorp/terraform/tree/main/internal"&gt;https://github.com/hashicorp/terraform/tree/main/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/influxdata/influxdb/tree/master/internal"&gt;https://github.com/influxdata/influxdb/tree/master/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/perkeep/perkeep/tree/master/internal"&gt;https://github.com/perkeep/perkeep/tree/master/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jaegertracing/jaeger/tree/main/internal"&gt;https://github.com/jaegertracing/jaeger/tree/main/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/moby/moby/tree/master/internal"&gt;https://github.com/moby/moby/tree/master/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/satellity/satellity/tree/main/internal"&gt;https://github.com/satellity/satellity/tree/main/internal&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/minio/minio/tree/master/internal"&gt;https://github.com/minio/minio/tree/master/internal&lt;/a&gt;
#### &lt;code&gt;/internal/pkg&lt;/code&gt;
Examples:&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/hashicorp/waypoint/tree/main/internal/pkg"&gt;https://github.com/hashicorp/waypoint/tree/main/internal/pkg&lt;/a&gt;
### e. &lt;code&gt;/pkg&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This one contain library code that's ok to use by external applications (e.g., &lt;code&gt;/pkg/jwt&lt;/code&gt;). Other projects will import these libraries expecting them to work, so think twice before you put something here :-) Note that the &lt;code&gt;internal&lt;/code&gt; directory is a better way to ensure your private packages are not importable because it's enforced by Go. The &lt;code&gt;/pkg&lt;/code&gt; directory is still a good way to explicitly communicate that the code in that directory is safe for use by others. The &lt;a href="https://travisjeffery.com/b/2019/11/i-ll-take-pkg-over-internal/"&gt;&lt;code&gt;I'll take pkg over internal&lt;/code&gt;&lt;/a&gt; blog post by Travis Jeffery provides a good overview of the &lt;code&gt;pkg&lt;/code&gt; and &lt;code&gt;internal&lt;/code&gt; directories and when it might make sense to use them.&lt;/p&gt;

&lt;p&gt;It's also a way to group Go code in one place when your root directory contains lots of non-Go components and directories making it easier to run various Go tools (as mentioned in these talks: &lt;a href="https://www.youtube.com/watch?v=PTE4VJIdHPg"&gt;&lt;code&gt;Best Practices for Industrial Programming&lt;/code&gt;&lt;/a&gt; from GopherCon EU 2018, &lt;a href="https://www.youtube.com/watch?v=oL6JBUk6tj0"&gt;GopherCon 2018: Kat Zien - How Do You Structure Your Go Apps&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=3gQa1LWwuzk"&gt;GoLab 2018 - Massimiliano Pippi - Project layout patterns in Go&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Note that this is not a universally accepted pattern and for every popular repo that uses it you can find 10 that don't. It's up to you to decide if you want to use this pattern or not. Regardless of whether or not it's a good pattern more people will know what you mean than not. It might a bit confusing for some of the new Go devs, but it's a pretty simple confusion to resolve and that's one of the goals for this project layout repo.&lt;/p&gt;

&lt;p&gt;Ok not to use it if your app project is really small and where an extra level of nesting doesn't add much value (unless you really want to). Think about it when it's getting big enough and your root directory gets pretty busy (especially if you have a lot of non-Go app components).&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;pkg&lt;/code&gt; directory origins: The old Go source code used to use &lt;code&gt;pkg&lt;/code&gt; for its packages and then various Go projects in the community started copying the pattern (see &lt;a href="https://twitter.com/bradfitz/status/1039512487538970624"&gt;&lt;code&gt;this&lt;/code&gt;&lt;/a&gt; Brad Fitzpatrick's tweet for more context).&lt;/p&gt;

&lt;p&gt;Examples :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/containerd/containerd/tree/main/pkg"&gt;https://github.com/containerd/containerd/tree/main/pkg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/slimtoolkit/slim/tree/master/pkg"&gt;https://github.com/slimtoolkit/slim/tree/master/pkg&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/telepresenceio/telepresence/tree/release/v2/pkg"&gt;https://github.com/telepresenceio/telepresence/tree/release/v2/pkg&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  f. &lt;code&gt;/test&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Additional external test apps and test data. Feel free to structure the &lt;code&gt;/test&lt;/code&gt; directory anyway you want. For bigger projects it makes sense to have a data subdirectory. For example, you can have &lt;code&gt;/test/data&lt;/code&gt; or &lt;code&gt;/test/testdata&lt;/code&gt; if you need Go to ignore what's in that directory. Note that Go will also ignore directories or files that begin with "." or "_", so you have more flexibility in terms of how you name your test data directory.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/openshift/origin/tree/master/test"&gt;https://github.com/openshift/origin/tree/master/test&lt;/a&gt; (test data is in the &lt;code&gt;/testdata&lt;/code&gt; subdirectory)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>go</category>
      <category>restapi</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
