Skip to content

Commit

Permalink
feat: 修改短链
Browse files Browse the repository at this point in the history
update: dockerfile,workflow
  • Loading branch information
nitezs committed Mar 13, 2024
1 parent 1d9de31 commit 14c3b97
Show file tree
Hide file tree
Showing 19 changed files with 629 additions and 547 deletions.
75 changes: 36 additions & 39 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,69 +1,66 @@
name: Build and Push to GHCR
name: Build and Push Docker

on:
push:
branches:
- dev
tags:
- '*'
- "*"
workflow_dispatch:

jobs:
build:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Set tag name
id: set_tag
run: |
if [[ $GITHUB_REF == refs/heads/* ]]; then
echo "::set-output name=tag::$(echo $GITHUB_REF | cut -d'/' -f3)"
else
echo "::set-output name=tag::${{ github.ref_name }}"
fi
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Check if triggered by tag
id: check_tag
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ secrets.DOCKER_HUB_USERNAME }}/sub2clash
ghcr.io/${{ github.repository }}
- name: Prepare args
id: prep
run: |
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
echo "::set-output name=triggered_by_tag::true"
else
echo "::set-output name=triggered_by_tag::false"
if [[ "${{ github.event_name }}" == 'workflow_dispatch' ]]; then
VERSION="${{ github.sha }}"
fi
if [[ "$GITHUB_REF" == 'refs/heads/dev' ]]; then
VERSION="${{ github.sha }}"
fi
if [[ "$GITHUB_REF" == 'refs/tags/'* ]]; then
VERSION=$TAG_NAME
fi
echo "version=$VERSION" >> $GITHUB_ENV
- name: Build and push Docker image for dev branch
if: steps.check_tag.outputs.triggered_by_tag == 'false'
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
build-args: |
dev=true
version=${{ github.sha }}
push: true
tags: ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}
- name: Build and push Docker image for tags
if: steps.check_tag.outputs.triggered_by_tag == 'true'
uses: docker/build-push-action@v2
- name: Set up Docker buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image to GHCR and Docker Hub
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
build-args: |
dev=false
version=${{ steps.set_tag.outputs.tag }}
build-args: version=${{ env.version }}
push: true
tags: |
ghcr.io/${{ github.repository }}:${{ steps.set_tag.outputs.tag }}
ghcr.io/${{ github.repository }}:latest
platforms: linux/amd64,linux/arm,linux/arm64
tags: ${{ steps.prep.outputs.tags }}
110 changes: 0 additions & 110 deletions .github/workflows/go.yml

This file was deleted.

31 changes: 31 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Build and Release

on:
push:
tags:
- "*"
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.5

- name: Run goreleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --rm-dist --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test
logs
sub2clash.db
.env
.vscode/settings.json
9 changes: 5 additions & 4 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#before:
# hooks:
# - go mod tidy
project_name: sub2clash
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- linux
- darwin
goarch:
- amd64
- arm64
- "386"
ldflags:
- -s -w -X sub2clash/config.Version={{ .Version }}
no_unique_dist_dir: true
binary: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
archives:
- format: binary
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"mode": "debug",
"program": "${workspaceFolder}/main.go",
"output": "${workspaceFolder}/dist/main.exe",
"buildFlags": "-ldflags '-X sub2clash/config.Dev=true -X sub2clash/config.Version=dev'"
"buildFlags": "-ldflags '-X sub2clash/config.Version=dev'"
}
]
}
16 changes: 13 additions & 3 deletions API_README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `/clash`, `/meta`
# `GET /clash`, `GET /meta`

获取 Clash/Clash.Meta 配置链接

Expand All @@ -17,7 +17,7 @@
| remove | string || - | 通过正则表达式删除节点 |
| nodeList | bool || `false` | 只输出节点 |

# `/short`
# `POST /short`

获取短链,Content-Type 为 `application/json`
具体参考使用可以参考 [api\templates\index.html](api/static/index.html)
Expand All @@ -27,11 +27,21 @@
| url | string || - | 需要转换的 Query 参数部分 |
| password | string || - | 短链密码 |

# `/s/:hash`
# `GET /s/:hash`

短链跳转
`hash` 为动态路由参数,可以通过 `/short` 接口获取

| Query 参数 | 类型 | 是否必须 | 默认值 | 说明 |
| ---------- | ------ | -------- | ------ | -------- |
| password | string || - | 短链密码 |

# `PUT /short`

更新短链,Content-Type 为 `application/json`

| Body 参数 | 类型 | 是否必须 | 默认值 | 说明 |
| --------- | ------ | -------- | ------ | ------------------------- |
| url | string || - | 需要转换的 Query 参数部分 |
| password | string || - | 短链密码 |
| hash | string || - | 短链 hash |
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ RUN go mod download

# 获取参数
ARG version
ARG dev

# 使用 -ldflags 参数进行编译
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X sub2clash/config.Version=${version} -X sub2clash/config.Dev=${dev}" -o sub2clash main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X sub2clash/config.Version=${version}" -o sub2clash main.go

FROM alpine:latest

Expand Down
2 changes: 1 addition & 1 deletion api/controller/clash.go → api/handler/clash.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package handler

import (
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion api/controller/default.go → api/handler/default.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package handler

import (
"crypto/sha256"
Expand Down
2 changes: 1 addition & 1 deletion api/controller/meta.go → api/handler/meta.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package handler

import (
_ "embed"
Expand Down
30 changes: 29 additions & 1 deletion api/controller/short_link.go → api/handler/short_link.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package handler

import (
"errors"
Expand Down Expand Up @@ -107,3 +107,31 @@ func ShortLinkGetHandler(c *gin.Context) {
}
c.String(http.StatusOK, string(all))
}

func ShortLinkUpdateHandler(c *gin.Context) {
var params validator.ShortLinkUpdateValidator
if err := c.ShouldBind(&params); err != nil {
c.String(400, "参数错误: "+err.Error())
}
if strings.TrimSpace(params.Url) == "" {
c.String(400, "参数错误")
return
}
var shortLink model.ShortLink
result := database.FindShortLinkByHash(params.Hash, &shortLink)
if result.Error != nil {
c.String(404, "未找到短链接")
return
}
if shortLink.Password == "" {
c.String(403, "无法修改无密码短链接")
return
}
if shortLink.Password != params.Password {
c.String(403, "密码错误")
return
}
shortLink.Url = params.Url
database.SaveShortLink(&shortLink)
c.String(200, "更新成功")
}
Loading

0 comments on commit 14c3b97

Please sign in to comment.