Lightweight example Go HTTP server used for demonstrating a small API with login/logout endpoints and static file serving.
This project is a minimal demo showing how to:
- Serve static files from the project directory
- Expose simple JSON API endpoints for login and logout
- Use a small internal toolkit package for JSON serialization and helpers
main.go
- the HTTP server and route handlersindex.html
- an example static file served at/
go.mod
,go.sum
- Go module files
- Go 1.18+ (module-aware Go)
- Network port 8080 available
This project depends on github.com/Go-Golang-Training/toolkit/v2
. Make sure your module proxy can fetch it, or replace the import with your local toolkit copy if needed.
From the project root run:
go mod tidy
go run ./
The server will start on port :8080
and serve static files from the project directory. You should see a log line like:
Starting server (Application) on :8080
GET /
- serves static files (e.g.,index.html
)POST /api/login
- accepts JSON payload withusername
andpassword
POST /api/logout
- returns a JSON logout confirmation
Request (JSON):
{
"username": "me@here.com",
"password": "verysecret"
}
Successful response (HTTP 202 Accepted):
{
"error": false,
"message": "You have been logged in"
}
Failed response (HTTP 401 Unauthorized):
{
"error": true,
"message": "Invalid credentials"
}
Notes:
- The example credentials are hard-coded in
main.go
for demonstration: usernameme@here.com
and passwordverysecret
.
Request: POST /api/logout
(no body required)
Response (HTTP 202 Accepted):
{
"message": "You have been logged out"
}
Login (successful):
curl -s -X POST http://localhost:8080/api/login \
-H "Content-Type: application/json" \
-d '{"username":"me@here.com","password":"verysecret"}'
Logout:
curl -s -X POST http://localhost:8080/api/logout
- This repository is a teaching/demo project. It intentionally keeps logic simple and contains hard-coded credentials. Do not use as-is in production.
- Suggested improvements: add environment-based configuration, proper authentication/storage for sessions, input validation, and tests.
If you'd like, I can also add a small Makefile, a Dockerfile, or create unit tests for the handlers.