Skip to content

Commit

Permalink
Merge pull request #115 from LucienShui/doc/swagger
Browse files Browse the repository at this point in the history
add swagger comment, add swagger in ci, remove aliyun in docker build…
  • Loading branch information
LucienShui authored Sep 25, 2021
2 parents 715b535 + b540255 commit 0d1340f
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 86 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to Aliyun Docker Registry
uses: docker/login-action@v1
with:
registry: registry.cn-hangzhou.aliyuncs.com
username: ${{ secrets.ALIYUN_DOCKER_REGISTRY_USERNAME }}
password: ${{ secrets.ALIYUN_DOCKER_REGISTRY_TOKEN }}
# - name: Login to Aliyun Docker Registry
# uses: docker/login-action@v1
# with:
# registry: registry.cn-hangzhou.aliyuncs.com
# username: ${{ secrets.ALIYUN_DOCKER_REGISTRY_USERNAME }}
# password: ${{ secrets.ALIYUN_DOCKER_REGISTRY_TOKEN }}

- name: Build and push
id: docker_build
Expand All @@ -61,7 +61,7 @@ jobs:
push: true
tags: |
pasteme/go-backend:beta
registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:beta
# registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:beta

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
Expand Down
40 changes: 36 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ${{ matrix.os }}

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go_version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
run: |
go get -v -t -d ./...
Expand All @@ -33,6 +33,38 @@ jobs:
run: |
GOARCH=amd64 GOOS=linux go build -v -o pastemed .
build_swagger:
strategy:
matrix:
go_version: [ 1.16 ]
os: [ ubuntu-latest ]

name: Build swagger with go ${{ matrix.go_version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go_version }}
id: go

- name: Get dependencies
run: |
go get -v -t -d ./...
go get -u github.com/swaggo/swag/cmd/swag
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Build swagger
run: |
GOARCH=amd64 GOOS=linux swag init
docker_build:
strategy:
matrix:
Expand Down
32 changes: 21 additions & 11 deletions .github/workflows/upload-to-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ jobs:
steps:

- name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v1
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go_version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1
uses: actions/checkout@v2

- name: Get dependencies
run: |
go get -v -t -d ./...
go get -u github.com/swaggo/swag/cmd/swag
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
Expand All @@ -38,6 +39,7 @@ jobs:
- name: Build
run: |
GOARCH=amd64 GOOS=linux go build -v -o pastemed .
GOARCH=amd64 GOOS=linux swag init
- name: Release Version
id: release_version
Expand All @@ -51,14 +53,22 @@ jobs:
cp config.example.json pastemed-linux-amd64
cp -r systemd pastemed-linux-amd64
tar -czvf pastemed-${{ steps.release_version.outputs.tag }}-linux-amd64.tar.gz pastemed-linux-amd64
tar -czvf pastemed-swagger-${{ steps.release_version.outputs.tag }}.tar.gz docs
- name: Upload to release
- name: Upload bin to release
uses: JasonEtco/upload-to-release@master
with:
args: pastemed-${{ steps.release_version.outputs.tag }}-linux-amd64.tar.gz application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload swagger to release
uses: JasonEtco/upload-to-release@master
with:
args: pastemed-swagger-${{ steps.release_version.outputs.tag }}.tar.gz application/octet-stream
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

docker_push:
if: github.repository == 'PasteUs/PasteMeGoBackend'
name: Build with docker on ${{ matrix.os }} and upload
Expand Down Expand Up @@ -89,12 +99,12 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to Aliyun Docker Registry
uses: docker/login-action@v1
with:
registry: registry.cn-hangzhou.aliyuncs.com
username: ${{ secrets.ALIYUN_DOCKER_REGISTRY_USERNAME }}
password: ${{ secrets.ALIYUN_DOCKER_REGISTRY_TOKEN }}
# - name: Login to Aliyun Docker Registry
# uses: docker/login-action@v1
# with:
# registry: registry.cn-hangzhou.aliyuncs.com
# username: ${{ secrets.ALIYUN_DOCKER_REGISTRY_USERNAME }}
# password: ${{ secrets.ALIYUN_DOCKER_REGISTRY_TOKEN }}

- name: Build and push
id: docker_build
Expand All @@ -111,8 +121,8 @@ jobs:
tags: |
pasteme/go-backend:latest
pasteme/go-backend:${{ steps.release_version.outputs.tag }}
registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:latest
registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:${{ steps.release_version.outputs.tag }}
# registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:latest
# registry.cn-hangzhou.aliyuncs.com/pasteus/pasteme-go-backend:${{ steps.release_version.outputs.tag }}

- name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
Expand Down
22 changes: 15 additions & 7 deletions handler/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,27 @@ import (
"net/http"
)

type Response struct {
Status int `json:"status" example:"200"`
}

func NotFoundHandler(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"status": http.StatusNotFound,
"message": ErrNoRouterFounded.Error(),
})
Error(context, http.StatusNotFound, ErrNoRouterFounded)
}

// Beat godoc
// @Summary 心跳检测
// @Description 心跳检测
// @Tags Common
// @Produce json
// @Param method query string true "方法" Enums("beat")
// @Success 200 {object} common.Response
// @Failure default {object} common.ErrorResponse
// @Router / [get]
func Beat(context *gin.Context) {
method := context.DefaultQuery("method", "none")
if method == "beat" {
context.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
})
context.JSON(http.StatusOK, Response{http.StatusOK})
} else {
NotFoundHandler(context)
}
Expand Down
23 changes: 22 additions & 1 deletion handler/common/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package common

import "errors"
import (
"errors"
"github.com/gin-gonic/gin"
)

var (
ErrNoRouterFounded = errors.New("no router founded")
)

type ErrorResponse struct {
*Response
Message string `json:"message" example:"ok"`
}

func Error(context *gin.Context, status int, err error) {
httpStatusCode := 0

if status < 1000 {
httpStatusCode = status
} else {
httpStatusCode = status / 100
}

response := ErrorResponse{&Response{Status: status}, err.Error()}
context.JSON(httpStatusCode, response)
}
24 changes: 18 additions & 6 deletions handler/paste/util.go → handler/paste/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package paste

import (
"github.com/PasteUs/PasteMeGoBackend/handler/common"
"github.com/PasteUs/PasteMeGoBackend/handler/session"
model "github.com/PasteUs/PasteMeGoBackend/model/paste"
"regexp"
Expand All @@ -11,11 +12,22 @@ var (
keyPattern = regexp.MustCompile("^[0-9a-z]{8}$")
)

type requestBody struct {
type CreateRequest struct {
*model.AbstractPaste
SelfDestruct bool `json:"self_destruct"`
ExpireMinute uint64 `json:"expire_minute"`
ExpireCount uint64 `json:"expire_count"`
SelfDestruct bool `json:"self_destruct" example:"true"` // 是否自我销毁
ExpireMinute uint64 `json:"expire_minute" example:"5"` // 创建若干分钟后自我销毁
ExpireCount uint64 `json:"expire_count" example:"1"` // 访问若干次后自我销毁
}

type CreateResponse struct {
*common.Response
Key string `json:"key" example:"a1b2c3d4"`
}

type GetResponse struct {
*common.Response
Lang string `json:"lang" example:"plain"`
Content string `json:"content" example:"Hello World!"`
}

func contains(s []string, e string) bool {
Expand All @@ -27,7 +39,7 @@ func contains(s []string, e string) bool {
return false
}

func validator(body requestBody) error {
func validator(body CreateRequest) error {
if body.Content == "" {
return ErrEmptyContent // 内容为空,返回错误信息 "empty content"
}
Expand Down Expand Up @@ -56,7 +68,7 @@ func validator(body requestBody) error {
return nil
}

func authenticator(body requestBody) error {
func authenticator(body CreateRequest) error {
if body.Username == session.Nobody {
if !body.SelfDestruct {
return ErrUnauthorized
Expand Down
2 changes: 1 addition & 1 deletion handler/paste/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var (
ErrEmptyContent = errors.New("empty content")
ErrEmptyLang = errors.New("empty lang")
ErrInvalidLang = errors.New("invalid lang")
ErrUnencryptedPassword = errors.New("unencrypted password ")
ErrWrongParamType = errors.New("wrong param type")
ErrQueryDBFailed = errors.New("query from db failed")
ErrSaveFailed = errors.New("save failed")
ErrUnauthorized = errors.New("unauthorized")
Expand Down
Loading

0 comments on commit 0d1340f

Please sign in to comment.