Skip to content

Commit

Permalink
refactor: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkborderman committed Oct 16, 2021
1 parent f9f6d4c commit 98e5c35
Show file tree
Hide file tree
Showing 25 changed files with 834 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/
.vscode/
.github/
*.env
.gitignore
docker-compose.yml
Dockerfile
LICENSE
Makefile
README.md
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: Bug report
about: Create a report to help us improve
title: ''
labels: ''
assignees: ''

---

## Expected Behavior


## Actual Behavior


## Steps to Reproduce the Problem
1.
2.
3.

## Specifications
- Version:
- Platform:
- Subsystem:
6 changes: 6 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Fixes #

## Proposed Changes
- Changes 1
- Changes 2
- Changes 3
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build

on: push

jobs:
build:
runs-on: ubuntu-18.04

steps:
- name: Repository checkout
uses: actions/checkout@v2
- name: Login to Github Registry
uses: docker/login-action@v1
with:
registry: docker.pkg.github.com
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and publish container image
uses: docker/build-push-action@v2
with:
push: true
tags: docker.pkg.github.com/darkbordermantemplate/gin/gin
25 changes: 25 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Integration

on: push

jobs:
integration:
runs-on: ubuntu-18.04
container: golang:1.16.9

steps:
- name: Install container dependencies
run: |
apt-get update
apt-get install -y make software-properties-common gnupg2
add-apt-repository -y ppa:git-core/ppa
apt-get install -y git
- name: Repository checkout
uses: actions/checkout@v2
- name: Install service dependencies
run: |
make init
- name: Run ci bundle command
run: |
/go/bin/swag init
make ci-bundle
20 changes: 20 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Release

on:
push:
branches:
- master

jobs:
release:
runs-on: ubuntu-18.04

steps:
- name: Repository checkout
uses: actions/checkout@v2
- uses: marvinpinto/action-automatic-releases@latest
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
automatic_release_tag: latest
prerelease: true
title: Latest Build
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docs/
.vscode/
.env
vendor/
Empty file added CHANGELOG.md
Empty file.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM golang:1.16
WORKDIR /app/
COPY go.mod go.sum /app/
RUN go mod download && go install github.com/swaggo/swag/cmd/[email protected]
COPY . /app/
RUN /go/bin/swag init
RUN go build .
CMD ./practice
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2020, Darkborderman
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
SHELL := /bin/bash

.SILENT: shell
.PHONY: init

init:
go mod download
go install github.com/swaggo/swag/cmd/[email protected]

format:
gofmt -w .

gendoc:
${HOME}/go/bin/swag init

run: gendoc
go run main.go

build: gendoc
go build .

test:
go test -cover tests/*.go

ci-bundle: format test

shell:
echo "Loading .env environment variables..."
set -a
source .env
set +a
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
# gin-practice
# Gin
A RESTful API service template built with gin.

![Integration](https://github.com/DarkbordermanTemplate/gin/workflows/Integration/badge.svg)
![Build](https://github.com/DarkbordermanTemplate/gin/workflows/Build/badge.svg)
![Release](https://github.com/DarkbordermanTemplate/gin/workflows/Release/badge.svg)

## Development

### Prerequisite

| Name | Version |
| --- | --- |
| GNU make | 4.2.1 |
| go | 1.16 |

### Environment setup

0. Initialize environment variable

```
cp sample.env .env
```

1. Apply environment variables

```
make shell
```

2. Download required packages
```
make init
```

3. Start development API service
```
make run
```

4. (Optional) Create binary executable output
```
make build
```

### Formatting

This project use `gofmt` for formatting
```
make format
```

### Testing

This project use `go` for testing
```
make test
```

## Deployment

### Prerequisite

| Name | Version |
| --- | --- |
| Docker | 19.03.6 |
| docker-compose | 1.17.1 |

### Building image

This will build the image with tag `gin:latest`
```
docker-compose build
```

### Deployment

The service is deployed with `docker-compose`.

0. Start containers
```
docker-compose up
```

### Contribution
* Darkborderman/Divik([email protected])
7 changes: 7 additions & 0 deletions connection/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package connection

var MEMORY = make(map[string]int)

func Setup() {
MEMORY["apple"] = 1
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.4'

services:
gin:
container_name: gin
build: .
image: gin:latest
restart: always
env_file:
- .env
ports:
- 8080:8080
65 changes: 65 additions & 0 deletions endpoints/fruit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package endpoints

import (
"log"
"practice/connection"

"github.com/gin-gonic/gin"
)

type Fruit struct {
Name string `json:"name"`
Count int `json:"count"`
}

// @Summary Get fruit count by url queries
// @produce application/json
// @param name query string true "Fruit name"
// @Success 200 {object} Fruit
// @Router /fruit [get]
func Get_by_query(context *gin.Context) {
value, ok := connection.MEMORY[context.Query("name")]
if !ok {
context.String(400, "Bad Request")
return
}
context.JSON(200, gin.H{
"name": context.Query("name"),
"count": value,
})
}

// @Summary Get fruit count by url paths
// @produce application/json
// @param name path string true "Fruit name"
// @Success 200 {object} Fruit
// @Router /fruit/{name} [get]
func Get_by_param(context *gin.Context) {
value, ok := connection.MEMORY[context.Param("name")]
if !ok {
context.String(400, "Bad Request")
return
}
context.JSON(200, gin.H{
"name": context.Param("name"),
"count": value,
})
}

// @Summary Post fruit count by JSON
// @produce text/plain
// @Param name body Fruit true "ok"
// @Success 200 {object} string "OK"
// @Router /fruit [post]
func Post_by_JSON(context *gin.Context) {
json := Fruit{}
context.BindJSON(&json)
_, ok := connection.MEMORY[json.Name]
log.Print(ok)
if ok {
context.String(400, "Bad Request")
return
}
connection.MEMORY[json.Name] = json.Count
context.String(200, "OK")
}
11 changes: 11 additions & 0 deletions endpoints/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package endpoints

import "github.com/gin-gonic/gin"

// @Summary Health check endpoint
// @produce text/plain
// @Success 200 {object} string "OK"
// @Router /health [get]
func Health(context *gin.Context) {
context.String(200, "OK")
}
19 changes: 19 additions & 0 deletions endpoints/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package endpoints

import (
"github.com/gin-gonic/gin"
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
_ "practice/docs"
)

func Create_router() *gin.Engine {
router := gin.Default()
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
router.Handle("GET", "/fruit", Get_by_query)
router.Handle("GET", "/fruit/:name", Get_by_param)
router.Handle("POST", "/fruit", Post_by_JSON)
router.Handle("GET", "/health", Health)

return router
}
Loading

0 comments on commit 98e5c35

Please sign in to comment.