Creating a Simple HTTP Server in Go
Go (often called Golang) is one of the most popular programming languages for building fast and reliable backend services. Thanks to its excellent standard library, creating an HTTP server requires only a few lines of code. In this article, we’ll build a simple HTTP server from scratch while keeping the project clean and organized.
project/
│── app/
│ └── application.go
│── .env
│── go.mod
└── main.goStep 1 : Intialize a Go Module
go mod init <name_you_want_to_give>this command creates a go.mod file, which keeps track of your project’s dependencies and module information.
Step 2 : Install the dotenv Package
To load configurations from a .env file , install the godotenv package by running:
go get github.com/joho/godotenvStep 3 : Configure and Load Environment Variables
Now that the godotenv package is installed, let’s create a .env file to store our application’s configuration.
Inside the root of your project, create a file named .env and add the following:
PORT = 4005Next, open your main.go file and import the required packages:
package main
import (
"fmt"
"log"
"os"
"github.com/joho/godotenv"
)Now, load the .env file inside the main() function:
func main(){
err:=godotenv.Load()
if err != nil{
log.Fatal("Error loading .env file")
}
PORT:=":" + os.Getenv("PORT")
}This value will later be used when we start our HTTP server.
Get Abhinav Sunil’s stories in your inbox
Join Medium for free to get updates from this writer.
Step 4: Initialize the HTTP Server
To keep our project organized, we’ll separate the server logic from main.go
Create the app Package
Inside the project root, create a new folder named app. Then, create a file called application.go.
package app
import (
"fmt"
"net/http"
"time"
)
type Config struct{
Addr string
}
type Application struct{
Config Config
}
func (app *Application) Run() error{
server:=&http.Server{
Addr: app.Config.Addr,
Handler: nil,
ReadTimeout: 10*time.Second,
WriteTimeout: 10*time.Second,
}
fmt.Println("Starting server on ",app.Config.Addr)
return server.ListenAndServe()
}Update main.go
Now that we’ve created the Application type, we can initialize it inside main.go.
cfg:=app.Config{
Addr: PORT,
}
app:=app.Application{
Config: cfg,
}
err = app.Run()
if err != nil{
log.Fatal(err)
}Full Code:
app/application.go:
package app
import (
"fmt"
"net/http"
"time"
)
type Config struct{
Addr string
}
type Application struct{
Config Config
}
func (app *Application) Run() error{
server:=&http.Server{
Addr: app.Config.Addr,
Handler: nil,
ReadTimeout: 10*time.Second,
WriteTimeout: 10*time.Second,
}
fmt.Println("Starting server on ",app.Config.Addr)
return server.ListenAndServe()
}main.go
package main
import (
"AeromindGO/app"
"fmt"
"log"
"os"
"github.com/joho/godotenv"
)
func main(){
err:=godotenv.Load()
if err != nil{
log.Fatal("Error loading .env file")
fmt.Println("Unable to load .env")
}
PORT:=":" + os.Getenv("PORT")
cfg:=app.Config{
Addr: PORT,
}
app:=app.Application{
Config: cfg,
}
err = app.Run()
if err != nil{
log.Fatal(err)
}
}
