Gin: add gin into the framework

This commit is contained in:
Muyao CHEN 2024-09-29 23:50:00 +02:00
parent f0b688ff8c
commit f0fc7ee2cb
6 changed files with 16 additions and 24 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "framework/gin"]
path = framework/gin
url = gitea@git.vinchent.xyz:vinchent/gin.git

View File

@ -18,16 +18,14 @@ type Container interface {
// IsBound returns true the provider is bound to the container.
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.
Get(name string) (interface{}, error)
Make(name string) (interface{}, error)
// JustGet gets a service, return nil if error.
JustGet(name string) interface{}
// GetExisted gets an initiated service by its name.
GetExisted(name string) interface{}
// MustMake makes a service, supposing that it is not instantiated before.
// Return nil if error.
MustMake(name string) interface{}
// MakeNew creates a new instance of the service with different parameters.
//
@ -109,15 +107,11 @@ func (c *GoWebContainer) getServiceProvider(name string) ServiceProvider {
return nil
}
func (c *GoWebContainer) Get(name string) (interface{}, error) {
func (c *GoWebContainer) Make(name string) (interface{}, error) {
return c.makeServiceInstance(name, nil, false)
}
func (c *GoWebContainer) GetExisted(name string) interface{} {
return c.getServiceInstance(name)
}
func (c *GoWebContainer) JustGet(name string) interface{} {
func (c *GoWebContainer) MustMake(name string) interface{} {
ins, err := c.makeServiceInstance(name, nil, false)
if err != nil {
return nil

View File

@ -75,7 +75,7 @@ func TestBind(t *testing.T) {
container.Bind(provider)
dummyService := container.JustGet(DummyProviderName).(IDummyService)
dummyService := container.MustMake(DummyProviderName).(IDummyService)
want := DummyStruct{
Name: "Dummy!",

1
framework/gin Submodule

@ -0,0 +1 @@
Subproject commit f05f966a0824b1d302ee556183e2579c91954266

2
go.mod
View File

@ -35,3 +35,5 @@ require (
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace github.com/gin-gonic/gin => ./framework/gin

12
main.go
View File

@ -10,19 +10,11 @@ import (
"syscall"
"time"
"git.vinchent.xyz/vinchent/go-web/framework"
"git.vinchent.xyz/vinchent/go-web/providers/webserver"
"github.com/gin-gonic/gin"
)
func main() {
container := framework.NewGoWebContainer()
webServiceProvider := &webserver.WebSrvProvider{}
container.Bind(webServiceProvider)
goWebGin := container.JustGet(webserver.WebSrvName).(*webserver.GoWebGin)
core := goWebGin.Engine
core := gin.New()
registerRouter(core)
server := &http.Server{
Addr: ":8080",