Skip to content

Commit

Permalink
refactor: 根据上游作出修改
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Mar 5, 2024
1 parent 86fcbf7 commit edf4870
Show file tree
Hide file tree
Showing 118 changed files with 2,593 additions and 2,146 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ jobs:
test:
name: Test
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
go: ['1.20.x']
go: ['1.21.x', '1.22.x']

steps:

- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
stable: '!contains(${{ matrix.go }}, "beta") && !contains(${{ matrix.go }}, "rc")'
go-version: ${{ matrix.go }}
id: go

- name: Vet
run: go vet -v ./...

- name: Test
run: go test -v -coverprofile='coverage.txt' -covermode=atomic ./...

- name: Upload Coverage report
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{secrets.CODECOV_TOKEN}}
file: ./coverage.txt
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 issue9
Copyright (c) 2022 caixw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
53 changes: 31 additions & 22 deletions cmd/cmfx/cmfx.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-FileCopyrightText: 2022-2024 caixw
//
// SPDX-License-Identifier: MIT

package main
Expand All @@ -10,33 +12,38 @@ import (
"github.com/issue9/cmfx"
"github.com/issue9/mux/v7"
"github.com/issue9/orm/v5"
"github.com/issue9/orm/v5/dialect"
"github.com/issue9/web"
"github.com/issue9/web/app"
"github.com/issue9/web/mode"
"github.com/issue9/web/comptime"
"github.com/issue9/web/server"
"github.com/issue9/web/server/app"

"github.com/issue9/cmfx/locales"
"github.com/issue9/cmfx/modules/admin"
"github.com/issue9/cmfx/pkg/db"
"github.com/issue9/cmfx/pkg/user"

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

type application = app.CLIOf[config]
type application = app.CLI[config]

type config struct {
DB *db.Config `yaml:"db" xml:"db" json:"db"`
Admin *user.Config `yaml:"admin" xml:"admin" json:"admin"`
}

func (c *config) SanitizeConfig() *web.FieldError {
if err := c.DB.SanitizeConfig(); err != nil {
return err
}
return c.Admin.SanitizeConfig()
}

func main() {
cmd := &application{
Name: "cmfx",
Version: cmfx.Version,
Init: initServer,
NewServer: initServer,
ConfigDir: "./",
ConfigFilename: "web.xml",
ServeActions: []string{"serve"},
Expand All @@ -48,51 +55,53 @@ func main() {
}
}

func initServer(s *web.Server, user *config, action string) error {
router := s.NewRouter("", nil,
func initServer(name, ver string, o *server.Options, user *config, action string) (web.Server, error) {
s, err := server.New(name, ver, o)
if err != nil {
return nil, err
}

router := s.Routers().New("", nil,
mux.URLDomain("https://localhost:8080/admin"),
mux.AllowedCORS(3600), // TODO 转配置项?
mux.AllowedCORS(3600),
mux.Recovery(func(w http.ResponseWriter, a any) {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
s.Logs().ERROR().Println(a)
s.Logs().ERROR().Print(a)
}),
mux.AnyInterceptor("any"), mux.DigitInterceptor("digit"),
)

mode.DebugRouter(router, "/debug", cmfx.BadRequestInvalidPath)
comptime.DebugRouter(router, "/debug", cmfx.BadRequestInvalidPath)

cmfx.AddProblems(s)

if err := s.LoadLocales(locales.Locales, "*.*"); err != nil {
return err
if err := s.Locale().LoadMessages("*.*", locales.All...); err != nil {
return nil, err
}

db, err := orm.NewDB("./cmfx.db", dialect.Sqlite3("sqlite3"))
if err != nil {
return err
}
db := user.DB.DB()
s.OnClose(func() error {
return db.Close()
})

switch action {
case "serve":
return load(s, db, router, user.Admin)
return s, load(s, db, router, user.Admin)
case "install":
return install(s, db)
return s, install(s, db, router)
case "upgrade":
panic("not implements")
default:
panic("invalid action")
}
}

func load(s *web.Server, db *orm.DB, router *web.Router, conf *user.Config) error {
_, err := admin.New(cmfx.NewModule("admin", web.Phrase("admin"), s, db), router, conf)
func load(s web.Server, db *orm.DB, router *web.Router, conf *user.Config) error {
_, err := admin.New(cmfx.NewModule("admin", web.Phrase("admin"), s, db, router), conf)
return err
}

func install(s *web.Server, db *orm.DB) error {
admin.Install(cmfx.NewModule("admin", web.Phrase("admin"), s, db))
func install(s web.Server, db *orm.DB, router *web.Router) error {
admin.Install(cmfx.NewModule("admin", web.Phrase("admin"), s, db, router))
return nil
}
66 changes: 35 additions & 31 deletions cmd/cmfx/web.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<web>
<http port=":8080" />
<logs created="true" caller="true">
<writer type="file">
<arg>15:04:05.000</arg>
<arg>./logs</arg>
<arg>20060102-%i.log</arg>
<arg>10485760</arg><!-- 约 10M -->
</writer>
<writer type="term">
<arg>04:05.000</arg>
<arg>stdout</arg>
</writer>
<logs created="15:04:05.000" caller="true">
<handlers>
<handler type="file">
<arg>./logs</arg>
<arg>20060102-%i.log</arg>
<arg>10485760</arg><!-- 约 10M -->
</handler>
<handler type="term">
<arg>stdout</arg>
</handler>
</handlers>
</logs>

<encodings>
Expand All @@ -33,36 +33,39 @@
<mimetype type="application/json" target="json" problem="application/problem+json" />
<mimetype type="application/xml" target="xml" problem="application/problem+xml" />
<mimetype type="text/xml" target="xml" problem="application/problem+xml" />
<mimetype type="multipart/form-data" target="nil" />
<mimetype type="multipart/form-data" target="nop" />
</mimetypes>

<fileSerializers>
<fileSerializer>.json</fileSerializer>
<fileSerializer>.xml</fileSerializer>
<fileSerializer>.yaml</fileSerializer>
<fileSerializer>.yml</fileSerializer>
<fileSerializer>json</fileSerializer>
<fileSerializer>xml</fileSerializer>
<fileSerializer>yaml</fileSerializer>
</fileSerializers>

<user>
<db type="sqlite3">
<dsn>./cmfx.db</dsn>
</db>
<admin accessExpires="43200" refreshExpires="86400">
<urlPrefix>/admin</urlPrefix>
<algorithm type="hmac">
<name>HS256</name>
<public>a;asdfasi8iuhf;;DKf;ewieqpwe</public>
<private>a;asdfasi8iuhf;;DKf;ewieqpwe</private>
</algorithm>
<algorithm type="hmac">
<name>HS512</name>
<public>a:DJfi;[pqwieqw;werjrqpwe</public>
<private>a:DJfi;[pqwieqw;werjrqpwe</private>
</algorithm>
<algorithm type="rsa">
<name>RS512</name>
<public><![CDATA[-----BEGIN PUBLIC KEY-----
<algorithms>
<algorithm type="hmac">
<name>HS256</name>
<public>a;asdfasi8iuhf;;DKf;ewieqpwe</public>
<private>a;asdfasi8iuhf;;DKf;ewieqpwe</private>
</algorithm>
<algorithm type="hmac">
<name>HS512</name>
<public>a:DJfi;[pqwieqw;werjrqpwe</public>
<private>a:DJfi;[pqwieqw;werjrqpwe</private>
</algorithm>
<algorithm type="rsa">
<name>RS512</name>
<public><![CDATA[-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOALys4MJZXhSBVhgSgIXCsFZU1D8rPX
D0kpOUjD1uUF90jZNBQPT/8HLYVd7yxp8EcWCQWROmlf6n4FDdVLTHcCAwEAAQ==
-----END PUBLIC KEY-----]]></public>
<private>-----BEGIN PRIVATE KEY-----
<private>-----BEGIN PRIVATE KEY-----
MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA4AvKzgwlleFIFWGB
KAhcKwVlTUPys9cPSSk5SMPW5QX3SNk0FA9P/wcthV3vLGnwRxYJBZE6aV/qfgUN
1UtMdwIDAQABAkAs0y8oOlXqvr/lRzTIBaQrF1FFPCr6wDRWtuC3JjeAamlSWBNO
Expand All @@ -72,7 +75,8 @@ eQIhAMhXie5pufXoLjlCgBN49aUw900L3MqtdSacFemfetb3AiAk5PLGTNuZKrU6
9hrtrHxZSoulFZZctl3xMpuc6x9fEQIhAKKA2WJ2BFExcjhwN24mZyekdP5jCJJU
jPakRl2w0aio
-----END PRIVATE KEY-----</private>
</algorithm>
</algorithm>
</algorithms>
</admin>
</user>
</web>
64 changes: 0 additions & 64 deletions cmd/enums/enums.go

This file was deleted.

14 changes: 8 additions & 6 deletions cmfx.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// 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-Hans.yaml

//go:generate web doc -d ./
//go:generate web restdoc -d ./

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

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

import (
"fmt"
Expand All @@ -32,7 +34,7 @@ const Version = "0.7.0"
// 依次执行 f 中的函数,碰到第一个返回错误的函数时即退出整个流程,并执行 cleanup 进行清理操作。
//
// cleanup 清理操作,只有当 f 出错时,才会执行 cleanup。
func Init(s *web.Server, cleanup func(), f ...func() error) {
func Init(s web.Server, cleanup func(), f ...func() error) {
for _, ff := range f {
if err := ff(); err != nil {
if cleanup != nil {
Expand All @@ -41,7 +43,7 @@ func Init(s *web.Server, cleanup func(), f ...func() error) {

var msg any
if ls, ok := err.(web.LocaleStringer); ok {
msg = ls.LocaleString(s.LocalePrinter())
msg = ls.LocaleString(s.Locale().Printer())
} else {
msg = err
}
Expand Down
Loading

0 comments on commit edf4870

Please sign in to comment.