From eeb28d78941cf2746e611c02b148e347c47501da Mon Sep 17 00:00:00 2001 From: orca-zhang Date: Thu, 18 Jul 2024 16:13:51 +0800 Subject: [PATCH 1/3] fix: 304 cache after update (#49) --- route/static_route.go | 92 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/route/static_route.go b/route/static_route.go index 24d91a0..2a3d741 100644 --- a/route/static_route.go +++ b/route/static_route.go @@ -1,7 +1,12 @@ package route import ( + "fmt" + "io" + "io/fs" "net/http" + "os" + "time" "github.com/IceWhaleTech/CasaOS-Gateway/service" "github.com/labstack/echo/v4" @@ -12,7 +17,7 @@ type StaticRoute struct { state *service.State } -var RouteCache = make(map[string]string) +var startTime = time.Now() func NewStaticRoute(state *service.State) *StaticRoute { return &StaticRoute{ @@ -20,23 +25,82 @@ func NewStaticRoute(state *service.State) *StaticRoute { } } +type CustomFS struct { + base fs.FS +} + +func NewCustomFS(prefix string) *CustomFS { + return &CustomFS{ + base: fs.FS(os.DirFS(prefix)), + } +} + +func (c *CustomFS) Open(name string) (fs.File, error) { + file, err := c.base.Open(name) + if err != nil { + return nil, err + } + return &CustomFile{ + File: file, + }, nil +} + +func (c *CustomFS) Stat(name string) (fs.FileInfo, error) { + file, err := c.base.Open(name) + if err != nil { + return nil, err + } + info, err := file.Stat() + if err != nil { + return nil, err + } + return &CustomFileInfo{ + FileInfo: info, + }, nil +} + +type CustomFile struct { + fs.File +} + +func (c *CustomFile) Stat() (fs.FileInfo, error) { + info, err := c.File.Stat() + if err != nil { + return nil, err + } + return &CustomFileInfo{ + FileInfo: info, + }, nil +} + +func (c *CustomFile) Read(p []byte) (int, error) { + if seeker, ok := c.File.(io.Reader); ok { + return seeker.Read(p) + } + return 0, fmt.Errorf("file does not implement io.Reader") +} + +func (c *CustomFile) Seek(offset int64, whence int) (int64, error) { + if seeker, ok := c.File.(io.Seeker); ok { + return seeker.Seek(offset, whence) + } + return 0, fmt.Errorf("file does not implement io.Seeker") +} + +type CustomFileInfo struct { + fs.FileInfo +} + +func (c *CustomFileInfo) ModTime() time.Time { + return startTime +} + func (s *StaticRoute) GetRoute() http.Handler { e := echo.New() e.Use(echo_middleware.Gzip()) - e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { - return func(ctx echo.Context) error { - if _, ok := RouteCache[ctx.Request().URL.Path]; !ok { - ctx.Response().Writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate,proxy-revalidate, max-age=0") - RouteCache[ctx.Request().URL.Path] = ctx.Request().URL.Path - } - return next(ctx) - } - }) - - e.Static("/", s.state.GetWWWPath()) - + // sovle 304 cache problem by 'If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT' from web browser + e.StaticFS("/", NewCustomFS(s.state.GetWWWPath())) return e } - From 4a2c4ce19979368f0358b9cb9d029731b273f80c Mon Sep 17 00:00:00 2001 From: orca-zhang Date: Thu, 18 Jul 2024 21:30:00 +0800 Subject: [PATCH 2/3] Delete .github/workflows/push_test_server.yml (#50) Signed-off-by: orca-zhang --- .github/workflows/push_test_server.yml | 75 -------------------------- 1 file changed, 75 deletions(-) delete mode 100644 .github/workflows/push_test_server.yml diff --git a/.github/workflows/push_test_server.yml b/.github/workflows/push_test_server.yml deleted file mode 100644 index e795a27..0000000 --- a/.github/workflows/push_test_server.yml +++ /dev/null @@ -1,75 +0,0 @@ -name: Auto Publish Website -on: - push: - branches: - - main -permissions: - contents: write -jobs: - goreleaser: - runs-on: ubuntu-22.04 - steps: - - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: isntall git - run: sudo apt install --yes git - - name: git global - run: sudo git config --global --add safe.directory '*' - - - name: Fetch all tags - run: sudo git fetch --force --tags - - - name: set version - run: sudo git tag v99.99.99-alpha - - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 'stable' - - - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v4 - with: - # either 'goreleaser' (default) or 'goreleaser-pro' - distribution: goreleaser - version: latest - args: release --rm-dist --snapshot - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # Your GoReleaser Pro key, if you are using the 'goreleaser-pro' distribution - # GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} - - - name: remove migration file - run: find . -type f \( -name '*migration*' \) -delete - - - name: install sshpass - run: sudo apt install sshpass --yes - - - name: ZeroTier - uses: zerotier/github-action@v1.0.1 - with: - network_id: ${{ secrets.ZEROTIER_NETWORK_ID }} - auth_token: ${{ secrets.ZEROTIER_CENTRAL_TOKEN }} - - - name: ping host - shell: bash - run: | - count=10 - while ! ping -c 1 10.147.18.11 ; do - echo "waiting..." ; - sleep 1 ; - let count=count-1 - done - echo "ping success" - - - name: copy tar to target host - shell: bash - run: | - sshpass -p "${{ secrets.ssh_password }}" scp -r -o StrictHostKeyChecking=no -P 22 ./dist/*.gz root@10.147.18.11:/var/www/download - echo "ping success" - - name: send message - run: | - curl -X POST -H "Content-Type: application/json" -d '{"msg_type":"text","content":{"text":"CasaOS-Gateway updated"}}' ${{ secrets.SSH_ROBOT_URL }} From e69e6122a81560a7c7130c43a8895b24575126f9 Mon Sep 17 00:00:00 2001 From: Ns2Kracy <89824014+Ns2Kracy@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:06:35 +0800 Subject: [PATCH 3/3] feat: api npm sdk (#51) --- .github/workflows/publish_npm.yaml | 16 ++++ .gitignore | 6 +- api/gateway/openapi.yaml | 116 +++++++++++++++++++++++++++++ api/index.html | 24 ++++++ go.mod | 3 - go.sum | 2 - package.json | 38 ++++++++++ tsconfig.json | 25 +++++++ 8 files changed, 224 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/publish_npm.yaml create mode 100644 api/gateway/openapi.yaml create mode 100644 api/index.html create mode 100644 package.json create mode 100644 tsconfig.json diff --git a/.github/workflows/publish_npm.yaml b/.github/workflows/publish_npm.yaml new file mode 100644 index 0000000..5a9ddf7 --- /dev/null +++ b/.github/workflows/publish_npm.yaml @@ -0,0 +1,16 @@ +name: publish npm + +on: + push: + tags: + - v*.*.* + workflow_dispatch: + +permissions: + contents: write + +jobs: + call-workflow-passing-data: + uses: IceWhaleTech/github/.github/workflows/npm_release.yml@main + secrets: + NPM_TOKEN_PRIVATE: ${{ secrets.NPM_TOKEN_PRIVATE }} diff --git a/.gitignore b/.gitignore index 34a9d41..55b7d9d 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,8 @@ __debug_bin dist/ -/github \ No newline at end of file +/github +node_modules +yarn.lock +generate +openapitools.json diff --git a/api/gateway/openapi.yaml b/api/gateway/openapi.yaml new file mode 100644 index 0000000..5613abf --- /dev/null +++ b/api/gateway/openapi.yaml @@ -0,0 +1,116 @@ +openapi: 3.0.3 + +info: + title: CasaOS Gateway API + version: v1 + description: |- + + + + CasaOS + + +servers: + - url: /v1/gateway + +tags: + - name: USB methods + description: |- + USB methods + +security: + - access_token: [] + +paths: + /port: + put: + summary: Set gateway port + description: |- + Set gateway port + operationId: setGatewayPort + tags: + - Gateway methods + requestBody: + content: + application/json: + schema: + type: object + properties: + port: + type: integer + description: Gateway port + example: 80 + responses: + "200": + $ref: "#/components/responses/ResponseOK" + get: + summary: Get gateway port + description: |- + Get gateway port + operationId: getGatewayPort + tags: + - Gateway methods + responses: + "200": + $ref: "#/components/responses/ResponseStringOK" + "400": + $ref: "#/components/responses/ResponseBadRequest" + "500": + $ref: "#/components/responses/ResponseInternalServerError" + +components: + securitySchemes: + access_token: + type: apiKey + in: header + name: Authorization + + responses: + ResponseOK: + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/BaseResponse" + ResponseStringOK: + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/SuccessResponseString" + ResponseBadRequest: + description: Bad Request + content: + application/json: + schema: + readOnly: true + allOf: + - $ref: "#/components/schemas/BaseResponse" + example: + message: "Bad Request" + ResponseInternalServerError: + description: Internal Server Error + content: + application/json: + schema: + readOnly: true + allOf: + - $ref: "#/components/schemas/BaseResponse" + example: + message: "Internal Server Error" + schemas: + BaseResponse: + properties: + message: + readOnly: true + description: message returned by server side if there is any + type: string + example: "" + + SuccessResponseString: + allOf: + - $ref: "#/components/schemas/BaseResponse" + - properties: + data: + type: string + description: When the interface returns success, this field is the specific success information diff --git a/api/index.html b/api/index.html new file mode 100644 index 0000000..0653efc --- /dev/null +++ b/api/index.html @@ -0,0 +1,24 @@ + + + + + CasaOS | Developers + + + + + + + + + + + + + + diff --git a/go.mod b/go.mod index f84667e..c7aced2 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,6 @@ require ( github.com/IceWhaleTech/CasaOS-Common v0.4.8-alpha9 github.com/labstack/echo/v4 v4.12.0 github.com/spf13/viper v1.18.2 - github.com/stretchr/testify v1.8.4 go.uber.org/fx v1.20.1 gotest.tools v2.2.0+incompatible ) @@ -16,7 +15,6 @@ require ( github.com/bytedance/sonic v1.9.1 // indirect github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect github.com/coreos/go-systemd/v22 v22.5.0 // indirect - github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-gonic/gin v1.9.1 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect @@ -24,7 +22,6 @@ require ( github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/rogpeppe/go-internal v1.10.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect diff --git a/go.sum b/go.sum index c7f14a0..9a91b7d 100644 --- a/go.sum +++ b/go.sum @@ -15,7 +15,6 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= @@ -84,7 +83,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= diff --git a/package.json b/package.json new file mode 100644 index 0000000..8ed8e0b --- /dev/null +++ b/package.json @@ -0,0 +1,38 @@ +{ + "name": "@icewhale/casaos-gateway-openapi", + "version": "0.0.1", + "scripts": { + "clean": "rm -rf generate", + "build": "rm -rf dist && tsc && yarn clean", + "generate:local": "openapi-generator-cli generate -g typescript-axios -i ./api/gateway/openapi.yaml -o ./generate", + "generate:npx": "npx @openapitools/openapi-generator-cli generate -g typescript-axios -i ./api/gateway/openapi.yaml -o ./generate", + "generate:ts": "npx openapi-typescript-codegen --input ./api/gateway/openapi.yaml --output ./generate", + "start": "yarn generate:local && yarn build" + }, + "homepage": "https://github.com/IceWhaleTech/CasaOS-Gateway#readme", + "description": "CasaOS Gateway Typescript+Axios SDK", + "keywords": [ + "CasaOS", + "SDK", + "CasaOS Axios" + ], + "main": "dist/index.js", + "files": [ + "LICENSE", + "README.md", + "dist", + "generate" + ], + "dependencies": { + "axios": "^1.1.0" + }, + "devDependencies": { + "all-contributors-cli": "^6.24.0", + "@openapitools/openapi-generator-cli": "2.5.2", + "@types/node": "^18.8.3", + "openapi-typescript-codegen": "^0.23.0", + "typescript": "^4.9.5" + }, + "author": "casaos", + "license": "Apache-2.0" +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d78c4f0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "declaration": true, + "lib": [ + "es2017", + "DOM" + ], + "module": "commonjs", + "target": "es6", + "skipLibCheck": true, + "sourceMap": false, + "strict": true, + "useUnknownInCatchVariables": false, + "resolveJsonModule": true, + "esModuleInterop": true, + "outDir": "dist", + "types": [ + "node" + ], + }, + "exclude": [ + "node_modules" + ], + "main": "generate/index" +}