Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2022-2025, Mikhail Knyazhev <markus621@yandex.com>
Copyright (c) 2022-2026, Mikhail Knyazhev <markus621@yandex.com>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- Application customization via plugins
- Built-in dependency container
- Data binding for JSON
- Executing console commands
- Automatic dependency resolution at startup

## Guide

Expand Down
8 changes: 8 additions & 0 deletions _example/basic/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
SHELL=/bin/bash

help:
go run main.go --help
go run main.go env --help

run:
go run main.go --config=config.yaml

cmd:
go run main.go env
go run main.go ctrl

check:
curl -v http://127.0.0.1:10000/users && echo "" && echo "" && echo ""
time ab -n 1230 -c 5 http://127.0.0.1:10000/users && echo "" && echo "" && echo ""
Expand Down
3 changes: 2 additions & 1 deletion _example/basic/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ env: dev

log:
file_path: /dev/stdout
format: string
format: json
level: 4

http:
Expand All @@ -14,3 +14,4 @@ metrics:
gauge:
- users_request

custom:
79 changes: 57 additions & 22 deletions _example/basic/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package main

import (
"context"
"encoding/json"
"fmt"
"os"

"go.osspkg.com/goppy/v3"
"go.osspkg.com/goppy/v3/console"
"go.osspkg.com/goppy/v3/dic/broker"
"go.osspkg.com/goppy/v3/metrics"
"go.osspkg.com/goppy/v3/plugins"
"go.osspkg.com/goppy/v3/web"
"go.osspkg.com/logx"

"go.osspkg.com/goppy/v2"
"go.osspkg.com/goppy/v2/metrics"
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/goppy/v2/web"
"go.osspkg.com/xc"
)

type IStatus interface {
GetStatus() int
}

func main() {
// Specify the path to the config via the argument: `--config`.
// Specify the path to the pidfile via the argument: `--pid`.
Expand All @@ -27,24 +34,48 @@ func main() {
web.WithServer(),
)
app.Plugins(
plugins.Kind{
Inject: NewController,
Resolve: func(routes web.ServerPool, c *Controller) {
router, ok := routes.Main()
if !ok {
return
}

router.Use(web.ThrottlingMiddleware(100))
router.Get("/users", c.Users)

api := router.Collection("/api/v1", web.ThrottlingMiddleware(100))
api.Get("/user/{id}", c.User)
},
NewController,
func(routes web.ServerPool, c *Controller) {
router, ok := routes.Main()
if !ok {
return
}

router.Use(web.ThrottlingMiddleware(100))
router.Get("/users", c.Users)

api := router.Collection("/api/v1", web.ThrottlingMiddleware(100))
api.Get("/user/{id}", c.User)
},
broker.WithUniversalBroker[IStatus](
func(_ xc.Context, status IStatus) error {
fmt.Println(">> UniversalBroker got status", status.GetStatus())
return nil
},
func(status IStatus) error {
return nil
},
),
)
app.Command("env", func() {
fmt.Println(os.Environ())
app.Command(func(ctx context.Context, _ plugins.DIResolver, setter console.CommandSetter) {
setter.Setup("env", "show all envs")
setter.ExecFunc(func() {
fmt.Println(os.Environ())
})
})
app.Command(func(ctx context.Context, r plugins.DIResolver, setter console.CommandSetter) {
setter.Setup("ctrl", "call ctrl")
setter.ExecFunc(func() {
logx.SetLevel(0)

console.FatalIfErr(r.Resolve(func(c *Controller) {
fmt.Println(c.GetStatus())
}), "can't find controller")

console.FatalIfErr(r.Resolve(func(c *Controller) {
fmt.Println(c.GetStatus())
}), "can't find controller")
})
})
app.Run()
}
Expand All @@ -69,6 +100,10 @@ func (v *Controller) User(ctx web.Ctx) {
logx.Info("user - %d", id)
}

func (v *Controller) GetStatus() int {
return 200
}

type Model struct {
data []int64
}
Expand Down
43 changes: 20 additions & 23 deletions _example/database/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand All @@ -11,12 +11,11 @@ import (

"go.osspkg.com/logx"

"go.osspkg.com/goppy/v2/orm/clients/sqlite"

"go.osspkg.com/goppy/v2"
"go.osspkg.com/goppy/v2/orm"
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/goppy/v2/web"
"go.osspkg.com/goppy/v3"
"go.osspkg.com/goppy/v3/orm"
"go.osspkg.com/goppy/v3/orm/clients/sqlite"
"go.osspkg.com/goppy/v3/plugins"
"go.osspkg.com/goppy/v3/web"
)

func main() {
Expand All @@ -39,23 +38,21 @@ func main() {
}),
orm.WithMigration(),
)
app.Plugins(
plugins.Kind{
Inject: NewController,
Resolve: func(routes web.ServerPool, c *Controller) {
router, ok := routes.Main()
if !ok {
return
}

router.Use(web.ThrottlingMiddleware(100))
router.Get("/users", c.Users)

api := router.Collection("/api/v1", web.ThrottlingMiddleware(100))
api.Get("/user/{id}", c.User)
},
app.Plugins(plugins.Inject(
NewController,
func(routes web.ServerPool, c *Controller) {
router, ok := routes.Main()
if !ok {
return
}

router.Use(web.ThrottlingMiddleware(100))
router.Get("/users", c.Users)

api := router.Collection("/api/v1", web.ThrottlingMiddleware(100))
api.Get("/user/{id}", c.User)
},
)
))
app.Run()

}
Expand Down
4 changes: 4 additions & 0 deletions _example/demo-console/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SHELL=/bin/bash

run:
go run main.go
42 changes: 42 additions & 0 deletions _example/demo-console/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package main

import (
"fmt"

"go.osspkg.com/goppy/v3/console"
)

func main() {
cmd := console.New("", "")
cmd.RootCommand(console.NewCommand(func(setter console.CommandSetter) {
setter.ExecFunc(func() {

m := console.InteractiveMenu{
Title: "Choose variant",
Items: []string{
"Kubernetes", "Docker", "Terraform", "Ansible",
"Prometheus", "Grafana", "Vault", "Consul",
"Nginx", "PostgreSQL", "Redis", "Kafka",
},
CallBack: func(args ...string) {
fmt.Println("Selected:", args)
},
//MultiChoice: true,
//MaxCols: 10,
}

for i := 0; i < 100; i++ {
m.Items = append(m.Items, fmt.Sprintf("%d", i))
}

m.Run()

})
}))
cmd.Exec()
}
6 changes: 3 additions & 3 deletions _example/dns-server/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

package main

import (
"go.osspkg.com/goppy/v2"
"go.osspkg.com/goppy/v2/xdns"
"go.osspkg.com/goppy/v3"
"go.osspkg.com/goppy/v3/xdns"
)

func main() {
Expand Down
63 changes: 29 additions & 34 deletions _example/geoip/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand All @@ -9,10 +9,9 @@ import (
"encoding/json"
"net"

"go.osspkg.com/goppy/v2"
"go.osspkg.com/goppy/v2/geoip"
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/goppy/v2/web"
"go.osspkg.com/goppy/v3"
"go.osspkg.com/goppy/v3/geoip"
"go.osspkg.com/goppy/v3/web"
)

func main() {
Expand All @@ -22,37 +21,33 @@ func main() {
web.WithServer(),
geoip.WithMaxMindGeoIP(),
)
app.Plugins(
plugins.Kind{
Resolve: func(routes web.ServerPool, gip geoip.GeoIP) {
router, ok := routes.Main()
if !ok {
return
}
app.Plugins(func(routes web.ServerPool, gip geoip.GeoIP) {
router, ok := routes.Main()
if !ok {
return
}

router.Use(
geoip.ResolveIPMiddleware(gip),
geoip.HeadersMiddleware(
geoip.HeaderCloudflareClientIP,
geoip.HeaderCloudflareClientCountry,
),
)
router.Use(
geoip.ResolveIPMiddleware(gip),
geoip.HeadersMiddleware(
geoip.HeaderCloudflareClientIP,
geoip.HeaderCloudflareClientCountry,
),
)

router.Get("/", func(ctx web.Ctx) {
m := Model{data: struct {
ClientIP string `json:"client_ip"`
Country string `json:"country"`
ProxyIPs []net.IP `json:"proxy_ips"`
}{
ClientIP: geoip.GetClientIP(ctx.Context()).String(),
Country: geoip.GetCountryName(ctx.Context()),
ProxyIPs: geoip.GetProxyIPs(ctx.Context()),
}}
ctx.JSON(200, &m)
})
},
},
)
router.Get("/", func(ctx web.Ctx) {
m := Model{data: struct {
ClientIP string `json:"client_ip"`
Country string `json:"country"`
ProxyIPs []net.IP `json:"proxy_ips"`
}{
ClientIP: geoip.GetClientIP(ctx.Context()).String(),
Country: geoip.GetCountryName(ctx.Context()),
ProxyIPs: geoip.GetProxyIPs(ctx.Context()),
}}
ctx.JSON(200, &m)
})
})
app.Run()

}
Expand Down
2 changes: 1 addition & 1 deletion _example/go-gen/empty.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
2 changes: 1 addition & 1 deletion _example/go-gen/model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2025 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Copyright (c) 2022-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/

Expand Down
Loading
Loading