Skip to content

Commit b2825ec

Browse files
authored
full refactoring (#146)
1 parent b09e768 commit b2825ec

File tree

213 files changed

+7340
-6070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

213 files changed

+7340
-6070
lines changed

.github/dependabot.yml

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
version: 2
23
updates:
34
- package-ecosystem: "gomod" # See documentation for possible values

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
name: CI
23

34
on:
@@ -11,7 +12,7 @@ jobs:
1112
runs-on: ubuntu-latest
1213
strategy:
1314
matrix:
14-
go: [ '1.23.11' ]
15+
go: [ '1.25' ]
1516
steps:
1617
- uses: actions/checkout@v3
1718

@@ -23,4 +24,4 @@ jobs:
2324
- name: Run CI
2425
env:
2526
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
26-
run: make pre-commit
27+
run: make ci

.gitignore

100644100755
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
*.bin
1+
2+
.tools/
3+
bin/
4+
vendor/
5+
build/
6+
.idea/
7+
.vscode/
8+
coverage.txt
9+
coverage.out
210
*.exe
311
*.exe~
412
*.dll
513
*.so
614
*.dylib
7-
*.test
8-
*.out
915
*.db
10-
*.db-shm
11-
*.db-wal
1216
*.db-journal
13-
*.dev.yaml
14-
*.mmdb
15-
16-
.idea/
17-
.vscode/
18-
.tools/
19-
20-
coverage.txt
21-
coverage.out
22-
23-
bin/
24-
vendor/
25-
build/
26-
2717
*.mmdb
18+
*.test
19+
*.out
2820
.env
21+
*.db-shm
22+
*.db-wal
23+
*.bin

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
version: "2"
22

33
run:
4-
go: "1.23"
4+
go: "1.25"
55
timeout: 5m
66
tests: false
77
issues-exit-code: 1

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ tests:
2727
.PHONY: pre-commit
2828
pre-commit: install setup license lint build tests
2929

30+
.PHONY: ci
31+
ci: pre-commit
32+
3033
.PHONY: tidy
3134
tidy:
3235
go mod tidy -v

_example/basic/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ run:
44
go run main.go --config=config.yaml
55

66
check:
7-
time ab -n 1230 -c 5 http://127.0.0.1:10000/users
8-
curl -v http://127.0.0.1:12000/metrics
7+
curl -v http://127.0.0.1:10000/users && echo "" && echo "" && echo ""
8+
time ab -n 1230 -c 5 http://127.0.0.1:10000/users && echo "" && echo "" && echo ""
9+
curl -v http://127.0.0.1:12000/metrics && echo "" && echo "" && echo ""

_example/basic/main.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
package main
77

88
import (
9+
"encoding/json"
910
"fmt"
1011
"os"
1112

12-
"go.osspkg.com/console"
1313
"go.osspkg.com/logx"
1414

1515
"go.osspkg.com/goppy/v2"
@@ -19,16 +19,22 @@ import (
1919
)
2020

2121
func main() {
22+
// Specify the path to the config via the argument: `--config`.
23+
// Specify the path to the pidfile via the argument: `--pid`.
2224
app := goppy.New("app_name", "v1.0.0", "app description")
2325
app.Plugins(
2426
metrics.WithServer(),
2527
web.WithServer(),
2628
)
2729
app.Plugins(
28-
plugins.Plugin{
30+
plugins.Kind{
2931
Inject: NewController,
30-
Resolve: func(routes web.RouterPool, c *Controller) {
31-
router := routes.Main()
32+
Resolve: func(routes web.ServerPool, c *Controller) {
33+
router, ok := routes.Main()
34+
if !ok {
35+
return
36+
}
37+
3238
router.Use(web.ThrottlingMiddleware(100))
3339
router.Get("/users", c.Users)
3440

@@ -37,7 +43,7 @@ func main() {
3743
},
3844
},
3945
)
40-
app.Command("env", func(s console.CommandSetter) {
46+
app.Command("env", func() {
4147
fmt.Println(os.Environ())
4248
})
4349
app.Run()
@@ -49,14 +55,24 @@ func NewController() *Controller {
4955
return &Controller{}
5056
}
5157

52-
func (v *Controller) Users(ctx web.Context) {
58+
func (v *Controller) Users(ctx web.Ctx) {
5359
metrics.Gauge("users_request").Inc()
54-
data := []int64{1, 2, 3, 4}
60+
data := Model{
61+
data: []int64{1, 2, 3, 4},
62+
}
5563
ctx.JSON(200, data)
5664
}
5765

58-
func (v *Controller) User(ctx web.Context) {
66+
func (v *Controller) User(ctx web.Ctx) {
5967
id, _ := ctx.Param("id").Int() // nolint: errcheck
6068
ctx.String(200, "user id: %d", id)
6169
logx.Info("user - %d", id)
6270
}
71+
72+
type Model struct {
73+
data []int64
74+
}
75+
76+
func (m Model) MarshalJSON() ([]byte, error) {
77+
return json.Marshal(m.data)
78+
}

_example/database/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ run:
44
go run main.go --config=config.yaml
55

66
check:
7-
curl -v http://127.0.0.1:10000/users && echo ""
8-
curl -v http://127.0.0.1:10000/api/v1/user/1 && echo ""
7+
curl -v http://127.0.0.1:10000/users && echo "" && echo "" && echo ""
8+
curl -v http://127.0.0.1:10000/api/v1/user/1 && echo "" && echo "" && echo ""
9+
time ab -n 1230 -c 5 http://127.0.0.1:10000/api/v1/user/1 && echo "" && echo "" && echo ""

_example/database/config.yaml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,6 @@ http:
99
- tag: main
1010
addr: 0.0.0.0:10000
1111

12-
mysql:
13-
- tags: mysql_master,mysql_slave
14-
host: 127.0.0.1
15-
port: 3306
16-
schema: test_database
17-
user: test
18-
password: test
19-
20-
pgsql:
21-
- tags: pgsql_master,pgsql_slave
22-
host: 127.0.0.1
23-
port: 5432
24-
schema: postgres
25-
user: postgres
26-
password: postgres
27-
2812
sqlite:
2913
- tags: sqlite_master
3014
file: ./sqlite.db

_example/database/main.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
package main
77

88
import (
9+
"encoding/json"
910
"fmt"
1011

1112
"go.osspkg.com/logx"
1213

14+
"go.osspkg.com/goppy/v2/orm/clients/sqlite"
15+
1316
"go.osspkg.com/goppy/v2"
1417
"go.osspkg.com/goppy/v2/orm"
1518
"go.osspkg.com/goppy/v2/plugins"
@@ -21,24 +24,30 @@ func main() {
2124
app := goppy.New("goppy_database", "v1.0.0", "")
2225
app.Plugins(
2326
web.WithServer(),
24-
orm.WithMysqlClient(),
25-
orm.WithSqliteClient(),
26-
orm.WithPgsqlClient(),
27-
orm.WithORM(),
27+
orm.WithORM(sqlite.Name),
2828
orm.WithMigration(orm.Migration{
29-
Tags: []string{"mysql_master"},
29+
Tags: []string{"sqlite_master"},
3030
Dialect: "mysql",
3131
Data: map[string]string{
32-
"0001_data.sql": "CREATE TABLE IF NOT EXISTS `demo` (\n `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY\n);",
32+
"0002_data.sql": `
33+
CREATE TABLE IF NOT EXISTS "demo2"
34+
(
35+
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT
36+
);
37+
`,
3338
},
3439
}),
3540
orm.WithMigration(),
3641
)
3742
app.Plugins(
38-
plugins.Plugin{
43+
plugins.Kind{
3944
Inject: NewController,
40-
Resolve: func(routes web.RouterPool, c *Controller) {
41-
router := routes.Main()
45+
Resolve: func(routes web.ServerPool, c *Controller) {
46+
router, ok := routes.Main()
47+
if !ok {
48+
return
49+
}
50+
4251
router.Use(web.ThrottlingMiddleware(100))
4352
router.Get("/users", c.Users)
4453

@@ -61,27 +70,29 @@ func NewController(orm orm.ORM) *Controller {
6170
}
6271
}
6372

64-
func (v *Controller) Users(ctx web.Context) {
65-
data := []int64{1, 2, 3, 4}
73+
func (v *Controller) Users(ctx web.Ctx) {
74+
data := Model{data: []int64{1, 2, 3, 4}}
6675
ctx.JSON(200, data)
6776
}
6877

69-
func (v *Controller) User(ctx web.Context) {
78+
func (v *Controller) User(ctx web.Ctx) {
7079
id, _ := ctx.Param("id").Int() // nolint: errcheck
7180

72-
err := v.orm.Tag("mysql_master").PingContext(ctx.Context())
73-
if err != nil {
74-
ctx.ErrorJSON(500, err, web.ErrCtx{"id": id})
75-
return
76-
}
77-
78-
err = v.orm.Tag("sqlite_master").PingContext(ctx.Context())
81+
err := v.orm.Tag("sqlite_master").PingContext(ctx.Context())
7982
if err != nil {
80-
ctx.ErrorJSON(500, err, web.ErrCtx{"id": id})
83+
ctx.ErrorJSON(500, err, "id", id)
8184
return
8285
}
8386

84-
ctx.ErrorJSON(400, fmt.Errorf("user not found"), web.ErrCtx{"id": id})
87+
ctx.ErrorJSON(200, fmt.Errorf("user not found"), "id", id)
8588

8689
logx.Info("user", "id", id)
8790
}
91+
92+
type Model struct {
93+
data []int64
94+
}
95+
96+
func (m Model) MarshalJSON() ([]byte, error) {
97+
return json.Marshal(m.data)
98+
}

0 commit comments

Comments
 (0)