Skip to content

Commit

Permalink
Merge pull request #1 from ccfish86/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ccfish86 authored Feb 22, 2023
2 parents 78cbce5 + aea253d commit 6e7868b
Show file tree
Hide file tree
Showing 70 changed files with 8,978 additions and 27 deletions.
11 changes: 11 additions & 0 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[wI2L/fizz: Gin wrapper with OpenAPI 3 spec generation (github.com)](https://github.com/wI2L/fizz)]的基础上,追加了UI。

主要是以下几点:

1. 参考了`springdoc`的处理方式,后续版本(如果使用的人多)可以尝试支持多个分组
2. 使用了针对swagger2和springdoc(openapi v3)的UI,即[[Knife4j Knife4j is a set of Swagger2 and OpenAPI3 All-in-one enhancement solution ](https://github.com/xiaoymin/knife4j)]`knife4j-vue3`项目

开发时注意事项,因使用了`内嵌`静态资源文件的库,所以需要`golang 1.16`以上的支持。

更新`knife4j-vue3`时,只需要使用`npm`将编译后的`dist/`内容复制到本项目的`/ui/knife4go`下即可。

10 changes: 7 additions & 3 deletions examples/market/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/loopfz/gadgeto/tonic"
"github.com/mcorbin/gadgeto/tonic"

"github.com/wI2L/fizz"
"github.com/wI2L/fizz/openapi"
"github.com/ccfish86/fizz/v2"
"github.com/ccfish86/fizz/v2/openapi"
"github.com/ccfish86/fizz/v2/ui"
)

// NewRouter returns a new router for the
Expand All @@ -33,12 +34,15 @@ func NewRouter() (*fizz.Fizz, error) {
// Create a new route that serve the OpenAPI spec.
fizz.GET("/openapi.json", nil, fizz.OpenAPI(infos, "json"))

ui.AddUIHandler(fizz.Engine(), "/doc", "/openapi.json")

// Setup routes.
routes(fizz.Group("/market", "market", "Your daily dose of freshness"))

if len(fizz.Errors()) != 0 {
return nil, fmt.Errorf("fizz errors: %v", fizz.Errors())
}

return fizz, nil
}

Expand Down
4 changes: 2 additions & 2 deletions fizz.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"strings"
"time"

"github.com/ccfish86/fizz/v2/openapi"
"github.com/gin-gonic/gin"
"github.com/loopfz/gadgeto/tonic"
"github.com/wI2L/fizz/openapi"
"github.com/mcorbin/gadgeto/tonic"
)

const ctxOpenAPIOperation = "_ctx_openapi_operation"
Expand Down
4 changes: 2 additions & 2 deletions fizz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

"github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
"github.com/loopfz/gadgeto/tonic"
"github.com/mcorbin/gadgeto/tonic"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"

"github.com/wI2L/fizz/openapi"
"github.com/ccfish84/fizz/v2/openapi"
)

func TestMain(m *testing.M) {
Expand Down
52 changes: 34 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
module github.com/wI2L/fizz
module github.com/ccfish86/fizz/v2

go 1.12
go 1.17

require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/gin-contrib/cors v1.3.0
github.com/gin-gonic/gin v1.7.7
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/gin-gonic/gin v1.8.1
github.com/gofrs/uuid v3.2.0+incompatible
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/juju/errors v0.0.0-20220622220526-54a94488269b
github.com/stretchr/testify v1.7.2
github.com/wI2L/fizz v0.22.0
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/loopfz/gadgeto v0.9.0 // indirect
github.com/mcorbin/gadgeto v0.12.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/goccy/go-json v0.9.8 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/errors v0.0.0-20190930114154-d42613fe1ab9
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2 // indirect
github.com/loopfz/gadgeto v0.9.0
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/stretchr/testify v1.7.0
github.com/ugorji/go v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/text v0.3.7
google.golang.org/protobuf v1.27.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220630215102-69896b714898 // indirect
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/go-playground/validator.v9 v9.30.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 1 addition & 1 deletion openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"

"github.com/gofrs/uuid"
"github.com/loopfz/gadgeto/tonic"
"github.com/mcorbin/gadgeto/tonic"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion openapi/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/Pallinder/go-randomdata"
"github.com/loopfz/gadgeto/tonic"
"github.com/mcorbin/gadgeto/tonic"
"github.com/stretchr/testify/assert"
)

Expand Down
15 changes: 15 additions & 0 deletions ui/api_docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ui

type SwaggerConfig struct {
ConfigUrl string `json:"configUrl"`
DisplayRequestDuration bool `json:"displayRequestDuration"`
Oauth2RedirectUrl string `json:"oauth2RedirectUrl"`
OperationsSorter string `json:"operationsSorter"`
ValidatorUrl string `json:"validatorUrl"`
Urls *[]SwaggerUrl `json:"urls"`
}

type SwaggerUrl struct {
Url string `json:"url"`
Name string `json:"name"`
}
95 changes: 95 additions & 0 deletions ui/fs_swagger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ui

import (
"bytes"
"html/template"
"io"
"io/fs"
"strings"
"time"
)

type fsWrapper struct {
fs fs.FS
jsonFileName string
}

func FsWrapper(fs fs.FS, jsonFilePath string) *fsWrapper {
return &fsWrapper{
fs: fs,
jsonFileName: jsonFilePath,
}
}

func (f *fsWrapper) Open(name string) (fs.File, error) {
if strings.HasSuffix(name, "index.html") {
tpl, err := template.ParseFS(f.fs, "index.gohtml")
if err != nil {
return nil, err
}
data := map[string]interface{}{
"openApiJson": strings.TrimPrefix(f.jsonFileName, "/"),
}

buf := new(bytes.Buffer)
if err = tpl.Execute(buf, data); err != nil {
return nil, err
}

return fsWrapperFile{
r: io.NopCloser(bytes.NewReader(buf.Bytes())),
fInfo: fileInfoMock{
name: name,
size: int64(buf.Len()),
},
}, nil
}

return f.fs.Open(name)
}

type fsWrapperFile struct {
r io.ReadCloser
fInfo fs.FileInfo
}

func (f fsWrapperFile) Stat() (fs.FileInfo, error) {
return f.fInfo, nil
}

func (f fsWrapperFile) Read(bytes []byte) (int, error) {
return f.r.Read(bytes)
}

func (f fsWrapperFile) Close() error {
return f.r.Close()
}

type fileInfoMock struct {
name string
size int64
}

func (f fileInfoMock) Name() string {
return f.name
}

func (f fileInfoMock) Size() int64 {
return f.size
}

func (f fileInfoMock) Mode() fs.FileMode {
return fs.FileMode(0)
}

func (f fileInfoMock) ModTime() time.Time {
return time.Now()
}

func (f fileInfoMock) IsDir() bool {
return false
}

func (f fileInfoMock) Sys() interface{} {
panic("implement me")
}
15 changes: 15 additions & 0 deletions ui/knife4go/doc.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
<script type="module" crossorigin src="./webjars/js/doc-3e371862.js"></script>
<link rel="stylesheet" href="./webjars/css/doc-c5acc9d0.css">
</head>
<body>
<div id="app"></div>

</body>
</html>
Binary file added ui/knife4go/favicon.ico
Binary file not shown.
Binary file added ui/knife4go/img/icons/android-chrome-192x192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/android-chrome-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/apple-touch-icon-60x60.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/apple-touch-icon-76x76.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/apple-touch-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/knife4go/img/icons/mstile-150x150.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6e7868b

Please sign in to comment.