Skip to content

Commit

Permalink
BREAKING CHANGE: v1
Browse files Browse the repository at this point in the history
BREAKING CHANGES: v1
  • Loading branch information
Pradumnasaraf authored May 27, 2023
2 parents 9ed94cd + 61199c4 commit 91daf06
Show file tree
Hide file tree
Showing 27 changed files with 4,461 additions and 259 deletions.
10 changes: 6 additions & 4 deletions .env.docker
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
MONGO_URL=mongodb://go-mongodb:27017
MONGO_DB=netflix
MONGO_COLLECTION=watchlist
PORT=:8088
MONGO_URL=mongodb://db:27017
MONGO_DB=opensource
MONGO_COLLECTION=contributors
PORT=8088
BASIC_AUTH_USERNAME=opensource
BASIC_AUTH_PASSWORD=greensquare
10 changes: 6 additions & 4 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
MONGO_URL=mongodb://localhost:27017
MONGO_DB=netflix
MONGO_COLLECTION=watchlist
PORT=8088
MONGO_URI=mongodb://localhost:27017
MONGO_DB=opensource
MONGO_COLLECTION=contributors
PORT=8088
BASIC_AUTH_USERNAME=opensource
BASIC_AUTH_PASSWORD=greensquare
2 changes: 1 addition & 1 deletion .github/workflows/publish-ghcr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ jobs:
with:
context: .
push: true
tags: ghcr.io/pradumnasaraf/go-api:latest
tags: ghcr.io/pradumnasaraf/contributors:latest
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: TriPSs/[email protected]
with:
github-token: ${{ secrets.PA_TOKEN }}
version-file: './releases.json'
version-file: './releases.yaml'

- name: create release
uses: actions/create-release@v1
Expand Down
2 changes: 1 addition & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tasks:
go mod download
command: |
cp .env.example .env
go run main.go
go run server.go
image:
Expand Down
31 changes: 7 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
## GO API
## Contributors

A simple API to do CRUD operations on a database.

## Tech Stack

- [Golang](https://golang.org/)
- [MongoDB](https://www.mongodb.com/)
- [Gin](https://github.com/gin-gonic/gin)
Contributors is a GraphQL API written in Go. It uses MongoDB as a database. It is a simple API to manage Open Source Contributors and their contributions.

## Using and developing

Expand All @@ -31,33 +25,22 @@ go run main.go

The easiest way to run this project in cloud with use of [Gitpod](https://www.gitpod.io/). Just click on the button below to start the project in Gitpod.

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#github.com/Pradumnasaraf/go-api)
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#github.com/pradumnasaraf/Contributors)

### Docker Compose

Make sure you have [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) installed. And you have alredy cloned the repository.
Make sure you have [Docker](https://www.docker.com/) and [Docker Compose](https://docs.docker.com/compose/) installed. And you have alredy cloned the repository.

Then, run the following commands to start the server. It will step up a MongoDB container and a Go API container.

```bash
docker compose up
```

## API Endpoints

- `GET /` - Homepage
- `GET /api/movie/{id}` - Get a movie
- `GET /api/movies` - Get all movies
- `POST /api/movie` - Create a movie
- `PUT /api/movie/{id}` - Mark a movie as watched
- `DELETE /api/movie/{id}` - Delete a movie
- `DELETE /api/movies` - Delete all movies

```

## License
## License

This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](LICENSE) for details.

## Security
## Security

If you discover a security vulnerability within this project, please check the [security policy](SECURITY.md) for more information.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
)

func Config() {

err := godotenv.Load(".env")
if err != nil {
fmt.Println(".env file not found. Loading from os environment")
fmt.Println(".env file not found. Loading the system environment variables.")
}

}
61 changes: 0 additions & 61 deletions controller/controller.go

This file was deleted.

128 changes: 128 additions & 0 deletions database/mongo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package database

import (
"context"
"errors"
"log"
"os"
"time"

"github.com/pradumnasaraf/Contributors/config"
"github.com/pradumnasaraf/Contributors/graph/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)

var Collection *mongo.Collection
var Ctx context.Context

// MongoDB is a struct that holds the MongoDB client
type MongoDB struct {
Client *mongo.Client
}

// NewMongoDB creates a new MongoDB client and returns it
func NewMongoDB() *MongoDB {
// Load .env file
config.Config()

// Create a context
Ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Set client options
clientOptions := options.Client().ApplyURI(os.Getenv("MONGO_URI"))

// Connect to MongoDB
client, err := mongo.Connect(Ctx, clientOptions)
if err != nil {
log.Fatal(err)
}

// Check the connection
err = client.Ping(Ctx, nil)
if err != nil {
log.Fatal(err)
}

log.Println("Connected to MongoDB!")
Collection = client.Database(os.Getenv("MONGO_DB")).Collection(os.Getenv("MONGO_COLLECTION"))

return &MongoDB{
Client: client,
}
}

// ADD a new contributor
func (db *MongoDB) Add(contributor *model.Contributor) error {
_, err := Collection.InsertOne(Ctx, contributor)

if err != nil {
return errors.New("error while adding a new document. Document with the given ID may already exist")
}

return nil
}

// GET all contributors
func (db *MongoDB) GetAll() ([]*model.Contributor, error) {
cursor, err := Collection.Find(context.Background(), bson.D{{}})
if err != nil {
return nil, errors.New("error while getting the documents")
}

defer cursor.Close(Ctx)
var result []*model.Contributor

for cursor.Next(context.Background()) {
var contributor *model.Contributor
err := cursor.Decode(&contributor)
if err != nil {
return nil, errors.New("error while decoding the document")
}

result = append(result, contributor)
}

return result, nil
}

// GET a contributor by ID
func (db *MongoDB) GetByID(id string) (*model.Contributor, error) {
filter := bson.M{"_id": id}
var contributor *model.Contributor
err := Collection.FindOne(Ctx, filter).Decode(&contributor)

if err != nil {
return nil, errors.New("error while getting the document. Document with the given ID may not exist")
}

return contributor, nil
}

// UPDATE a contributor by ID
func (db *MongoDB) UpdateByID(contributor *model.Contributor) error {
filter := bson.M{"_id": contributor.ID}
update := bson.M{"$set": bson.M{"githubUsername": contributor.GithubUsername, "name": contributor.Name, "email": contributor.Email}}
result, _ := Collection.UpdateOne(context.Background(), filter, update)

if result.MatchedCount == 0 {
return errors.New("document not found. Document with the given ID may not exist")
}

return nil
}

// DELETE a contributor by ID
func (db *MongoDB) DeleteByID(id string) error {
filter := bson.M{"_id": id}
result, _ := Collection.DeleteOne(context.Background(), filter)

if result.DeletedCount == 0 {
return errors.New("error while deleting the document. Document with the given ID may not exist")
}

return nil

}
28 changes: 21 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,30 @@ services:
dockerfile: Dockerfile
image: go-api
ports:
- 8088:8088
env_file:
- .env.docker
database:
- 8080:8080
environment:
- MONGO_URI=mongodb://db:27017
- MONGO_DB=opensource
- MONGO_COLLECTION=contributors
- PORT=8080
- BASIC_AUTH_USERNAME=opensource
- BASIC_AUTH_PASSWORD=greensquare
depends_on:
- db
networks:
- go-network

db:
container_name: go-mongodb
image: mongo:5.0
ports:
- "27017:27017"
image: mongo:6.0
volumes:
- dbdata:/data/db
networks:
- go-network

volumes:
dbdata:

networks:
go-network:
driver: bridge
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module github.com/pradumnasaraf/go-api
module github.com/pradumnasaraf/Contributors

go 1.19

require (
github.com/99designs/gqlgen v0.17.31
github.com/gin-gonic/gin v1.9.0
github.com/joho/godotenv v1.5.1
go.mongodb.org/mongo-driver v1.11.2
github.com/vektah/gqlparser/v2 v2.5.1
go.mongodb.org/mongo-driver v1.11.6
)

require (
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -17,11 +20,14 @@ require (
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
Expand All @@ -36,7 +42,7 @@ require (
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
Loading

0 comments on commit 91daf06

Please sign in to comment.