This project includes a simple Go web server that serves static files.
-
Make sure you have Go installed (https://golang.org/dl/).
-
By default, the server will look for a
config.jsonfile in the project root. Exampleconfig.json:{ "homedir": "./html", "port": "80", "error_pages": { "404": "./html/404.html", "500": "./html/500.html" } }homedir: Directory to serve static files from (default:./public)port: Port to serve HTTP on (default:80)error_pages: Paths to custom error pages for 404 and 500 errors (optional)
-
To run the server:
go run main.go
- You can specify a different config file with the
-configflag:go run main.go -config=/path/to/your/config.json
- You can override config file values with flags:
Flags take precedence over config file values.
go run main.go -homedir=/tmp/files -port=8080
- You can specify a different config file with the
-
Place your static files (e.g.,
index.html,picture.jpg,file.js) in the home directory. -
Open your browser and go to
http://localhost:<port>to see the server response.
- You can specify custom error pages for 404 (Not Found) and 500 (Internal Server Error) in
config.jsonunder theerror_pagesfield. - If a requested file is not found, the server will serve the specified 404 page. If the 404 page is missing, a default message is shown.
- If a server error occurs, the server will serve the specified 500 page (future support for 500 errors).
- Example error pages are provided in the
publicfolder.
- The default directory for static files is
./html. - An example
index.htmlis provided in thehtmlfolder. - You can add more files (images, JavaScript, etc.) to this directory to have them served by the web server.