Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: core-api add publish event report api #88

Merged
51 changes: 51 additions & 0 deletions src/core-api/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
run:
# default concurrency is a available CPU number
concurrency: 4
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 2m
# exit code when at least one issue was found, default is 1
issues-exit-code: 1
# include test files or not, default is true
tests: false
# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true

skip-files:
- ".*/mock/.*.go"
- ".*testing.go"

linters:
# enable-all: true
# disable-all: true
disable:
- errcheck
enable:
- nilerr
- nakedret
- lll
- gofmt
- gocritic
- gocyclo
- whitespace
- stylecheck
- misspell
- goimports
# - bodyclose
# - nestif
# - gofumpt
# - godox
# - wsl
# - funlen
# - golint
# - cyclop
fast: false

linters-settings:
stylecheck:
checks: ["-ST1000", "-ST1016", "-ST1020", "-ST1021", "-ST1022"]
goimports:
# A comma-separated list of prefixes, which, if set, checks import paths
# with the given prefixes are grouped after 3rd-party packages.
# Default: ""
local-prefixes: "core"
5 changes: 4 additions & 1 deletion src/core-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ init:
go install mvdan.cc/gofumpt@latest
# for golines
go install github.com/segmentio/golines@latest
# for goimports
go install -v github.com/incu6us/goimports-reviser/v3@latest

.PHONY: dep
dep:
Expand All @@ -38,7 +40,8 @@ check-license:
.PHONY: fmt
fmt:
golines ./ -m 120 -w --base-formatter gofmt --no-reformat-tags
gofumpt -l -w .
gofumpt -l -w .
goimports-reviser -rm-unused -set-alias -format ./...


.PHONY: test
Expand Down
6 changes: 3 additions & 3 deletions src/core-api/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ package cmd
import (
"fmt"

sentry "github.com/getsentry/sentry-go"
"github.com/spf13/viper"

"core/pkg/config"
"core/pkg/database"
"core/pkg/logging"
"core/pkg/metric"
"core/pkg/trace"

"github.com/getsentry/sentry-go"
"github.com/spf13/viper"
)

var globalConfig *config.Config
Expand Down
6 changes: 2 additions & 4 deletions src/core-api/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ import (
"fmt"
"os"

"core/pkg/server"

_ "github.com/go-sql-driver/mysql"
"github.com/spf13/cobra"
"github.com/spf13/viper"

// Register mysql resource
_ "github.com/go-sql-driver/mysql"
"core/pkg/server"
)

// rootCmd represents the base command when called without any subcommands
Expand Down
2 changes: 1 addition & 1 deletion src/core-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/onsi/ginkgo/v2 v2.9.5
github.com/onsi/gomega v1.27.6
github.com/prometheus/client_golang v1.15.0
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
Expand Down Expand Up @@ -67,7 +68,6 @@ require (
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/api/microgateway/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"database/sql"
"errors"

"github.com/gin-gonic/gin"

"core/pkg/service"
"core/pkg/util"

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

type queryPermissionSerializer struct {
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/api/microgateway/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (
"database/sql"
"errors"

"github.com/gin-gonic/gin"

"core/pkg/service"
"core/pkg/util"

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

type queryPublicKeySerializer struct {
Expand Down
62 changes: 62 additions & 0 deletions src/core-api/pkg/api/microgateway/publish_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* TencentBlueKing is pleased to support the open source community by making
* 蓝鲸智云 - API 网关(BlueKing - APIGateway) available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* We undertake not to change the open source license (MIT license) applicable
* to the current version of the project delivered to anyone in the future.
*/

package microgateway

import (
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved

"core/pkg/service"
"core/pkg/util"
)

type reportPublishEventSerializer struct {
BkGatewayName string `json:"bk_gateway_name" binding:"required" example:"benchmark"`
BkStageName string `json:"bk_stage_name" binding:"required" example:"dev"`
Name string `json:"name" binding:"required" example:"generate_release_task"`
Status string `json:"status" binding:"required" example:"success" `
Detail map[string]interface{} `json:"detail"`
}

// ReportPublishEvent report publish event
func ReportPublishEvent(c *gin.Context) {
var query reportPublishEventSerializer
if err := c.ShouldBindJSON(&query); err != nil {
util.BadRequestErrorJSONResponse(c, util.ValidationErrorMessage(err))
return
}
svc := service.NewPublishEventService()
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
publishID := cast.ToInt64(c.Param("publish_id"))
event := service.Event{
Gateway: query.BkGatewayName,
Stage: query.BkGatewayName,
Name: query.Name,
Status: query.Status,
PublishID: publishID,
DetailMap: query.Detail,
}
err := svc.Report(c.Request.Context(), event)
if err != nil {
util.SystemErrorJSONResponse(c, err)
return
}
util.SuccessJSONResponse(c, gin.H{
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
"result": "report success",
})
}
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/app_gateway_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"errors"
"strconv"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
"core/pkg/logging"

"github.com/TencentBlueKing/gopkg/cache"
)

// AppGatewayPermissionKey is the key of app-gateway permission
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/app_gateway_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"
"time"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/stretchr/testify/assert"

"core/pkg/database/dao"
)

func TestAppGatewayPermissionKey_Key(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/app_resource_permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"errors"
"strconv"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
"core/pkg/logging"

"github.com/TencentBlueKing/gopkg/cache"
)

// AppResourcePermissionKey is the key of app-resource permission
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/app_resource_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"
"time"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/stretchr/testify/assert"

"core/pkg/database/dao"
)

func TestAppResourcePermissionKey_Key(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"context"
"errors"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
)

// GatewayNameKey is the key of gateway
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"
"time"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/stretchr/testify/assert"

"core/pkg/database/dao"
)

func TestGatewayNameKey_Key(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions src/core-api/pkg/cacheimpls/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ var (
newRandomDuration(10),
)

releaseHistoryCache = memory.NewCache(
"release_history",
DisableCache,
tracedFuncWrapper("release_history", retrieveReleaseHistory),
1*time.Minute,
newRandomDuration(10),
)

// app_code + gateway_id => permission, may change frequently
appGatewayPermissionCache = memory.NewCache(
"app_gateway_permission",
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/jwt_public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"errors"
"strconv"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
)

// JWTPublicKeyCacheKey is the key of jwt public key
Expand Down
7 changes: 4 additions & 3 deletions src/core-api/pkg/cacheimpls/microgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"context"
"errors"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
)

// MicroGatewayKey is the key of micro gateway
Expand Down Expand Up @@ -66,7 +66,8 @@ func GetMicroGateway(ctx context.Context, instanceID string) (microGateway dao.M
// MicroGatewayConfig is the config of micro gateway, it configured on dashboard, saved into db as a json
// the schema is like {secret_key: {jwt_auth: xxxxx}}
// here we use the jwt_auth as the credentials of the micro gateway with the instance id
// Note: The original credentials were a JWT token, and after refactoring we changed to the instance_id + token in the header.
// Note: The original credentials were a JWT token, and after refactoring we changed to the instance_id + token in the
// header.
type MicroGatewayConfig struct {
JwtAuth JwtAuth `json:"jwt_auth"`
}
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/microgateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"
"time"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"
"github.com/TencentBlueKing/gopkg/cache/memory"
"github.com/stretchr/testify/assert"

"core/pkg/database/dao"
)

func TestMicroGatewayKey_Key(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions src/core-api/pkg/cacheimpls/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"errors"
"strconv"

"core/pkg/database/dao"

"github.com/TencentBlueKing/gopkg/cache"

"core/pkg/database/dao"
)

// ReleaseKey is the key of release
Expand Down
Loading
Loading