{"id":495,"date":"2023-11-06T18:47:38","date_gmt":"2023-11-06T17:47:38","guid":{"rendered":"https:\/\/clasenceneba.com\/?p=495"},"modified":"2023-11-06T18:58:51","modified_gmt":"2023-11-06T17:58:51","slug":"golang-echo-base-project-for-startups","status":"publish","type":"post","link":"https:\/\/clasenceneba.com\/fr\/golang-echo-base-project-for-startups\/","title":{"rendered":"Projet de d\u00e9marrage Golang(Echo+Ginkgo) pour startups"},"content":{"rendered":"<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/github.com\/scneba\/echobase\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"481\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-1-1024x481.png\" alt=\"\" class=\"wp-image-498\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-1-1024x481.png 1024w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-1-300x141.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-1-768x360.png 768w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-1.png 1078w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">See Project on <a href=\"https:\/\/github.com\/scneba\/echobase\" data-type=\"link\" data-id=\"https:\/\/github.com\/scneba\/echobase\">Github<\/a><\/figcaption><\/figure>\n\n\n\n<p>Il s'agit d'un projet de d\u00e9marrage pour toute startup qui souhaite utiliser Golang pour cr\u00e9er une API REST pour son backend. Il est construit sur <a href=\"https:\/\/echo.labstack.com\/\" data-type=\"link\" data-id=\"https:\/\/echo.labstack.com\/\">Echo<\/a> framework and tested with <a href=\"https:\/\/onsi.github.io\/ginkgo\/\" data-type=\"link\" data-id=\"https:\/\/onsi.github.io\/ginkgo\/\">Ginkgo<\/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 the 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 must 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 information 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 to 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 the 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>Install the ginkgo tool to start running tests with the ginkgo command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>go install github.com\/onsi\/ginkgo\/v2\/ginkgo<\/code><\/pre>\n\n\n\n<p>Open terminal\/command line and try the command below and you should see the output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ginkgo version<\/code><\/pre>\n\n\n\n<p>Ideally, all services in the project should have a&nbsp;<code>service_test.go<\/code> with the below testing setup. <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"805\" height=\"190\" src=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image.png\" alt=\"\" class=\"wp-image-496\" srcset=\"https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image.png 805w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-300x71.png 300w, https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/image-768x181.png 768w\" sizes=\"(max-width: 805px) 100vw, 805px\" \/><\/figure>\n\n\n\n<p>To run the tests, you can choose the ginkgo command or go test command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ginkgo<\/code><\/pre>\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>","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 Echo framework and tested with Ginkgo. Find below the structure and philosophy. Packages and Resources File structure Setup NB: All commands below should be run on git bash or any [&hellip;]<\/p>","protected":false},"author":1,"featured_media":502,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[41,42,43,22],"blocksy_meta":"","featured_image_urls":{"full":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",607,506,false],"thumbnail":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1-150x150.png",150,150,true],"medium":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1-300x250.png",300,250,true],"medium_large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",607,506,false],"large":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",607,506,false],"1536x1536":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",607,506,false],"2048x2048":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",607,506,false],"trp-custom-language-flag":["https:\/\/clasenceneba.com\/wp-content\/uploads\/2023\/11\/ginkgo-1.png",14,12,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 Echo framework and tested with Ginkgo. 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 the 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\u2026<\/p>\n","category_list":"<a href=\"https:\/\/clasenceneba.com\/fr\/category\/projects\/\" rel=\"category tag\">Projects<\/a>","author_info":{"name":"clasenceneba.com","url":"https:\/\/clasenceneba.com\/fr\/author\/clasenceneba-com\/"},"comments_num":"0 commentaire","_links":{"self":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/495"}],"collection":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":2,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":500,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/posts\/495\/revisions\/500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media\/502"}],"wp:attachment":[{"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/clasenceneba.com\/fr\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}