Gin: add gin into the framework
This commit is contained in:
parent
f0b688ff8c
commit
f0fc7ee2cb
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[submodule "framework/gin"]
|
||||||
|
path = framework/gin
|
||||||
|
url = gitea@git.vinchent.xyz:vinchent/gin.git
|
@ -18,16 +18,14 @@ type Container interface {
|
|||||||
// IsBound returns true the provider is bound to the container.
|
// IsBound returns true the provider is bound to the container.
|
||||||
IsBound(name string) bool
|
IsBound(name string) bool
|
||||||
|
|
||||||
// Get gets or creates a service by its name.
|
// Make gets or creates a service by its name.
|
||||||
//
|
//
|
||||||
// Return error if the service cannot be initiated or doesn't exist.
|
// Return error if the service cannot be initiated or doesn't exist.
|
||||||
Get(name string) (interface{}, error)
|
Make(name string) (interface{}, error)
|
||||||
|
|
||||||
// JustGet gets a service, return nil if error.
|
// MustMake makes a service, supposing that it is not instantiated before.
|
||||||
JustGet(name string) interface{}
|
// Return nil if error.
|
||||||
|
MustMake(name string) interface{}
|
||||||
// GetExisted gets an initiated service by its name.
|
|
||||||
GetExisted(name string) interface{}
|
|
||||||
|
|
||||||
// MakeNew creates a new instance of the service with different parameters.
|
// MakeNew creates a new instance of the service with different parameters.
|
||||||
//
|
//
|
||||||
@ -109,15 +107,11 @@ func (c *GoWebContainer) getServiceProvider(name string) ServiceProvider {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GoWebContainer) Get(name string) (interface{}, error) {
|
func (c *GoWebContainer) Make(name string) (interface{}, error) {
|
||||||
return c.makeServiceInstance(name, nil, false)
|
return c.makeServiceInstance(name, nil, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GoWebContainer) GetExisted(name string) interface{} {
|
func (c *GoWebContainer) MustMake(name string) interface{} {
|
||||||
return c.getServiceInstance(name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *GoWebContainer) JustGet(name string) interface{} {
|
|
||||||
ins, err := c.makeServiceInstance(name, nil, false)
|
ins, err := c.makeServiceInstance(name, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -75,7 +75,7 @@ func TestBind(t *testing.T) {
|
|||||||
|
|
||||||
container.Bind(provider)
|
container.Bind(provider)
|
||||||
|
|
||||||
dummyService := container.JustGet(DummyProviderName).(IDummyService)
|
dummyService := container.MustMake(DummyProviderName).(IDummyService)
|
||||||
|
|
||||||
want := DummyStruct{
|
want := DummyStruct{
|
||||||
Name: "Dummy!",
|
Name: "Dummy!",
|
||||||
|
1
framework/gin
Submodule
1
framework/gin
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit f05f966a0824b1d302ee556183e2579c91954266
|
2
go.mod
2
go.mod
@ -35,3 +35,5 @@ require (
|
|||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace github.com/gin-gonic/gin => ./framework/gin
|
||||||
|
12
main.go
12
main.go
@ -10,19 +10,11 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.vinchent.xyz/vinchent/go-web/framework"
|
"github.com/gin-gonic/gin"
|
||||||
"git.vinchent.xyz/vinchent/go-web/providers/webserver"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
container := framework.NewGoWebContainer()
|
core := gin.New()
|
||||||
webServiceProvider := &webserver.WebSrvProvider{}
|
|
||||||
|
|
||||||
container.Bind(webServiceProvider)
|
|
||||||
|
|
||||||
goWebGin := container.JustGet(webserver.WebSrvName).(*webserver.GoWebGin)
|
|
||||||
|
|
||||||
core := goWebGin.Engine
|
|
||||||
registerRouter(core)
|
registerRouter(core)
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: ":8080",
|
Addr: ":8080",
|
||||||
|
Loading…
Reference in New Issue
Block a user