A very simple implementation of a Web Server using the Go programming language.
This project implements a basic Web Server using only the Go Standard Library. It was created for educational purposes to explore the fundamental functions provided by the standard library for networking, specifically the net/http
package. The server is capable of:
- Listening on a specific port for incoming HTTP requests.
- Processing requests and replying with corresponding HTTP responses.
The application serves requests in two different ways:
- Handler functions: Registers individual functions to process requests based on specific patterns.
- FileServer handler: Serves HTTP requests with the contents of the file system rooted at
root
.
A Postman collection is included in this repository for testing the functionality of the Web server.
To build and compile the application, you will need:
- Go version 1.18.x or higher
- Postman (optional, for testing the provided requests)
- Clone the repository:
git clone https://github.com/your-repo/mini-web-server-go.git
- Navigate to the project directory:
cd mini-web-server-go
- Initialize the Go module:
go mod init module_name go mod vendor
- Update the import reference in
cmd/web-server/golang-web-server.go
:"module_name/pkg/handler"
- Compile and build the application:
- On Unix-like systems:
./scripts/build.sh
- Alternatively, run:
go build -o ./bin/web-server cmd/web-server/golang-web-server.go
web-server
in thebin
directory. - On Unix-like systems:
./bin/web-server
Expected output:
Initializing the handler functions...
Handler functions have been initialized
Listening on localhost:8081...
./bin/web-server -use-handler-functions=false
Expected output:
Initializing handler with FileServer
Handler has been initialized
Listening on localhost:8081...
You can test the application using a Web browser or Postman.
Open your browser and go to:
http://localhost:8081
If everything is working correctly, the index page should load. Check the source code for the requests supported by the Web server.
- Import the Postman collection provided in the repository.
- Open the Golang Mini Web Server collection.
- The collection contains two folders:
- Handler Functions Implementation
- FileServer Handler Implementation
- Run the requests matching the current mode of the Web server.