A simple web server built with Go's standard library. It serves static HTML files and provides several API endpoints to handle form submissions and basic GET requests. This project demonstrates fundamental Go web development concepts, including file serving, request routing, and parsing form data.
- Serves static files from a
./staticdirectory. - Handles form submissions on multiple endpoints.
- Provides simple text-based responses for basic GET requests.
- Built entirely with the standard Go
net/httppackage.
The server is configured to serve static files from a ./static directory. For the server to function correctly, you must create this directory and place your HTML files inside it.
Follow these instructions to get the project up and running on your local machine.
- Go (version 1.18 or newer recommended)
-
Save the Code: Save the provided Go source code as
main.go. -
Create the Static Directory: In the same folder as
main.go, create a new directory namedstatic.mkdir static
-
Create HTML Files: Inside the
staticdirectory, paste the above three HTML files in thestaticdirectory which you created. -
Run the Server: Open your terminal, navigate to the project directory, and run the following command:
go run main.go
You will see the message:
Starting server at port 8080.
| Path | Method | Description |
|---|---|---|
/ |
GET |
Serves static files from the ./static directory (defaults to index.html). |
/hello |
GET |
Returns a simple hello! text response. |
/goodbye |
GET |
Returns a simple farewell text response. |
/form |
POST |
Processes form data with name and address fields and echoes them back. |
/wellbeing |
POST |
Processes form data with name, age, and health-condition fields. |
- Open your web browser and navigate to
http://localhost:8080. - You will see the
index.htmlpage. - Click the links to navigate to the forms, fill them out, and submit to see the server's response.
You can also test the form handlers directly from your terminal.
-
Test the
/formendpoint:curl -X POST -d "name=John Doe&address=123 Main St" http://localhost:8080/form -
Test the
/wellbeingendpoint:curl -X POST -d "name=Jane Doe&age=30&health-condition=Excellent" http://localhost:8080/wellbeing -
Test the
/helloendpoint:curl http://localhost:8080/hello
Thank you for exploring the project