Skip to content

Commit

Permalink
update container package to go-ioc
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Nov 28, 2022
1 parent 1d7447b commit 1e3fa21
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 83 deletions.
27 changes: 21 additions & 6 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package main

import (
"bytes"
"net"
"runtime"
"strconv"
"time"

"github.com/gorilla/mux"
"github.com/mylxsw/glacier/log"
"github.com/mylxsw/glacier/starter/app"
"github.com/mylxsw/glacier/web"

"net/http"
_ "net/http/pprof"

"github.com/mylxsw/glacier/event"
"github.com/mylxsw/glacier/example/config"
"github.com/mylxsw/glacier/example/job"
Expand Down Expand Up @@ -43,6 +48,8 @@ func main() {
// }
//})

runtime.SetBlockProfileRate(1)

infra.DEBUG = true
infra.PrintGraph = true

Expand Down Expand Up @@ -81,11 +88,19 @@ func run(ins *app.App) error {
})

// ins.Provider(api.ServiceProvider{})
ins.Provider(web.DefaultProvider(func(resolver infra.Resolver, router web.Router, mw web.RequestMiddleware) {
router.Get("/", func(ctx web.Context) web.Response {
return ctx.JSON(web.M{"hello": ctx.InputWithDefault("name", "world")})
})
}))
ins.Provider(web.DefaultProvider(
func(resolver infra.Resolver, router web.Router, mw web.RequestMiddleware) {
router.Get("/", func(ctx web.Context) web.Response {
return ctx.JSON(web.M{"hello": ctx.InputWithDefault("name", "world")})
})
},
web.SetMuxRouteHandlerOption(func(resolver infra.Resolver, router *mux.Router) {
router.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
}),
web.SetServerConfigOption(func(server *http.Server, listener net.Listener) {
//server.ReadTimeout = 10 * time.Second
}),
))

//ins.Provider(web.Provider(
// listener.FlagContext("listen"),
Expand Down Expand Up @@ -132,7 +147,7 @@ func run(ins *app.App) error {
}

// 10s 后自动关闭服务
go time.AfterFunc(10*time.Second, gf.Shutdown)
// go time.AfterFunc(10*time.Second, gf.Shutdown)
})

return nil
Expand Down
10 changes: 5 additions & 5 deletions framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"sync"
"time"

"github.com/mylxsw/container"
"github.com/mylxsw/glacier/infra"
"github.com/mylxsw/go-ioc"
)

// framework is the Glacier framework
type framework struct {
version string
startTime time.Time

cc container.Container
cc ioc.Container
logger infra.Logger

lock sync.RWMutex
Expand Down Expand Up @@ -139,9 +139,9 @@ func (impl *framework) Prototype(ins ...interface{}) infra.Glacier {
return impl
}

// ResolveWithError is a proxy to container's ResolveWithError function
func (impl *framework) ResolveWithError(resolver interface{}) error {
return impl.cc.ResolveWithError(resolver)
// Resolve is a proxy to container's Resolve function
func (impl *framework) Resolve(resolver interface{}) error {
return impl.cc.Resolve(resolver)
}

// MustResolve is a proxy to container's MustResolve function
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ require (
github.com/gorilla/schema v1.2.0
github.com/gorilla/sessions v1.2.1
github.com/gorilla/websocket v1.4.2
github.com/mylxsw/container v1.0.0
github.com/mylxsw/go-utils v1.0.1
github.com/mylxsw/go-ioc v1.1.0
github.com/mylxsw/go-utils v1.0.2
github.com/pkg/errors v0.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/urfave/cli v1.22.5
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mylxsw/container v1.0.0 h1:GIfdVWPcQPxxTW1N9mNrrY+b9KEGlvZXxL13UPX2iX8=
github.com/mylxsw/container v1.0.0/go.mod h1:UDbF8EtqT7jB0yDc6g9u4P8ORvw4RbFot1QputpgR5U=
github.com/mylxsw/go-utils v1.0.0 h1:ZiDRGrKvtMbMw3Fm/sS8siBLZ2rQ/7YdBL12z2bSsPI=
github.com/mylxsw/go-utils v1.0.0/go.mod h1:GIywna5jA7dodjmre1qXJYLpKg1x6N/foN6BSqc9jgs=
github.com/mylxsw/go-ioc v1.1.0 h1:oBHGkeIl511UTw22wM4wNJlsvQjheyLsfo8K5p4hp+U=
github.com/mylxsw/go-ioc v1.1.0/go.mod h1:HUGesamRKCt0Rd7DEPYKfj+ZJuY7rtjiYuZLNGasekk=
github.com/mylxsw/go-utils v1.0.1 h1:jKW/lcT+A0GA+Ct3O4MG+P9kE+3jWibobY8OfuuDvFE=
github.com/mylxsw/go-utils v1.0.1/go.mod h1:F5pQ/vTAgccZxQA7jsIBXM6m2INAbqPKfzbNwQgqhzY=
github.com/mylxsw/go-utils v1.0.2 h1:zsTqivsXi1poKyTCaiXrO2f8QWoAmx/KrgDj1M9YNDY=
github.com/mylxsw/go-utils v1.0.2/go.mod h1:F5pQ/vTAgccZxQA7jsIBXM6m2INAbqPKfzbNwQgqhzY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
14 changes: 7 additions & 7 deletions infra/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"
"time"

"github.com/mylxsw/container"
"github.com/mylxsw/go-ioc"
)

const (
Expand Down Expand Up @@ -159,22 +159,22 @@ type Glacier interface {

Singleton(ins ...interface{}) Glacier
Prototype(ins ...interface{}) Glacier
ResolveWithError(resolver interface{}) error
Resolve(resolver interface{}) error
MustResolve(resolver interface{})
Container() Container
Resolver() Resolver
Binder() Binder
}

type Container container.Container
type Binder container.Binder
type Resolver container.Resolver
type Container ioc.Container
type Binder ioc.Binder
type Resolver ioc.Resolver

type Hook interface {
// OnServerReady call a function a server ready
OnServerReady(ffs ...interface{})
}

func WithCondition(init interface{}, onCondition interface{}) container.Conditional {
return container.WithCondition(init, onCondition)
func WithCondition(init interface{}, onCondition interface{}) ioc.Conditional {
return ioc.WithCondition(init, onCondition)
}
2 changes: 1 addition & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type asyncJob struct {
}

func (aj asyncJob) Call(resolver infra.Resolver) error {
return resolver.ResolveWithError(aj.fn)
return resolver.Resolve(aj.fn)
}

// Async 添加一个异步执行函数
Expand Down
2 changes: 1 addition & 1 deletion scheduler/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (c *schedulerImpl) Add(name string, plan string, handler interface{}) error
}
}
}()
if err := c.resolver.ResolveWithError(hh.Handle); err != nil {
if err := c.resolver.Resolve(hh.Handle); err != nil {
log.Errorf("[glacier] cron job [%s] failed, Err: %v, Stack: \n%s", name, err, debug.Stack())
}
}
Expand Down
4 changes: 2 additions & 2 deletions scheduler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func newHandler(handler interface{}) JobHandler {
}

func (h jobHandlerImpl) Handle(resolver infra.Resolver) error {
return resolver.ResolveWithError(h.handler)
return resolver.Resolve(h.handler)
}

// WithoutOverlap 可以避免当前任务执行时间过长时,同一任务同时存在多个运行实例的问题
Expand Down Expand Up @@ -47,7 +47,7 @@ func (handler *OverlapJobHandler) Handle(resolver infra.Resolver) error {
select {
case handler.executing <- struct{}{}:
defer func() { <-handler.executing }()
return resolver.ResolveWithError(handler.handler)
return resolver.Resolve(handler.handler)
default:
if handler.skipCallback != nil {
handler.skipCallback()
Expand Down
8 changes: 4 additions & 4 deletions stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/mylxsw/glacier/graceful"
"github.com/mylxsw/glacier/log"
"github.com/mylxsw/go-ioc"

"github.com/mylxsw/container"
"github.com/mylxsw/glacier/infra"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func (impl *framework) diBindStage(ctx context.Context, flagCtx infra.FlagContex
impl.pushGraphvizNode("create container", false)
}

impl.cc = container.NewWithContext(ctx)
impl.cc = ioc.NewWithContext(ctx)

impl.cc.MustBindValue(infra.VersionKey, impl.version)
impl.cc.MustBindValue(infra.StartupTimeKey, impl.startTime)
Expand Down Expand Up @@ -128,7 +128,7 @@ func (impl *framework) Start(flagCtx infra.FlagContext) error {
impl.initStage(flagCtx)
impl.diBindStage(ctx, flagCtx)

return impl.cc.ResolveWithError(func(resolver infra.Resolver, gf infra.Graceful, conf *Config) error {
return impl.cc.Resolve(func(resolver infra.Resolver, gf infra.Graceful, conf *Config) error {
gf.AddShutdownHandler(cancel)

// 设置服务关闭钩子
Expand Down Expand Up @@ -241,7 +241,7 @@ func (impl *framework) readyStage(resolver infra.Resolver, gf infra.Graceful) {

go func(hook namedFunc) {
defer wg.Done()
if err := resolver.ResolveWithError(hook.fn); err != nil {
if err := resolver.Resolve(hook.fn); err != nil {
log.Errorf("[glacier] onServerReady hook [%s] failed: %v", hook.name, err)
}
}(hook)
Expand Down
4 changes: 2 additions & 2 deletions starter/app/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (app *App) Prototype(ins ...interface{}) *App {
return app
}

func (app *App) ResolveWithError(resolver interface{}) error {
return app.gcr.ResolveWithError(resolver)
func (app *App) Resolve(resolver interface{}) error {
return app.gcr.Resolve(resolver)
}

func (app *App) MustResolve(resolver interface{}) {
Expand Down
22 changes: 10 additions & 12 deletions web/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@ import (

type RouteHandler func(resolver infra.Resolver, router Router, mw RequestMiddleware)
type MuxRouteHandler func(resolver infra.Resolver, router *mux.Router)
type ListenerHandler func(server *http.Server, listener net.Listener)
type ServerConfigHandler func(server *http.Server, listener net.Listener)
type InitHandler func(resolver infra.Resolver, webServer Server, conf *Config) error

type Config struct {
routeHandler RouteHandler
listenerHandler ListenerHandler
muxRouteHandler MuxRouteHandler
initHandler InitHandler
exceptionHandler ExceptionHandler
routeHandler RouteHandler
serverConfigHandler ServerConfigHandler
muxRouteHandler MuxRouteHandler
initHandler InitHandler
exceptionHandler ExceptionHandler

MultipartFormMaxMemory int64 // Multipart-form 解析占用最大内存
ViewTemplatePathPrefix string // 视图模板目录
TempDir string // 临时目录,用于上传文件等
TempFilePattern string // 临时文件规则
IgnoreLastSlash bool // 是否忽略 URL 末尾的 /

HttpWriteTimeout time.Duration
HttpReadTimeout time.Duration
HttpIdleTimeout time.Duration
HttpWriteTimeout time.Duration
HttpIdleTimeout time.Duration
HttpReadTimeout time.Duration
HttpReadHeaderTimeout time.Duration
}

// DefaultConfig create a default config
Expand All @@ -40,8 +41,5 @@ func DefaultConfig() *Config {
TempDir: "/tmp",
TempFilePattern: "glacier-files-",
IgnoreLastSlash: false,
HttpWriteTimeout: time.Second * 15,
HttpReadTimeout: time.Second * 15,
HttpIdleTimeout: time.Second * 60,
}
}
10 changes: 5 additions & 5 deletions web/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (

"github.com/gorilla/sessions"
"github.com/gorilla/websocket"
"github.com/mylxsw/container"
"github.com/mylxsw/go-ioc"
"github.com/pkg/errors"
)

// WebContext 作为一个web请求的上下文信息
type WebContext struct {
response *HttpResponse
request *HttpRequest
cc container.Container
cc ioc.Container
conf Config
}

type webHandler struct {
handle WebHandler
container container.Container
container ioc.Container
router *routerImpl
conf *Config
}
Expand Down Expand Up @@ -81,8 +81,8 @@ func (ctx *WebContext) Response() ResponseCreator {
return ctx.response
}

// Container return underlying container.Container
func (ctx *WebContext) Container() container.Container {
// Container return underlying ioc.Container
func (ctx *WebContext) Container() ioc.Container {
return ctx.cc
}

Expand Down
4 changes: 2 additions & 2 deletions web/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/mylxsw/container"
"github.com/mylxsw/go-ioc"
)

type Context interface {
Expand Down Expand Up @@ -71,7 +71,7 @@ type Context interface {
Session() *sessions.Session
Request() Request
Response() ResponseCreator
Container() container.Container
Container() ioc.Container
View(tplPath string, data interface{}) *HTMLResponse
Validate(validator Validator, jsonResponse bool)

Expand Down
6 changes: 3 additions & 3 deletions web/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func SetInitHandlerOption(h InitHandler) Option {
}
}

// SetListenerHandlerOption 服务初始化阶段,web 服务对象已经创建,此时不能再更新 web 配置了
func SetListenerHandlerOption(h ListenerHandler) Option {
// SetServerConfigOption 服务初始化阶段,web 服务对象已经创建,此时不能再更新 web 配置了
func SetServerConfigOption(h ServerConfigHandler) Option {
return func(cc infra.Resolver, conf *Config) {
conf.listenerHandler = h
conf.serverConfigHandler = h
}
}

Expand Down
4 changes: 2 additions & 2 deletions web/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package web
import (
"context"

"github.com/mylxsw/container"
"github.com/mylxsw/glacier/infra"
"github.com/mylxsw/glacier/listener"
"github.com/mylxsw/go-ioc"
)

type provider struct {
Expand Down Expand Up @@ -33,7 +33,7 @@ func Provider(builder infra.ListenerBuilder, options ...Option) infra.DaemonProv
}

func (p *provider) Register(app infra.Binder) {
app.MustSingletonOverride(func(cc container.Container) Server {
app.MustSingletonOverride(func(cc ioc.Container) Server {
return NewServer(cc, p.options...)
})
app.MustSingletonOverride(func() infra.ListenerBuilder {
Expand Down
4 changes: 2 additions & 2 deletions web/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/gorilla/sessions"
"github.com/mylxsw/container"
"github.com/mylxsw/go-ioc"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
Expand All @@ -26,7 +26,7 @@ type HttpRequest struct {
r *http.Request
body []byte
session *sessions.Session
cc container.Container
cc ioc.Container
router *routerImpl
conf Config
}
Expand Down
Loading

0 comments on commit 1e3fa21

Please sign in to comment.