Skip to content

Commit

Permalink
feat: uses swagger to generate http api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 committed Feb 19, 2025
1 parent 7578292 commit 6522477
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 6 deletions.
35 changes: 35 additions & 0 deletions server/controller/config/common/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

package common

type Swagger struct {
Enabled bool `default:"false" yaml:"enabled"`
}

// TODO use this
type FPermit struct {
Enabled bool `default:"false" yaml:"enabled"`
Host string `default:"fpermit" yaml:"host"`
Port int `default:"20823" yaml:"port"`
Timeout int `default:"30" yaml:"timeout"`
}

type IngesterApi struct {
Port int `default:"20106" yaml:"port"`
NodePort int `default:"30106" yaml:"node-port"`
Timeout int `default:"60" yaml:"timeout"`
}
2 changes: 2 additions & 0 deletions server/controller/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

shared_common "github.com/deepflowio/deepflow/server/common"
"github.com/deepflowio/deepflow/server/controller/common"
configs "github.com/deepflowio/deepflow/server/controller/config/common"
"github.com/deepflowio/deepflow/server/controller/db/clickhouse"
metadb "github.com/deepflowio/deepflow/server/controller/db/metadb/config"
"github.com/deepflowio/deepflow/server/controller/db/redis"
Expand Down Expand Up @@ -102,6 +103,7 @@ type ControllerConfig struct {
TagRecorderCfg tagrecorder.TagRecorderConfig `yaml:"tagrecorder"`
PrometheusCfg prometheus.Config `yaml:"prometheus"`
HTTPCfg http.Config `yaml:"http"`
SwaggerCfg configs.Swagger `yaml:"swagger"`
}

type Config struct {
Expand Down
22 changes: 22 additions & 0 deletions server/controller/http/appender/swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/

package appender

import (
"github.com/deepflowio/deepflow/server/controller/config"
)
func SetSwaggerConfig(cfg *config.ControllerConfig) { }
12 changes: 6 additions & 6 deletions server/controller/http/common/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@ func SetHTTPStatus(httpStatus int) ResponseSetter {
}

type Response struct {
rawPageResponse
RawPageResponse

err error `json:"-"` // Error is not serializ`ed
HttpStatus int `json:"-"` // httpStatus is not serialized
}

type rawPageResponse struct {
rawResponse
type RawPageResponse struct {
RawResponse
Page Page `json:"PAGE"`
}

type rawResponse struct {
type RawResponse struct {
OptStatus string `json:"OPT_STATUS"`
Description string `json:"DESCRIPTION"`
Data interface{} `json:"DATA"`
Expand Down Expand Up @@ -122,9 +122,9 @@ func (r *Response) Format() {

func (r Response) JSON() interface{} {
if r.Page.IsValid() {
return r.rawPageResponse
return r.RawPageResponse
}
return r.rawResponse
return r.RawResponse
}

// JSONString returns the string representation of the response when Data is a byte slice.
Expand Down
11 changes: 11 additions & 0 deletions server/controller/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/gin-gonic/gin"
"github.com/op/go-logging"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"

"github.com/deepflowio/deepflow/server/controller/config"
"github.com/deepflowio/deepflow/server/controller/genesis"
Expand Down Expand Up @@ -62,6 +64,11 @@ func NewServer(logFile string, cfg *config.ControllerConfig) *Server {
g.Use(gin.LoggerWithFormatter(logger.GinLogFormat))
// set custom middleware
g.Use(HandleORGIDMiddleware())

appender.SetSwaggerConfig(cfg)
if cfg.SwaggerCfg.Enabled {
g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
s.engine = g
return s
}
Expand Down Expand Up @@ -98,6 +105,10 @@ func (s *Server) RegisterRouters() {
i.RegisterTo(s.engine)
}
trouter.RegisterTo(s.engine)

for _, route := range s.engine.Routes() {
log.Infof(" TODO method: %s, path: %s", route.Method, route.Path)
}
}

func (s *Server) appendRegistrant() []registrant.Registrant {
Expand Down
5 changes: 5 additions & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ require (
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/pyroscope-io/pyroscope v0.37.1
github.com/spf13/viper v1.19.0
github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.8.12
github.com/volcengine/volcengine-go-sdk v1.0.141
go.opentelemetry.io/collector/pdata v1.0.0
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
Expand All @@ -137,6 +140,7 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/DataDog/zstd v1.4.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
Expand All @@ -156,6 +160,7 @@ require (
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
Expand Down
13 changes: 13 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ github.com/IBM/sarama v1.43.0 h1:YFFDn8mMI2QL0wOrG0J2sFoVIAFl7hS9JQi2YZsXtJc=
github.com/IBM/sarama v1.43.0/go.mod h1:zlE6HEbC/SMQ9mhEYaF7nNLYOUyrs0obySKCckWP9BM=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down Expand Up @@ -262,6 +264,8 @@ github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uq
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
Expand Down Expand Up @@ -305,9 +309,12 @@ github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJ
github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns=
github.com/go-openapi/spec v0.19.3/go.mod h1:FpwSN1ksY1eteniUU7X0N/BgJ7a4WvBFVA8Lj9mJglo=
github.com/go-openapi/spec v0.19.5/go.mod h1:Hm2Jr4jv8G1ciIAo+frC/Ft+rR2kQDh8JHKHb3gWUSk=
github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M=
github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I=
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-openapi/swag v0.21.1 h1:wm0rhTb5z7qpJRHBdPOMuY4QjVUMbF6/kwoYeRAOrKU=
github.com/go-openapi/swag v0.21.1/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
Expand Down Expand Up @@ -787,6 +794,12 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE=
github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg=
github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M=
github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo=
github.com/swaggo/swag v1.8.12 h1:pctzkNPu0AlQP2royqX3apjKCQonAnf7KGoxeO4y64w=
github.com/swaggo/swag v1.8.12/go.mod h1:lNfm6Gg+oAq3zRJQNEMBE66LIJKM44mxFqhEEgy2its=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.726 h1:ZHyvJ5yedfZGccd1ZUJD8ChnFq7BX621RdGQDfcJf4w=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.726/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
github.com/textnode/fencer v0.0.0-20121219195347-6baed0e5ef9a h1:nLqlJjMRYhG0n0/As6hBX1CiDbENjnVjXVjVS6zNIGc=
Expand Down
3 changes: 3 additions & 0 deletions server/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ controller:
# 2 (K8s Cluster in local AZs)
pod-cluster-internal-ip-to-ingester: 0

swagger:
enabled: false

http:
# resource api redis cache refresh interval, unit:s
redis_refresh_interval: 3600
Expand Down

0 comments on commit 6522477

Please sign in to comment.