Skip to content

Commit

Permalink
refactor: 准备重构项目
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 29, 2024
1 parent edf4870 commit 2968741
Show file tree
Hide file tree
Showing 152 changed files with 2,989 additions and 4,439 deletions.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-FileCopyrightText: 2024 caixw
#
# SPDX-License-Identifier: MIT

.PHONY: api gen watch build install

ROOT = .
DOCS = $(ROOT)/docs
API = $(DOCS)/api
TARGET = $(ROOT)/cmd/server
BIN = $(TARGET)/server

dist = ./dist

# 生成 API 文件
api:
web restdoc -o=$(API)/restdoc.json

gen:
go generate ./...

build:
web build -o=$(BIN) -v $(TARGET)

install:
cd $(TARGET) && ./server -a=install

watch:
web watch -app=-a=serve $(TARGET)
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## 目录结构

- pkg 一些通用的包;
- modules 通用模块,模块只能是单例;
- locales cmfx 用到的本地化内容;
- cmd 命令行
- cmfx 后端主目录
- cmfx/modules 通用模块,模块只能是单例;
- locales 本地化内容;

- web 前端
- dist 前端编译后的内容

- docs 文档
- docs/api API 文档
3 changes: 0 additions & 3 deletions cmd/cmfx/.gitignore

This file was deleted.

107 changes: 0 additions & 107 deletions cmd/cmfx/cmfx.go

This file was deleted.

3 changes: 3 additions & 0 deletions cmd/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
server
server.db
logs/*
18 changes: 18 additions & 0 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2022-2024 caixw
//
// SPDX-License-Identifier: MIT

package main

import (
"github.com/issue9/cmfx/cmfx"
"github.com/issue9/cmfx/cmfx/inital/cmd"

_ "github.com/mattn/go-sqlite3"
)

func main() {
if err := cmd.Exec("cmfx", cmfx.Version); err != nil {
panic(err)
}
}
14 changes: 7 additions & 7 deletions cmd/cmfx/web.xml → cmd/server/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@

<user>
<db type="sqlite3">
<dsn>./cmfx.db</dsn>
<dsn>./server.db</dsn>
</db>
<router>
<url>http://localhost:8080</url>
</router>
<admin accessExpires="43200" refreshExpires="86400">
<urlPrefix>/admin</urlPrefix>
<algorithms>
<algorithm type="hmac">
<name>HS256</name>
<algorithm name="HS256">
<public>a;asdfasi8iuhf;;DKf;ewieqpwe</public>
<private>a;asdfasi8iuhf;;DKf;ewieqpwe</private>
</algorithm>
<algorithm type="hmac">
<name>HS512</name>
<algorithm name="HS512">
<public>a:DJfi;[pqwieqw;werjrqpwe</public>
<private>a:DJfi;[pqwieqw;werjrqpwe</private>
</algorithm>
<algorithm type="rsa">
<name>RS512</name>
<algorithm name="RS512">
<public><![CDATA[-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOALys4MJZXhSBVhgSgIXCsFZU1D8rPX
D0kpOUjD1uUF90jZNBQPT/8HLYVd7yxp8EcWCQWROmlf6n4FDdVLTHcCAwEAAQ==
Expand Down
54 changes: 0 additions & 54 deletions cmfx.go

This file was deleted.

57 changes: 57 additions & 0 deletions cmfx/cmfx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: 2022-2024 caixw
//
// SPDX-License-Identifier: MIT

//go:generate web locale -l=und -m -f=yaml ./
//go:generate web update-locale -src=./locales/und.yaml -dest=./locales/zh-CN.yaml

// Package cmfx 基于 https://github.com/issue9/web 框架的一些通用模块
package cmfx

// # restdoc cmfx 文档
//
// @media application/json application/xml
// @version [Version]
// @tag admin 管理员端
// @tag rbac RBAC
// @resp 4XX * github.com/issue9/web.Problem
// @resp 5XX * github.com/issue9/web.Problem desc

import (
"github.com/issue9/web"
"github.com/issue9/web/locales"
)

// Version 表示当前框架的版本
const Version = "0.7.0"

// 定义可用的错误代码
const (
BadRequest = web.ProblemBadRequest
BadRequestInvalidPath = "40001"
BadRequestInvalidQuery = "40002"
BadRequestInvalidHeader = "40003"
BadRequestInvalidBody = "40004"
)

// 401
const (
Unauthorized = web.ProblemUnauthorized
UnauthorizedInvalidState = "40101"
UnauthorizedInvalidToken = "40102"
UnauthorizedSecurityToken = "40103" // 需要强验证
UnauthorizedInvalidAccount = "40104" // 无效的账号或密码
UnauthorizedNeedChangePassword = "40105"

// 可注册的状态,比如 OAuth2 验证,如果未注册,返回一个 ID 可用以注册。
UnauthorizedRegistrable = "40106"
)

// 403
const (
Forbidden = web.ProblemForbidden
ForbiddenStateNotAllow = "40301"
ForbiddenCaNotDeleteYourself = "40302"
)

func ErrNotFound() error { return locales.ErrNotFound() }
38 changes: 38 additions & 0 deletions cmfx/filters/filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2022-2024 caixw
//
// SPDX-License-Identifier: MIT

// Package filters 通用过滤器
package filters

import (
"github.com/issue9/web/filter"
v "github.com/issue9/webfilter/validator"

"github.com/issue9/cmfx/locales"
)

func NilOr[T any](validator func(T) bool) filter.Builder[T] {
return filter.NewBuilder(v.V(v.Or(validator, v.Nil), locales.InvalidValue))
}

func Nil[T any]() filter.Builder[T] {
return filter.NewBuilder(v.V[T](v.Nil, locales.MustBeEmpty))
}

// NotZero 非零值
func NotZero[T any]() filter.Builder[T] {
return filter.NewBuilder(v.V(v.Not(v.Zero[T]), locales.Required))
}

func Zero[T any]() filter.Builder[T] {
return filter.NewBuilder(v.V(v.Zero[T], locales.Required))
}

func Equal[T comparable](val T) filter.Builder[T] {
return filter.NewBuilder(v.V(v.Equal(val), locales.InvalidValue))
}

func NotEqual[T comparable](val T) filter.Builder[T] {
return filter.NewBuilder(v.V(v.Not(v.Equal(val)), locales.InvalidValue))
}
Loading

0 comments on commit 2968741

Please sign in to comment.