Skip to content

Commit

Permalink
Merge pull request #105 from LucienShui/chore/remove_v2
Browse files Browse the repository at this point in the history
remove release-v prefix, remove v2 api
  • Loading branch information
LucienShui authored Aug 29, 2021
2 parents a8baed5 + a6ba65d commit 7fb8c4d
Show file tree
Hide file tree
Showing 21 changed files with 215 additions and 713 deletions.
4 changes: 2 additions & 2 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name-template: 'release-v$NEXT_PATCH_VERSION'
tag-template: 'release-v$NEXT_PATCH_VERSION'
name-template: '$NEXT_PATCH_VERSION'
tag-template: '$NEXT_PATCH_VERSION'
categories:
- title: 'Features'
labels:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/upload-to-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Release Version
id: release_version
run: |
echo ::set-output name=tag::$(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g" | sed -E "s/release-v?([0-9]+)\.([0-9]+)\.([0-9]+)(-[a-zA-Z]+(\.[0-9]+)?)?/\1.\2.\3\4/g")
echo ::set-output name=tag::$(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g")
- name: Gzip
run: |
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
- name: Release Version
id: release_version
run: |
echo ::set-output name=tag::$(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g" | sed -E "s/release-v?([0-9]+)\.([0-9]+)\.([0-9]+)(-[a-zA-Z]+(\.[0-9]+)?)?/\1.\2.\3\4/g")
echo ::set-output name=tag::$(echo "${GITHUB_REF}" | sed -e "s/refs\/tags\///g")
- name: Check out code into the Go module directory
uses: actions/checkout@v1
Expand Down
4 changes: 2 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"version": "3.3.0",
"version": "3.4.0",
"address": "0.0.0.0",
"admin_url": "",
"port": 8000,
"secret": "!!! CHANGE THIS !!!",
"log_file": "pasteme.log",
"database": {
"type": "mysql",
"username": "username",
Expand Down
8 changes: 2 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type Database struct {
type config struct {
Version string `json:"version"`
Address string `json:"address"`
AdminUrl string `json:"admin_url"` // PasteMe Admin's hostname
Port uint16 `json:"port"`
Secret string `json:"secret"`
LogFile string `json:"log_file"`
Database Database `json:"database"`
}

Expand All @@ -32,10 +32,6 @@ var Config config
func init() {
load(flag.Config)
checkVersion(Config.Version)
setDefault()
}

func setDefault() {
}

func isInArray(item string, array []string) bool {
Expand Down Expand Up @@ -66,7 +62,7 @@ func exportConfig(filename string, c config) {
zap.String("config_file", filename),
zap.String("config_version", c.Version),
zap.String("address", c.Address),
zap.String("admin_url", c.AdminUrl),
zap.String("log_file", c.LogFile),
zap.Uint16("port", c.Port),
)

Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
depends_on:
- pasteme-mysql
healthcheck:
test: ["CMD", "wget", "localhost:8000/api/?method=beat", "--output=/dev/null"]
test: ["CMD", "wget", "localhost:8000/api/v3/?method=beat", "--output=/dev/null"]
interval: 30s
timeout: 3s
retries: 3
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/ugorji/go v1.2.6 // indirect
github.com/wonderivan/logger v1.0.0
go.uber.org/zap v1.18.1
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ=
github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw=
github.com/wonderivan/logger v1.0.0 h1:Z6Nz+3SNcizolx3ARH11axdD4DXjFpb2J+ziGUVlv/U=
github.com/wonderivan/logger v1.0.0/go.mod h1:NObMfQ3WOLKfYEZuGeZQfuQfSPE5+QNgRddVMzsAT/k=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
Expand Down
24 changes: 24 additions & 0 deletions handler/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package common

import (
"github.com/gin-gonic/gin"
"net/http"
)

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

func Beat(context *gin.Context) {
method := context.DefaultQuery("method", "none")
if method == "beat" {
context.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
})
} else {
NotFoundHandler(context)
}
}
7 changes: 7 additions & 0 deletions handler/common/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package common

import "errors"

var (
ErrNoRouterFounded = errors.New("no router founded")
)
40 changes: 40 additions & 0 deletions logging/logger_func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logging

import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func exportField(requests *gin.Context) []zap.Field {
var result []zap.Field
result = append(result, zap.String("ip", requests.ClientIP()))
return result
}

func loggerPreprocess(fields []interface{}) []zap.Field {
var result []zap.Field

if len(fields) != 0 {
var beginIndex = 0
switch fields[0].(type) {
case *gin.Context:
context := fields[0].(*gin.Context)
zapField := exportField(context)
result = append(result, zapField...)
beginIndex = 1
}
for _, each := range fields[beginIndex:] {
result = append(result, each.(zap.Field))
}
}

return result
}

type loggerFunc func(string, ...interface{})

func getLogger(log func(string, ...zap.Field)) loggerFunc {
return func(msg string, a ...interface{}) {
log(msg, loggerPreprocess(a)...)
}
}
36 changes: 0 additions & 36 deletions logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
package logging

import (
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)

func exportField(requests *gin.Context) []zap.Field {
var result []zap.Field
result = append(result, zap.String("ip", requests.ClientIP()))
return result
}

func loggerPreprocess(fields []interface{}) []zap.Field {
var result []zap.Field

if len(fields) != 0 {
var beginIndex = 0
switch fields[0].(type) {
case *gin.Context:
context := fields[0].(*gin.Context)
zapField := exportField(context)
result = append(result, zapField...)
beginIndex = 1
}
for _, each := range fields[beginIndex:] {
result = append(result, each.(zap.Field))
}
}

return result
}

type loggerFunc func(string, ...interface{})

func getLogger(log func(string, ...zap.Field)) loggerFunc {
return func(msg string, a ...interface{}) {
log(msg, loggerPreprocess(a)...)
}
}


var (
logger *zap.Logger
Debug loggerFunc
Expand Down
2 changes: 1 addition & 1 deletion model/paste/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func generator(length int, zeroFirst bool) string {

func Generator(length int, zeroFirst bool, model interface{}) string {
str := generator(length, zeroFirst)
for exist(str, model) {
for Exist(str, model) {
str = generator(length, zeroFirst)
}
return str
Expand Down
7 changes: 7 additions & 0 deletions model/paste/paste.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package paste

import (
"github.com/PasteUs/PasteMeGoBackend/model/dao"
"github.com/PasteUs/PasteMeGoBackend/util"
"time"
)
Expand Down Expand Up @@ -44,3 +45,9 @@ func (paste *AbstractPaste) checkPassword(password string) error {
}
return ErrWrongPassword
}

func Exist(key string, model interface{}) bool {
count := uint8(0)
dao.DB.Model(model).Where("`key` = ?", key).Count(&count)
return count > 0
}
6 changes: 1 addition & 5 deletions model/paste/temporary.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ func (paste *Temporary) Get(password string) error {
return err
}

func exist(key string, model interface{}) bool {
count := uint8(0)
dao.DB.Model(model).Where("`key` = ?", key).Count(&count)
return count > 0
}

18 changes: 4 additions & 14 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package router
import (
"fmt"
"github.com/PasteUs/PasteMeGoBackend/flag"
"github.com/PasteUs/PasteMeGoBackend/handler/common"
"github.com/PasteUs/PasteMeGoBackend/handler/paste"
"github.com/PasteUs/PasteMeGoBackend/handler/session"
"github.com/PasteUs/PasteMeGoBackend/logging"
v2Handler "github.com/PasteUs/PasteMeGoBackend/v2/handler"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
Expand All @@ -21,20 +21,10 @@ func init() {

api := router.Group("/api")
{
api.GET("/", v2Handler.Beat) // 心跳检测

v2 := api.Group("/v2")
{
// 访问未加密的 Paste,token 为 <Paste ID>
// 访问加密的 Paste,token 为 <Paste ID>,<Password>
v2.GET("/:token", v2Handler.Query)
v2.POST("/", v2Handler.PermanentCreator) // 创建一个永久的 Paste, key 是自增键
v2.POST("/once", v2Handler.ReadOnceCreator) // 创建一个阅后即焚的 Paste, key 是随机的
v2.PUT("/:key", v2Handler.TemporaryCreator) // 创建一个阅后即焚的 Paste, key 是指定的
}

v3 := api.Group("/v3")
{
v3.GET("/", common.Beat)

s := v3.Group("/session")
{
s.POST("", session.AuthMiddleware.LoginHandler) // 创建 Session(登陆)
Expand All @@ -58,7 +48,7 @@ func init() {
}
}

router.NoRoute(v2Handler.NotFoundHandler)
router.NoRoute(common.NotFoundHandler)
}

func Run(address string, port uint16) {
Expand Down
Loading

0 comments on commit 7fb8c4d

Please sign in to comment.