Build an OTP-Based Authentication Server with Go: Part 1
Getting Started
Begin by creating a new folder for your project and initialize a Go module with the following command:
go mod init github.com/vishaaxl/cheershare
Set up the Project Structure
Start by setting up a new Go project with the following folder structure:
my-otp-auth-server/ ├── cmd/ │ └── api/ │ └── main.go │ └── user.go │ └── token.go ├── internal/ │ └── data/ │ ├── models.go │ └── user.go │ └── token.go ├── docker-compose.yml ├── go.mod └── Makefile
Next, set up your docker-compose.yml file. This configuration will define the services—PostgreSQL and Redis—that you'll be working with throughout this tutorial.
Setting Up Services with Docker Compose
We will start by configuring the services required for our project. For the backend, we need the following:
Redis: We'll use the redis:6 image. This service will configure a password for secure access, expose port 6379, and enforce password authentication using the --requirepass flag to secure Redis access.
PostgreSQL: We'll use the postgres:13 image. The service will define a default user, password, and database, expose port 5432 for communication, and persist data with a named volume (postgres_data) to ensure durability.
Optional:
- Main Backend Service: You can define the main backend service here as well, which will interact with both PostgreSQL and Redis.
// docker-compose.yml services: postgres: image: postgres:13 container_name: postgres environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: mysecretpassword POSTGRES_DB: cheershare ports: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data redis: image: redis:6 container_name: redis environment: REDIS_PASSWORD: mysecretpassword ports: - "6379:6379" command: ["redis-server", "--requirepass", "mysecretpassword"] volumes: postgres_data:
Main Backend Service
For routing and handling HTTP requests, we’ll use the github.com/julienschmidt/httprouter package. To install the dependency, run the following command:
go get github.com/julienschmidt/httprouter
Next, create a file at cmd/api/main.go and paste the following code. An explanation for each line is provided in the comments:
// main.go package main import ( "fmt" "log" "net/http" "os" "time" "github.com/julienschmidt/httprouter" ) /* config struct: - Holds application-wide configuration settings such as: - `port`: The port number on which the server will listen. - `env`: The current environment (e.g., "development", "production"). */ type config struct { port int env string } /* applications struct: - Encapsulates the application's dependencies, including: - `config`: The application's configuration settings. - `logger`: A logger instance to handle log messages. */ type applications struct { config config logger *log.Logger } func main() { cfg := &config{ port: 4000, env: "development", } logger := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime) app := &applications{ config: *cfg, logger: logger, } router := httprouter.New() router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { w.WriteHeader(http.StatusOK) fmt.Fprintln(w, "Welcome to the Go application!") }) /* Initialize the HTTP server - Set the server's address to listen on the specified port. - Assign the router as the handler. - Configure timeouts for idle, read, and write operations. - Set up an error logger to capture server errors. */ srv := &http.Server{ Addr: fmt.Sprintf(":%d", app.config.port), Handler: router, IdleTimeout: time.Minute, ReadTimeout: 10 * time.Second, WriteTimeout: 30 * time.Second, } app.logger.Printf("Starting server on port %d in %s mode", app.config.port, app.config.env) err := srv.ListenAndServe() if err != nil { app.logger.Fatalf("Could not start server: %s", err) } }
Right now, you can test your setup by starting out server using go run ./cmd/api and sending a request to http://localhost:4000, which will return a welcome message. Next, we’ll define three additional routes to implement our core functionality:
/send-otp: This route will handle sending OTPs to users. It will generate a unique OTP, store it in Redis, and deliver it to the user.
/verify-otp: This route will verify the OTP provided by the user. It will check against the value stored in Redis to confirm the user's identity.
/login: This route will handle user login functionality once the OTP is verified and user is successfully created.
But before we continue, we need a way to store user information like phone number and their one-time password for which we need to connect to the services we defined earlier in the docker-compose.yml file.
Defining Helper Functions
Before implementing the routes, let’s define two essential helper functions. These functions will handle connections to the Redis and PostgreSQL servers, ensuring that our backend can interact with these services.
Modify the 'config' struct to store information about the services. These functions are pretty self-explanatory.
my-otp-auth-server/ ├── cmd/ │ └── api/ │ └── main.go │ └── user.go │ └── token.go ├── internal/ │ └── data/ │ ├── models.go │ └── user.go │ └── token.go ├── docker-compose.yml ├── go.mod └── Makefile
You can use these functions to establish a connection to the PostgreSQL database and Redis server after starting the services with the docker-compose up -d command.
In the next part, we'll start working on those routes we talked about earlier. This is what your main.go file should look like now.
// docker-compose.yml services: postgres: image: postgres:13 container_name: postgres environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: mysecretpassword POSTGRES_DB: cheershare ports: - "5432:5432" volumes: - postgres_data:/var/lib/postgresql/data redis: image: redis:6 container_name: redis environment: REDIS_PASSWORD: mysecretpassword ports: - "6379:6379" command: ["redis-server", "--requirepass", "mysecretpassword"] volumes: postgres_data:
The above is the detailed content of Build an OTP-Based Authentication Server with Go: Part 1. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
