Skip to content

Commit

Permalink
feat: init api && db demo (#46)
Browse files Browse the repository at this point in the history
* feat: init client api mode with sqlite3
  • Loading branch information
DVKunion committed Mar 1, 2024
1 parent 37b0da2 commit 8e46f9f
Show file tree
Hide file tree
Showing 232 changed files with 10,606 additions and 35,004 deletions.
3 changes: 2 additions & 1 deletion .github/conf/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
before:
hooks:
- go mod tidy
-
builds:
- binary: seamoon
env:
Expand All @@ -14,7 +15,7 @@ builds:
- 6
- 7
ldflags:
- -s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version={{.Version}}
- -s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version={{.Version}} -X github.com/DVKunion/SeaMoon/pkg/consts.Commit={{.Sha}}
checksum:
name_template: 'checksums.txt'
snapshot:
Expand Down
29 changes: 25 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,32 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Clone seamoon-web repository
uses: actions/checkout@v4
with:
repository: 'DVKunion/seamoon-web'
ref: 'main' # Or any other branch or tag you want to checkout
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: |
npm install
- name: Build Front
run: |
npm run build
cp -r dist /tmp
- uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.21
- name: Build
run: go build --ldflags "-s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version=${{github.ref_name}}" cmd/main.go

run: |
cp -r /tmp/dist cmd/client/static
go mod tidy
go build -o seamoon -v --ldflags "-s -w -X github.com/DVKunion/SeaMoon/pkg/consts.Version=${{github.ref_name}} -X github.com/DVKunion/SeaMoon/pkg/consts.Commit=${{github.sha}}" cmd/main.go
- uses: actions/upload-artifact@v4
with:
path: seamoon
10 changes: 5 additions & 5 deletions .github/workflows/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jobs:
runs-on: ubuntu-20.04
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v3
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
- uses: docker/login-action@v2
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- uses: docker/build-push-action@v3
- uses: docker/build-push-action@v5
with:
push: true
platforms: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
node-version: [ 18.x ]
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Build
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.21
id: go
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*.crt
*.key

dist/
cmd/client/static/dist/*
web/
*.log
*.toml
Expand All @@ -31,4 +31,5 @@ web/
node_modules
bootstrap
.s/
.seamoom
.seamoom
.seamoon.db
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ COPY .. /src
WORKDIR /src
ENV CGO_ENABLED 0
ENV VERSION=${VERSION}
RUN go build -ldflags "-X github.com/DVKunion/SeaMoon/server/consts.Version=${VERSION}" -o /tmp/seamoon cmd/main.go
ENV SHA=${SHA}
RUN go build -ldflags "-X github.com/DVKunion/SeaMoon/server/consts.Version=${VERSION} -X github.com/DVKunion/SeaMoon/server/consts.Commit=${SHA}" -o /tmp/seamoon cmd/main.go
RUN chmod +x /tmp/seamoon
# run stage
FROM alpine:3.19
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

月海之名取自于苏轼的《西江月·顷在黄州》,寓意月海取自于传统工具,用之于云,最终达到隐匿于海的效果。

月海基于 Serverless 的动态特性、分别从网络层、应用层实现对应的能力,并利用serverless-devs工具来实现快捷的部署/跨厂商操作。
月海基于 Serverless 的动态特性、分别从网络层、应用层实现对应的能力,并集成了SDK来实现快捷的部署/跨厂商操作。

想要了解更多,请移步 [官方手册](https://seamoon.dvkunion.cn)

Expand Down
87 changes: 87 additions & 0 deletions cmd/client/api/control/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package control

import (
"crypto/md5"
"encoding/hex"
"net/http"
"strings"

"github.com/gin-gonic/gin"

"github.com/DVKunion/SeaMoon/cmd/client/api/models"
"github.com/DVKunion/SeaMoon/cmd/client/api/service"
"github.com/DVKunion/SeaMoon/cmd/client/api/types"
"github.com/DVKunion/SeaMoon/pkg/tools"
)

type AuthController struct {
svc service.ApiService
}

func (a AuthController) Login(c *gin.Context) {
var obj *models.Auth
if err := c.ShouldBindJSON(&obj); err != nil {
c.JSON(http.StatusBadRequest, errorMsg(10000))
return
}
if obj.Username == "" || obj.Password == "" {
c.JSON(http.StatusBadRequest, errorMsg(10003))
return
}
// 检查用户是否存在
data := a.svc.List(0, 1, false, service.Condition{Key: "USERNAME", Value: obj.Username}, service.Condition{Key: "TYPE", Value: types.Admin})
if len(data.([]models.Auth)) != 1 {
c.JSON(http.StatusForbidden, errorMsg(10010))
return
}
// 检查用户密码是否正确
target := data.([]models.Auth)[0]
hash := md5.New()

// 写入数据到哈希实例中
hash.Write([]byte(obj.Password))

// 计算哈希值
if target.Password != strings.ToLower(hex.EncodeToString(hash.Sum(nil))) {
c.JSON(http.StatusForbidden, errorMsg(10010))
return
}

token, err := JWTAuth(target.Username, target.Type)
if err != nil {
c.JSON(http.StatusForbidden, errorMsg(10010))
return
}

c.JSON(http.StatusOK, successMsg(1, token))
}

func (a AuthController) Passwd(c *gin.Context) {
var obj *models.Auth
if err := c.ShouldBindJSON(&obj); err != nil {
c.JSON(http.StatusBadRequest, errorMsg(10000))
return
}
if obj.Username == "" || obj.Password == "" {
c.JSON(http.StatusBadRequest, errorMsg(10003))
return
}
// 检查用户是否存在
data := a.svc.List(0, 1, false, service.Condition{Key: "USERNAME", Value: obj.Username}, service.Condition{Key: "TYPE", Value: types.Admin})
if len(data.([]models.Auth)) != 1 {
c.JSON(http.StatusBadRequest, errorMsg(10002))
return
}
target := data.([]models.Auth)[0]
hash := md5.New()

// 写入数据到哈希实例中
hash.Write([]byte(obj.Password))

// 更新数据
target.Password = strings.ToLower(hex.EncodeToString(hash.Sum(nil)))
a.svc.Update(target.ID, &target)
// 更新 jwt secret 让之前的 token 失效
secret = []byte(tools.GenerateRandomString(64))
c.JSON(http.StatusOK, successMsg(1, "更新成功"))
}
Loading

0 comments on commit 8e46f9f

Please sign in to comment.