Skip to content

RecyThing/RecyThing-API

Repository files navigation

codecov

Nama Alternatif


Table of Contents

Folder Structure

├── app                            
│   ├── config                      # configuration connection any database
│   ├── database                    # any database connection
│   └── route                      
├── features
│   └── featureA
│       ├── dto                    
│       │   ├── request
│       │   │   ├── mapping.go
│       │   │   └── request.go
│       │   └── response
│       │       ├── mapping.go
│       │       └── response.go
│       ├── entity
│       │   ├── entity.go           
│       │   ├── interface.go        # interface contract for all function repository and service 
│       │   └── mapping.go          # mapping struct core to model or model to core
│       ├── handler
│       │   └── handler.go          # manages HTTP requests and responses.map HTTP input to calls to functions in the service layer.
│       ├── model
│       │   └── hook.go        
│       │   └── model.go            # structure database 
│       ├── repository
│       │   └── respository.go      # query for manipulating data
│       └── service
│           └── service.go          # contains the core business logic of the application and validation
├── utils                           # providing commonly used helper functions across different parts of the project

Tech Stack

header header-sub-header-body-text-header-2 header-sub-header-body-text-header-3 Static Badge Static Badge Static Badge Static Badge Static Badge Static Badge Static Badge Static Badge

API Documentation

We use Swagger UI as the API documentation, running on Docker with JSON configuration located on an external server.

Pull a pre-built docker image of the swagger-ui directly from Docker Hub:

docker pull swaggerapi/swagger-ui

Provide a URL to a swagger.json on an external host:

docker run -p 80:8080 -e SWAGGER_JSON_URL=https://recything.apicode.my.id/swagger/apidoc.json swaggerapi/swagger-ui

Running on cloud run :

  • Pull swaggerapi/swagger-ui,
    docker pull swaggerapi/swagger-ui
    docker tag swaggerapi/swagger-ui gcr.io/$PROJECT_ID/swagger-ui  #change $PROJECT_ID to your project
    
  • Push to container registry
    docker push gcr.io/$PROJECT_ID/swagger-ui #change $PROJECT_ID to your project
    
  • Deploy cloud run
    gcloud run deploy swagger-ui \
    --image=gcr.io/$PROJECT_ID/swagger-ui:latest \                                         #change $PROJECT_ID to your project
    --platform=managed \
    --allow-unauthenticated \
    --set-env-vars=SWAGGER_JSON_URL=https://recything.apicode.my.id/swagger/apidoc.json    #change to your external json
    

Unit Testing

Using testify for testing code and generate mock with Mockery.

  • Install Mockery

    go install github.com/vektra/mockery/v2@v2.38.0
    
  • Generate Mock Auto Detect All Interface

    mockery -all   #The generated mocks will be available in a single file named 'mocks'.
    
  • Test Command

    Running test and cover on directory test :

    go test -v -cover   
    

    Running Test and cover with html view :

    go test -coverprofile=cover.out && go tool cover -html=cover.out
    

    Running Test All Sub Directories :

    go list ./... | grep service | xargs -n1 go test -cover  
    

    #change grep with your folder name (e.g grep handler), It is recommended to use the same folder name. view like this :

    Screenshot from 2023-12-17 20-10-40

  • Configuration Codecov

    Connect codecov first here, Look YAML configuration here

Running Project

Start project with Go :

go run main.go

How to deploy to Cloud Run

  • First, build your Docker image and push it to the registry, just like you did with the Swagger UI configuration.

  • Deploy cloud run

    gcloud run deploy <Service-Name> \                            # e.g  gcloud run deploy recything
    --image=gcr.io/$PROJECT_ID/<Your-image>:latest \              #change $PROJECT_ID to your project
    --platform=managed \
    --allow-unauthenticated \
    --set-env-vars=MYSQL_PORT=3306                                #change to your env
    --set-env-vars=SERVER_PORT=  
    --set-env-vars=STORAGE=   
    
  • Custom Domain

    Mapping domain with any DNS management , configuration mapping here. Click Add Mapping, And you can choose from 3 different ways of mapping domains, either using a custom domain with a load balancer, a custom domain with Cloud Run, or a custom domain with Firebase Hosting.

    Screenshot from 2023-12-18 11-06-44

    You will receive DNS records and add that configuration to the DNS Management in the platform where your domain is located

CI/CD Platform

Cloud Build is a Google Cloud Platform CI/CD platform that is easily implemented with GitHub. Configure trigger here. We use cloudbuid.yaml for trigger CI/CD :

Screenshot from 2023-12-18 10-50-19

Team Back-End

Name University
Hanief Fathul Bahri Ahmad Universitas Nusa Mandiri
Tiara Juli Arsita Universitas Tadulako
Juan Azhar Adviseta Setiawan Universitas Pembangunan Nasional Veteran Yogyakarta
Al Hilaluddin Universitas Muslim Indonesia
Stanley Universitas Mikroskil

References

  • Echo : High performance, extensible, minimalist Go web framework
  • Gorm : GORM facilitates the interaction with relational databases, simplifying the process of accessing and managing data
  • Swagger-Ui : Api Documentation
  • Go-openai : Configuration Open-Ai
  • Gorilla/schema : Bind request struct
  • Cloud Run : Deployment Platform
  • Govalidator : Package of validators and sanitizers for strings, numerics, slices and structs
  • Codecov : Coverage Reporting
  • Testify : A toolkit with common assertions and mocks that plays nicely with the standard library
  • Mockery : A mock code autogenerator for Go Interface