{"id":336,"date":"2022-10-17T20:10:00","date_gmt":"2022-10-17T19:10:00","guid":{"rendered":"https:\/\/clasenceneba.com\/?p=336"},"modified":"2023-11-06T19:01:28","modified_gmt":"2023-11-06T18:01:28","slug":"golang-base-project-for-startups","status":"publish","type":"post","link":"https:\/\/clasenceneba.com\/en_gb\/golang-base-project-for-startups\/","title":{"rendered":"Golang (Mux + Testify) starter project for startups"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/github.com\/scneba\/gobase\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"534\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-1024x534.jpg\" alt=\"\" class=\"wp-image-337\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-1024x534.jpg 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-300x156.jpg 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-768x400.jpg 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-1536x800.jpg 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang.jpg 1871w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">See project on <a href=\"https:\/\/github.com\/scneba\/gobase\" data-type=\"link\" data-id=\"https:\/\/github.com\/scneba\/gobase\">Github<\/a><\/figcaption><\/figure>\n\n\n\n<p>This is a starter project for any startup that wants to use Golang to build a REST API for their backend. It is built on a <a href=\"https:\/\/github.com\/gorilla\/mux\" data-type=\"link\" data-id=\"https:\/\/github.com\/gorilla\/mux\">Mux<\/a> router and tested with <a href=\"https:\/\/www.npmjs.com\/package\/testify\" data-type=\"link\" data-id=\"https:\/\/www.npmjs.com\/package\/testify\">Testify<\/a>. Find below the structure and philosophy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"packages-and-resources\">Packages and Resources<\/h2>\n\n\n\n<ul>\n<li>Golang 1.17 or above<\/li>\n\n\n\n<li>Git<\/li>\n\n\n\n<li>PostgreSQL<\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/rubenv\/sql-migrate\" data-type=\"link\" data-id=\"https:\/\/github.com\/rubenv\/sql-migrate\">Sql-migrate<\/a> tool<\/li>\n\n\n\n<li>Postman<\/li>\n\n\n\n<li>VSCode<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">File structure<\/h2>\n\n\n\n<ul>\n<li><strong>migrate<\/strong>&nbsp;\n<ul>\n<li><strong>migrations\/  <\/strong>contains all migrations &nbsp;written in SQL and run by sql-migrate<\/li>\n\n\n\n<li><strong>dbconfig.yml <\/strong> file contains configuration for sql-migrate tool.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>pkg<\/strong>&nbsp;&nbsp;&nbsp;&nbsp;\n<ul>\n<li><strong>api\/<\/strong>&nbsp;functions which receive the API calls from the routes and call the services. They also write the responses from the services. &nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>database<\/strong>\/&nbsp;functions to connect to the database and perform any db operation. &nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>models<\/strong>\/&nbsp;all database models used to connect to the database. &nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>registering<\/strong>\/ &nbsp;All folders with names ending in&nbsp;<em><strong>ing<\/strong><\/em>&nbsp;are the sub-controllers with all the business logic. Each folder has a&nbsp; <strong>service.go<\/strong>&nbsp;file that has the logic and the&nbsp;<strong>service_test.go<\/strong>&nbsp;file which has the tests of the logic. The&nbsp;<strong>errors.go<\/strong>&nbsp;file contains all the errors. The rest of the folders contain data structures used in the business logic.<\/li>\n\n\n\n<li><strong>postman<\/strong>\/&nbsp;This contains the postman collection with tests.&nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>env.example<\/strong>&nbsp;This contains all the variables which are application secrets. To run the code, you will have to create a&nbsp;<code><strong>.env<\/strong><\/code><strong>&nbsp;<\/strong>file from this file and change all the variables to the ones you use locally. &nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>main.go<\/strong>&nbsp;The main file which connects to database, connects all services, and starts running the code on the specified port (5010 in this case).  <\/li>\n\n\n\n<li><strong>services.go<\/strong>&nbsp;This file initializes all services. &nbsp;&nbsp;<\/li>\n\n\n\n<li><strong>routes<\/strong>&nbsp;This file contains all the routes of the project.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"setup\">Setup<\/h2>\n\n\n\n<p>NB: All commands below should be run on git bash or any bash-supported terminal.<\/p>\n\n\n\n<p>1. Download and install git from&nbsp;<a href=\"https:\/\/git-scm.com\/downloads\">here<\/a>. This should also install git bash.<\/p>\n\n\n\n<p>2. Download and Install PgAdmin 4 from&nbsp;<a href=\"https:\/\/www.pgadmin.org\/download\/pgadmin-4-windows\/\">here<\/a>. This comes with PostgreSQL and should automatically run after installation on port 5432(unless changed during installation). Make sure to take note of your passwords. There should be a PgAdmin password and the default user password. After installation, open git bash and run the following commands to check if Postgres is running on the same port.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>psql --version<\/code><\/pre>\n\n\n\n<p>If the version does not show up, add psql path to your system paths. On windows, this is installed by default at<em>&nbsp;<code>C:\\Program Files\\PostgreSQL\\14\\bin<\/code><\/em>&nbsp;for version 14.<\/p>\n\n\n\n<ul>\n<li>Connect user (use the default username, which should be&nbsp;<em>postgres<\/em>&nbsp;if not changed). Enter the default user password when requested.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>psql -U postgres<\/code><\/pre>\n\n\n\n<ul>\n<li>Check connection status<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>\\conninfo<\/code><\/pre>\n\n\n\n<p>You should see output like this image here. This contains the informations you will need to connect to the database.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"97\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1024x97.png\" alt=\"\" class=\"wp-image-321\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1024x97.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-300x28.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-768x73.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres-1536x146.png 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postgres.png 1611w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<ul>\n<li>Start PgAdmin app( Can be found on the start menu on windows)<\/li>\n\n\n\n<li>Create a database called&nbsp;<code><em>gobase<\/em><\/code>&nbsp;with the user&nbsp;<em>postgres<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>3. Install latest Golang 1.*. Download the variant for your operating system&nbsp;<a href=\"https:\/\/go.dev\/doc\/install\">here<\/a>. After installing, open git bash and type the command below to see if you get the help page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go version<\/code><\/pre>\n\n\n\n<p>4. install s<a href=\"https:\/\/github.com\/rubenv\/sql-migrate\" data-type=\"link\" data-id=\"https:\/\/github.com\/rubenv\/sql-migrate\">ql-migrate<\/a> tool globally<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go install github.com\/rubenv\/sql-migrate\/...@latest<\/code><\/pre>\n\n\n\n<p>If successful, try the command below and you should see the help output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sql-migrate --help<\/code><\/pre>\n\n\n\n<p>5. Clone this repository<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/scneba\/gobase.git\n<\/code><\/pre>\n\n\n\n<p>6. Go to root directory and run this command to install go packages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go mod tidy<\/code><\/pre>\n\n\n\n<p>7. Make a copy of the .env.example and rename it to .env. Update the connection string on line 1 to include your Postgres username and password.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONNECTION_STRING=host=localhost port=5432 user=YOUR_USERNAME dbname=godb password=YOUR_PASSWORD sslmode=disable\n<\/code><\/pre>\n\n\n\n<p>8. Go into the .vscode\/launch.json file the&nbsp;<code>program<\/code>&nbsp;on line 4 to the full path to the project&#8217;s root.<\/p>\n\n\n\n<p>9. On VSCode, top left, select&nbsp;<code>run<\/code>&nbsp;and&nbsp;<code>start debugging<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"548\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1024x548.png\" alt=\"\" class=\"wp-image-338\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1024x548.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-300x161.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-768x411.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image-1536x822.png 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/image.png 1890w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>10. Install postman and create an account. Download from&nbsp;<a href=\"https:\/\/www.postman.com\/downloads\/\">here<\/a>.<\/p>\n\n\n\n<p>11.  Import the json filed in&nbsp;<code>\/postman\/gobase.postman_collection.json<\/code>&nbsp;into postman.<\/p>\n\n\n\n<p>12. Update the variables to localhost:5010<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"335\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman-1024x335.jpg\" alt=\"\" class=\"wp-image-344\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman-1024x335.jpg 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman-300x98.jpg 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman-768x251.jpg 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman-1536x503.jpg 1536w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/postman.jpg 1964w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Run the Postman test&nbsp;<code>Add User Success<\/code>&nbsp;and it should be successful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Development<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/github.com\/scneba\/gobase\/blob\/main\/image-7.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/github.com\/scneba\/gobase\/raw\/main\/image-7.png\" alt=\"image-7.png\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Testing<\/h2>\n\n\n\n<p>Ideally, all services in the project should have a&nbsp;<code>service_test.go<\/code>. <\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/github.com\/scneba\/gobase\/blob\/main\/image-6.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/github.com\/scneba\/gobase\/raw\/main\/image-6.png\" alt=\"image-6.png\"\/><\/a><\/figure>\n\n\n\n<p>To run the tests, run the below command on git bash<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>go test .\/...<\/code><\/code><\/pre>\n\n\n\n<p>Congratulations, you are ready to start developing your REST API with Go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a starter project for any startup that wants to use Golang to build a REST API for their backend. It is built on a Mux router and tested with Testify. Find below the structure and philosophy. Packages and Resources File structure Setup NB: All commands below should be run on git bash or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":337,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"blocksy_meta":"","featured_image_urls":{"full":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang.jpg",1871,975,false],"thumbnail":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-150x150.jpg",150,150,true],"medium":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-300x156.jpg",300,156,true],"medium_large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-768x400.jpg",768,400,true],"large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-1024x534.jpg",1024,534,true],"1536x1536":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang-1536x800.jpg",1536,800,true],"2048x2048":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang.jpg",1871,975,false],"trp-custom-language-flag":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/10\/golang.jpg",18,9,false]},"post_excerpt_stackable":"<p>See project on Github This is a starter project for any startup that wants to use Golang to build a REST API for their backend. It is built on a Mux router and tested with Testify. Find below the structure and philosophy. Packages and Resources Golang 1.17 or above Git PostgreSQL Sql-migrate tool Postman VSCode File structure migrate&nbsp; migrations\/ contains all migrations &nbsp;written in SQL and run by sql-migrate dbconfig.yml file contains configuration for sql-migrate tool. pkg&nbsp;&nbsp;&nbsp;&nbsp; api\/&nbsp;functions which receive the API calls from the routes and call the services. They also write the responses from the services. &nbsp;&nbsp;&nbsp; database\/&nbsp;functions&hellip;<\/p>\n","category_list":"<a href=\"https:\/\/clasenceneba.com\/en_gb\/category\/projects\/\" rel=\"category tag\">Projects<\/a>","author_info":{"name":"clasenceneba.com","url":"https:\/\/clasenceneba.com\/en_gb\/author\/clasenceneba-com\/"},"comments_num":"0 comments","_links":{"self":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/336"}],"collection":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":7,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":505,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/posts\/336\/revisions\/505"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/media\/337"}],"wp:attachment":[{"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clasenceneba.com\/en_gb\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}