{"id":26455035,"url":"https://github.com/revmax-creator/sample-api-in-go","last_synced_at":"2025-10-09T21:11:40.756Z","repository":{"id":282220138,"uuid":"947858486","full_name":"RevMax-creator/Sample-API-in-go","owner":"RevMax-creator","description":"This sample API is ideal for developers who want to learn how to build maintainable and efficient RESTful services in Go using a modern, modular approach. Feel free to explore, modify, and build upon this project to suit your own API development needs.","archived":false,"fork":false,"pushed_at":"2025-03-13T11:41:46.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-13T12:29:59.483Z","etag":null,"topics":["api","api-server","apiexample","go","go-api-example","go-apis","goapi","golang","rest-api","restapi","restful-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RevMax-creator.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-13T11:05:33.000Z","updated_at":"2025-03-13T11:45:28.000Z","dependencies_parsed_at":"2025-03-13T12:40:46.124Z","dependency_job_id":null,"html_url":"https://github.com/RevMax-creator/Sample-API-in-go","commit_stats":null,"previous_names":["revmax-creator/sample-api-in-go"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/RevMax-creator/Sample-API-in-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RevMax-creator%2FSample-API-in-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RevMax-creator%2FSample-API-in-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RevMax-creator%2FSample-API-in-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RevMax-creator%2FSample-API-in-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RevMax-creator","download_url":"https://codeload.github.com/RevMax-creator/Sample-API-in-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RevMax-creator%2FSample-API-in-go/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263247414,"owners_count":23436843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","api-server","apiexample","go","go-api-example","go-apis","goapi","golang","rest-api","restapi","restful-api"],"created_at":"2025-03-18T20:29:44.874Z","updated_at":"2025-10-09T21:11:35.735Z","avatar_url":"https://github.com/RevMax-creator.png","language":"Go","readme":"# Sample API in Go\n\nThis repository provides a sample implementation of a RESTful API built with Go. It demonstrates a modular, clean architecture and best practices for building scalable web services. The project is ideal for developers who want to learn how to structure a Go API with proper routing, middleware, error handling, and database abstraction.\n\n---\n\n## Table of Contents\n\n- [Features](#features)\n- [Project Structure](#project-structure)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Endpoints](#endpoints)\n- [Contributing](#contributing)\n- [License](#license)\n\n---\n\n## Features\n\n- **Clean Architecture \u0026 Modular Design:**  \n  The code is organized into distinct directories to separate API specifications, command-line entry, handlers, middleware, and tools. This makes the codebase easier to maintain and extend.\n\n- **Routing \u0026 Middleware:**  \n  Uses the [Chi](https://github.com/go-chi/chi) router for lightweight and flexible routing. Custom middleware handles authentication and error responses.\n\n- **Database Abstraction:**  \n  Implements a database interface along with a mock database (`mockdb`) for demonstration purposes. This allows for easy swapping of database implementations without altering the business logic.\n\n- **JSON Handling \u0026 Error Responses:**  \n  Includes examples for decoding JSON requests, encoding JSON responses, and consistent error handling using custom error handler functions.\n\n- **External Package Integration:**  \n  Utilizes popular Go packages such as `logrus` for advanced logging and `gorilla/schema` for URL query parameter decoding.\n\n- **Generics \u0026 Modern Go Features:**  \n  Demonstrates the usage of generics (where applicable) and modern Go features to keep the code concise and type-safe.\n\n---\n\n## Project Structure\n\n```\nAPI/\n├── api\n│   └── api.go                # API specifications: request/response structs and helper functions\n├── cmd\n│   └── api\n│       └── main.go           # Application entry point: sets up the router and starts the server\n├── internal\n│   ├── handlers              # API endpoint handlers\n│   │   ├── api.go            # Common handler functions and endpoint route definitions\n│   │   └── get_coin_balance.go  # Handler for the GET /account/coins endpoint\n│   ├── middleware            # Custom middleware\n│   │   └── authorization.go  # Authorization middleware for endpoint protection\n│   └── tools                 # Database interface and mock database implementation\n│       ├── database.go       # Database interface definitions and constructor\n│       └── mockdb.go         # Mock database implementation for testing/demo purposes\n├── go.mod                    # Module definition\n└── go.sum                    # Dependency checksums\n```\n\n---\n\n## Installation\n\n1. **Clone the Repository:**\n\n   ```bash\n   git clone https://github.com/RevMax-creator/sample-api-in-go.git\n   cd sample-api-in-go\n   ```\n\n2. **Install Dependencies:**\n\n   The repository uses Go modules. Ensure you have Go 1.18 or later installed, then run:\n\n   ```bash\n   go mod tidy\n   ```\n\n---\n\n## Usage\n\n1. **Build \u0026 Run:**\n\n   You can run the API server using the following command:\n\n   ```bash\n   go run cmd/api/main.go\n   ```\n\n2. **Server Details:**\n\n   The server starts on `localhost:8080`. You should see logs indicating that the service has started.\n\n3. **Testing the API:**\n\n   Use tools like [Postman](https://www.postman.com/) or `curl` to test the endpoints. For example, to get the coin balance for a user named \"Alex\":\n\n   ```bash\n   curl -X GET \"http://localhost:8080/account/coins?username=alex\" -H \"Authorization: 123ABC\"\n   ```\n\n   A successful response will include a JSON object with the coin balance and a status code.\n\n---\n\n## Endpoints\n\n### GET `/account/coins`\n\n- **Description:**  \n  Returns the coin balance for a given user account.\n\n- **Parameters:**\n  - Query parameter `username`: The username of the account.\n  - Header `Authorization`: The token required for authentication.\n\n- **Response:**\n\n  ```json\n  {\n    \"code\": 200,\n    \"balance\": 100\n  }\n  ```\n\n- **Error Handling:**  \n  If the username or token is missing, or if authentication fails, the API returns an appropriate error message using standardized error responses.\n\n---\n\n## Contributing\n\nContributions are welcome! Please follow these steps:\n\n1. Fork the repository.\n2. Create a new branch with your feature or bugfix.\n3. Commit your changes.\n4. Submit a pull request describing your changes.\n\n---\n\n## License\n\nThis project is licensed under the Creative Commons Modified License. See the [LICENSE](https://github.com/RevMax-creator/Sample-API-in-go?tab=License-1-ov-file) file for details.\n\n---\n\nFeel free to explore, modify, and extend this sample API to suit your needs. Happy coding!","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevmax-creator%2Fsample-api-in-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frevmax-creator%2Fsample-api-in-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevmax-creator%2Fsample-api-in-go/lists"}