Skip to content

Commit

Permalink
Auth (#4)
Browse files Browse the repository at this point in the history
* adding auth

* mocking oauth and working on tests

* oauth test - missing name

* adding auth

* mocking oauth and working on tests

* oauth test - missing name

* Readme, license and contributing

---------

Co-authored-by: Nico Bistolfi <[email protected]>
  • Loading branch information
nicobistolfi and nicobistolfi authored Sep 2, 2024
1 parent 781d1ee commit 4df0080
Show file tree
Hide file tree
Showing 28 changed files with 1,097 additions and 241 deletions.
37 changes: 37 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# OAuth 2.0 / OpenID Connect Configuration
OIDC_ISSUER=https://accounts.google.com
OAUTH_CLIENT_ID=your_oauth_client_id
OAUTH_CLIENT_SECRET=your_oauth_client_secret
OAUTH_REDIRECT_URL=http://localhost:8080/auth/callback

# JWT Configuration
JWT_SECRET=your_jwt_secret_key
JWT_EXPIRATION_MINUTES=60

# API Key Configuration
VALID_API_KEY=your_api_key

# Server Configuration
PORT=8080

# Database Configuration (if applicable)
# DB_HOST=localhost
# DB_PORT=5432
# DB_NAME=your_database_name
# DB_USER=your_database_user
# DB_PASSWORD=your_database_password

# Redis Configuration (if applicable)
# REDIS_HOST=localhost
# REDIS_PORT=6379
# REDIS_PASSWORD=

# Logging Configuration
LOG_LEVEL=info

# Rate Limiting Configuration
RATE_LIMIT_REQUESTS=10
RATE_LIMIT_DURATION=1s

# Other Configuration Options
# Add any other environment-specific configurations here
59 changes: 59 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Contributing to Go API Boilerplate

We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's:

- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features
- Becoming a maintainer

## We Develop with Github

We use github to host code, to track issues and feature requests, as well as accept pull requests.

## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests

Pull requests are the best way to propose changes to the codebase. We actively welcome your pull requests:

1. Fork the repo and create your branch from `main`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. Issue that pull request!

## Any contributions you make will be under the MIT Software License

In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](https://github.com/yourusername/go-api-boilerplate/issues)

We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/yourusername/go-api-boilerplate/issues/new); it's that easy!

## Write bug reports with detail, background, and sample code

**Great Bug Reports** tend to have:

- A quick summary and/or background
- Steps to reproduce
- Be specific!
- Give sample code if you can.
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

People *love* thorough bug reports. I'm not even kidding.

## Use a Consistent Coding Style

* 2 spaces for indentation rather than tabs
* You can try running `go fmt` for style unification

## License

By contributing, you agree that your contributions will be licensed under its MIT License.

## References

This document was adapted from the open-source contribution guidelines for [Facebook's Draft](https://github.com/facebook/draft-js/blob/a9316a723f9e918afde44dea68b5f9f39b7d9b00/CONTRIBUTING.md)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 [Your Name or Your Organization's Name]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
181 changes: 36 additions & 145 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go API Project Structure
# Go API Boilerplate

This repository contains a structured Go project for developing a robust and scalable API. The project is organized to promote clean architecture, separation of concerns, and ease of testing and deployment.
This repository provides a structured Go project for building scalable APIs. It emphasizes clean architecture, separation of concerns, and ease of testing and deployment.

## Project Structure

Expand Down Expand Up @@ -36,174 +36,65 @@ go-boilerplate/
└── README.md
```

### Directory and File Descriptions
### Key Components

#### `cmd/`
Contains the main applications for this project. The `api/` subdirectory is where the main.go file for starting the API server resides.
- `cmd/api/main.go`: Application entry point
- `internal/`: Core application logic
- `pkg/`: Reusable, non-project-specific code
- `tests/`: Comprehensive test suite
- `deployments/`: Deployment configurations for various platforms
- `docs/`: Docusaurus-based documentation

- `api/main.go`: Entry point of the application. Initializes and starts the API server.
## Functionality

#### `internal/`
Houses packages that are specific to this project and not intended for external use.
This boilerplate provides:

- `api/`: Contains API-specific code.
- `handlers/`: Request handlers for each API endpoint.
- `middleware/`: Custom middleware functions.
- `routes.go`: Defines API routes and links them to handlers.
- `config/`: Configuration management for the application.
- `models/`: Data models and DTOs (Data Transfer Objects).
- `repository/`: Data access layer, interfacing with the database.
- `service/`: Business logic layer, implementing core functionality.
1. A structured API server setup
2. Configuration management
3. Database integration (via repository layer)
4. Business logic separation (service layer)
5. Middleware support
6. Comprehensive testing framework
7. Multiple deployment options

#### `pkg/`
Shared packages that could potentially be used by external projects. Place reusable, non-project-specific code here.
## Deployment Options

#### `scripts/`
Utility scripts for development, CI/CD, database migrations, etc.
The boilerplate supports multiple deployment strategies:

#### `tests/`
Contains test files separated into integration and unit tests.
1. Docker: Containerization for consistent environments
2. Kubernetes: Orchestration for scalable deployments
3. Serverless: Cloud function deployment for serverless architectures

- `integration/`: API-level and end-to-end tests.
- `unit/`: Unit tests for individual functions and methods.
Deployment configurations are located in the `deployments/` directory.

#### `deployments/`
Configuration files and scripts for deploying the application.
## Documentation

- `docker/`: Dockerfile and related configurations for containerization.
- `kubernetes/`: Kubernetes manifests for orchestration.
- `serverless/`: Serverless configuration files for cloud function deployment.
To run the documentation locally:

#### `docs/`
Project documentation, API specifications, and any other relevant documentation. This directory contains a Docusaurus project for easy-to-navigate and visually appealing documentation.

To run and view the documentation locally:

1. Navigate to the `docs/` directory:
```
cd docs
1. Navigate to the `docs/` directory
2. Run:
```

2. Install the necessary dependencies:
```
npm install
npm install && npm start
```
3. Open `http://localhost:3000` in your browser

3. Start the Docusaurus development server:
```
npm run start
```

4. Open your web browser and visit `http://localhost:3000` to view the documentation.

The documentation includes:
- API specifications
- Getting started guide
- Architecture overview
- Deployment instructions
- Contributing guidelines

To build the documentation for production:
```
npm run build
```

This will generate static content in the `build` directory, which can be served using any static content hosting service.

#### Root Files
- `.gitignore`: Specifies intentionally untracked files to ignore.
- `go.mod` and `go.sum`: Go module files for dependency management.
- `Makefile`: Defines commands for building, testing, and deploying the application.
- `LICENSE`: Contains the MIT License text.
- `README.md`: This file, providing an overview of the project structure.
This will start a Docusaurus site with comprehensive project documentation.

## Getting Started

1. Clone this repository.
2. Navigate to the project root.
3. Run `go mod tidy` to ensure all dependencies are correctly installed.
4. Use the provided Makefile commands for common tasks:
1. Clone this repository
2. Run `go mod tidy` to install dependencies
3. Use the provided Makefile commands for common tasks:
- `make build`: Build the application
- `make test`: Run all tests
- `make run`: Run the application locally

## Development Workflow

1. Implement new features or bug fixes in the appropriate packages under `internal/`.
2. Write unit tests in the same package as the code being tested.
3. Write integration tests in the `tests/integration/` directory.
4. Update API documentation in the `docs/` directory as necessary.
5. Use the `scripts/` directory for any automation tasks.
6. Update deployment configurations in `deployments/` if there are infrastructure changes.

## Deployment

This project supports multiple deployment options:

### Docker

Refer to the `deployments/docker/` directory for Docker configurations. To build and run the Docker container:

1. Build the Docker image:
```
docker build -t go-rest-api -f deployments/docker/Dockerfile .
```
2. Run the container:
```
docker run -p 8080:8080 go-rest-api
```

### Kubernetes

Kubernetes manifests are available in the `deployments/kubernetes/` directory. To deploy to a Kubernetes cluster:

1. Apply the manifests:
```
kubectl apply -f deployments/kubernetes/
```

### Serverless

For serverless deployment, we use the Serverless Framework. Configuration files are located in the `deployments/serverless/` directory.

1. Install the Serverless Framework:
```
npm install -g serverless
```
2. Deploy the application:
```
cd deployments/serverless
serverless deploy
```

Ensure to update these configurations as the application evolves. For more detailed deployment instructions, refer to the respective README files in each deployment directory.
For more detailed information, please refer to the full documentation in the `docs/` directory.directory.

## Contributing

Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Testing

This project employs a comprehensive testing strategy to ensure reliability, performance, and security. For detailed information on our testing approach, please refer to the following documentation:

- [Testing Strategy](docs/docs/tests/testing-strategy.md)
- [Unit Testing](docs/docs/tests/unit-testing.md)
- [Integration Testing](docs/docs/tests/integration-testing.md)
- [Performance Testing](docs/docs/tests/performance-testing.md)
- [Security Testing](docs/docs/tests/security-testing.md)
- [End-to-End Testing](docs/docs/tests/e2e-testing.md)

To run the tests, you can use the following make commands:

- Run unit tests: `make test-unit`
- Run integration tests: `make test-integration`
- Run performance tests: `make test-performance`
- Run security tests: `make test-security`
- Run end-to-end tests: `make test-e2e`
- Run all tests: `make test-all`

For more information on these commands, refer to the Makefile in the root directory of the project.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
24 changes: 23 additions & 1 deletion deployments/serverlesss/handlers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@ import (
"log"

"go-boilerplate/internal/api"
"go-boilerplate/internal/config"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
ginadapter "github.com/awslabs/aws-lambda-go-api-proxy/gin"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

var ginLambda *ginadapter.GinLambda

func init() {
// stdout and stderr are sent to AWS CloudWatch Logs
log.Printf("Gin cold start")
r := api.SetupRouter()

// Load configuration
cfg, err := config.LoadConfig()
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}

// Initialize logger
logger, err := zap.NewProduction()
if err != nil {
log.Fatalf("Failed to initialize logger: %v", err)
}
defer logger.Sync()

// Create a new Gin router
r := gin.New()

// Setup router with middleware and routes
api.SetupRouter(r, cfg, logger)

ginLambda = ginadapter.New(r)
}

Expand Down
Loading

0 comments on commit 4df0080

Please sign in to comment.