Skip to content

A Go web server learning project implementing RESTful APIs, JWT authentication, SQLite database, middleware, and unit tests. Built to understand Go web development concepts.

Notifications You must be signed in to change notification settings

rhmnaulia/go-web-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Web Server

A learning project to understand web development concepts in Go. This project was created to explore and implement various features commonly found in modern web applications.

Learning Objectives

This project covers several key concepts in Go web development:

  • Building RESTful APIs
  • Implementing authentication with JWT
  • Working with databases (SQLite)
  • Writing middleware
  • Handling forms and JSON data
  • Writing unit tests
  • Structuring a Go application

Features

  • RESTful API endpoints
  • JWT-based authentication
  • SQLite database integration
  • Middleware support (logging, authentication)
  • Form handling
  • Unit tests
  • Clean architecture

Prerequisites

  • Go 1.22 or higher
  • SQLite3

Installation

  1. Clone the repository:
git clone <your-repo-url>
cd go-web-server
  1. Install dependencies:
go mod tidy

Running the Server

go run main.go

The server will start on http://localhost:8080

API Endpoints

Public Endpoints

  • GET / - Home page
  • GET /about - About page
  • GET /contact - Contact form
  • POST /contact - Submit contact form
  • POST /login - Login to get JWT token

Protected Endpoints (Requires JWT Token)

  • GET /api/messages - Get all messages
  • POST /api/messages - Create a new message

Authentication

To access protected endpoints, you need to:

  1. Get a token by sending a POST request to /login:
curl -X POST http://localhost:8080/login \
  -H "Content-Type: application/json" \
  -d '{"username": "user", "password": "pass"}'
  1. Use the token in subsequent requests:
curl http://localhost:8080/api/messages \
  -H "Authorization: Bearer <your-token>"

Testing

Run all tests:

go test ./...

Project Structure

.
├── main.go           # Main application file
├── main_test.go      # Main package tests
├── auth/
│   ├── auth.go      # Authentication package
│   └── auth_test.go # Authentication tests
└── db/
    └── db.go        # Database operations

Security Notes

  • The JWT secret key is hardcoded for demonstration. In production, use environment variables.
  • The login handler accepts any non-empty username/password. In production, implement proper authentication.
  • SQLite is used for simplicity. For production, consider using a more robust database.

Learning Resources

This project was inspired by and built while learning from:

  • Go's official documentation
  • Various web development best practices
  • RESTful API design principles
  • Authentication and security concepts

Disclaimer

This is a learning project and is not intended for production use without proper security review and enhancements.

Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements. As this is a learning project, constructive feedback is highly appreciated!

About

A Go web server learning project implementing RESTful APIs, JWT authentication, SQLite database, middleware, and unit tests. Built to understand Go web development concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages