<?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>Forem: Adetunji Adetayo</title>
    <description>The latest articles on Forem by Adetunji Adetayo (@hazaloolu).</description>
    <link>https://forem.com/hazaloolu</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%2F1696851%2F195dcf7f-9a44-4969-9170-eb39992e7b84.jpg</url>
      <title>Forem: Adetunji Adetayo</title>
      <link>https://forem.com/hazaloolu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hazaloolu"/>
    <language>en</language>
    <item>
      <title>Building a Blog API with Golang using Gin and Gorm (Part 1)</title>
      <dc:creator>Adetunji Adetayo</dc:creator>
      <pubDate>Thu, 22 Aug 2024 22:58:43 +0000</pubDate>
      <link>https://forem.com/hazaloolu/building-a-blog-api-with-golang-using-gin-and-gorm-451o</link>
      <guid>https://forem.com/hazaloolu/building-a-blog-api-with-golang-using-gin-and-gorm-451o</guid>
      <description>

&lt;h3&gt;
  
  
  Project Overview
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. What You Will Build
&lt;/h4&gt;

&lt;p&gt;You will construct a blog API with the following functionalities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Registration with a username, email and password&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Login with email and password&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create new post&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Update post&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Retrieve a particular post&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete Post&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Prerequisites
&lt;/h4&gt;

&lt;p&gt;Before diving in, ensure you have the following prerequisites:&lt;/p&gt;

&lt;p&gt;Go &lt;br&gt;
Basic understanding of Go and JWTs&lt;br&gt;
Postman&lt;br&gt;
Git&lt;br&gt;
psql&lt;br&gt;
PostgreSQL installed on your machine&lt;/p&gt;
&lt;h3&gt;
  
  
  Project Setup
&lt;/h3&gt;

&lt;p&gt;To initiate the project, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new folder named blog_api in your development directory and navigate into it.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir blog_api
cd blog_api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Initialize a Go module with the following command.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go mod init blog_api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This generates a &lt;strong&gt;go.mod&lt;/strong&gt; file to track your project's dependencies.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Dependencies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This project utilizes the gin framework. Run the following command to install gin and other dependencies we will be using&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/gin-gonic/gin
go get github.com/jinzhu/gorm
go get golang.org/x/crypto/bcrypt
github.com/golang-jwt/jwt/v5

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

&lt;/div&gt;



&lt;p&gt;Now create the following directories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;internal&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal/auth&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal/handler&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal/storage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal/router&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;internal/model&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;config&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;main.go&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Connecting to Postgres
&lt;/h3&gt;

&lt;p&gt;I am assuming that you have postgres installed already, if you dont go ahead and install &lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;it&lt;/a&gt;&lt;br&gt;
create a database named blog_db&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE blog_db;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your config folder Create a .env file in your root directory to store our database details&lt;/p&gt;

&lt;p&gt;config/.env&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB_DSN=host=localhost user=yourusername password= your_password dbname=blog_db port=5432 sslmode=disable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating models
&lt;/h3&gt;

&lt;p&gt;create models for &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Post&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In your internal/model folder, create a user.go and a post.go file&lt;/p&gt;

&lt;p&gt;Internal/model/user.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 model

import "gorm.io/gorm"

type User struct {
Username   string    `gorm:"unique; not null"`
Email      string    `gorm:"unique; not null"`
Password   string    `gorm:"not null"`
Posts      []Post    `gorm:"foreignKey:AuthorID"`
}

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

&lt;/div&gt;



&lt;p&gt;internal/model/post..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 model

import "gorm.io/gorm"

type Post struct {
    gorm.Model

    Title    string `gorm:"not null"`
    Content  string `gorm:"not null"`
    AuthorID uint
    Author   User `gorm:"foreignKey:AuthorID"`
}


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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Initializing Database
&lt;/h3&gt;

&lt;p&gt;We will setup the database connection using Gorm. In your internal/storage folder, create a new file named database.go.&lt;/p&gt;

&lt;p&gt;internal/storage/database.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 storage

import (
    "log"
    "os"
    "blog-api/internal/model"
    "gorm.io/driver/postgres"
    "gorm.io/gorm"
)

DB *gorm.DB

func InitDB()
 {
dsn := os.Getenv("DB_DSN")
db, err := gorm.open(postgres.Open(dsn), &amp;amp;gorm.Config{})

if err != nil {
log.Fatal("Failed to connect to database", err)

}

db.Automigrate(&amp;amp;model.User{}, &amp;amp;model.Post{})
DB = db
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code snippet:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DB is a global variable that holds the database connection.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;InitDB initializes the database connection using the gorm.Open method and automatically migrates the User and Post models.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Now, set up main.go file to :&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;loads the .env file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initializes the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sets up the router and starts the HTTP server.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

import (
    "log"
    "net/http"

    "blog-api/internal/router"
    "blog-api/internal/storage"
    "github.com/joho/godotenv"
)

func main() {
err := godotenv.Load("config/.env)

if err != nil {
log.Fatal("Error loading .env file)

}

//initialize database 
storage.InitDB()

//setup router
r := router.setupRouter()
log.Fatal(http.ListenAndServe(":8080", r))

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The setupRouter is a function in our router.go that sets up our application router, feel free to comment it out, we will setup our router in the next part of the blog.&lt;/p&gt;

&lt;h4&gt;
  
  
  Run the Application
&lt;/h4&gt;

&lt;p&gt;Run the application using the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go run main.go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Don't stop Building</title>
      <dc:creator>Adetunji Adetayo</dc:creator>
      <pubDate>Mon, 05 Aug 2024 04:23:35 +0000</pubDate>
      <link>https://forem.com/hazaloolu/dont-stop-building-4ji7</link>
      <guid>https://forem.com/hazaloolu/dont-stop-building-4ji7</guid>
      <description>&lt;p&gt;We often find ourselves comparing our work to others, doubting our abilities, and questioning the value of what we create. But here's a reminder for everyone: do not stop building.&lt;br&gt;
Whether you're building or designing a to-do app, writing an article, or crafting anything else — there is immense beauty in the act of creation.&lt;br&gt;
So, whenever you question the significance of your work, remind yourself: do not stop building. Celebrate your progress, no matter how minor it may seem. Share your creations with others, inspire and be inspired, and keep pushing forward.&lt;/p&gt;

&lt;p&gt;Your creations matter. Keep building, keep creating.&lt;br&gt;
I am rooting for you.&lt;/p&gt;

</description>
      <category>development</category>
      <category>buildinpublic</category>
    </item>
  </channel>
</rss>
