A sample web application built with Golang and the Gin framework, showcasing a RESTful API with basic CRUD operations.
The go-web-app
is a simple web server application developed using Go and the Gin framework. It serves as a demonstration of setting up a RESTful API with basic CRUD functionality and other essential web server capabilities.
- RESTful API with Gin framework
- Basic CRUD operations for managing resources
- Configurable environment settings
- Simple logging and error handling mechanisms
- Built-in testing support
Ensure you have the following installed:
-
Clone the Repository
git clone https://github.com/yourusername/go-web-app.git cd go-web-app
-
Install Dependencies
Download and install the necessary Go modules:
go mod tidy
-
Build the Application
Compile the application into a binary:
go build -o server main.go
-
Run the Application
Start the server:
./server
The server defaults to port
8080
. You can adjust this in the configuration file if needed.
With the server running, you can interact with the API using tools such as curl
or Postman. Here are example commands for common operations:
-
GET
/api/endpoint
curl -X GET http://localhost:8080/api/endpoint
-
POST
/api/endpoint
curl -X POST http://localhost:8080/api/endpoint -d '{"key":"value"}' -H "Content-Type: application/json"
-
PUT
/api/endpoint/{id}
curl -X PUT http://localhost:8080/api/endpoint/1 -d '{"key":"newvalue"}' -H "Content-Type: application/json"
-
DELETE
/api/endpoint/{id}
curl -X DELETE http://localhost:8080/api/endpoint/1
GET /api/endpoint
- Retrieve a list of items.POST /api/endpoint
- Create a new item.PUT /api/endpoint/{id}
- Update an existing item by its ID.DELETE /api/endpoint/{id}
- Delete an item by its ID.
Configuration can be managed via environment variables or a configuration file.
PORT
- Port on which the server listens (default:8080
).DATABASE_URL
- Connection URL for the database (if applicable).
Create a .env
file in the root directory to define these variables:
PORT=8080
DATABASE_URL=your-database-url
Alternatively, you can use a configuration file to set application-specific settings. Refer to the configuration documentation for details.
To run tests for the application, use:
go test ./...
For verbose test output:
go test -v
Contributions are encouraged! Please follow these steps:
- Fork the repository.
- Create a new feature branch (
git checkout -b feature-branch
). - Commit your changes (
git commit -am 'Add new feature'
). - Push your branch (
git push origin feature-branch
). - Submit a Pull Request.
Please ensure that your code adheres to the project's style guidelines and includes tests for new features.