yjp před 1 rokem
rodič
revize
df29deed59

+ 0 - 76
examples/examples/binding/main.go

@@ -1,76 +0,0 @@
-package main
-
-import (
-	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/application"
-	"git.sxidc.com/go-framework/baize/convenient/entity"
-	"git.sxidc.com/go-framework/baize/examples/example_domain/class"
-	"git.sxidc.com/go-framework/baize/infrastructure"
-	"git.sxidc.com/go-framework/baize/infrastructure/database/operations"
-	DEATH "github.com/vrecan/death"
-	"syscall"
-)
-
-// curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
-// curl -X PUT -H "Content-Type: application/json" -d '{"id":"1a8d5cf5c4574430903e7cfcf2f13e4f", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
-// curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
-// curl -X GET "http://localhost:10100/test/v1/class/get?id=1a8d5cf5c4574430903e7cfcf2f13e4f"
-// curl -X DELETE "http://localhost:10100/test/v1/class/1a8d5cf5c4574430903e7cfcf2f13e4f/delete"
-
-func main() {
-	app := baize.NewApplication(application.Config{
-		ApiConfig: application.ApiConfig{
-			UrlPrefix: "test",
-			Port:      "10100",
-		},
-		InfrastructureConfig: application.InfrastructureConfig{
-			Database: infrastructure.DatabaseConfig{
-				Operations: &operations.Config{
-					UserName:           "test",
-					Password:           "123456",
-					Address:            "localhost",
-					Port:               "30432",
-					Database:           "test",
-					MaxConnections:     40,
-					MaxIdleConnections: 10,
-				},
-			},
-		},
-	})
-
-	defer func() {
-		baize.DestroyApplication(app)
-	}()
-
-	app.Api().
-		PrefixRouter().
-		RegisterVersionedRouter("v1")
-
-	entity.BindSimple[class.Info](app.Binder(application.RouterPrefix, "v1"), &entity.Simple[class.Info]{
-		Entity:             &class.Entity{},
-		TableName:          class.TableName,
-		DomainPath:         "/class",
-		CreateJsonBody:     &class.CreateJsonBody{},
-		DeleteQueryParams:  &class.DeletePathParams{},
-		UpdateJsonBody:     &class.UpdateJsonBody{},
-		QueryQueryParams:   &class.QueryQueryParams{},
-		GetByIDQueryParams: &class.GetByIDQueryParams{},
-	}, entity.WithCreateTx[class.Info]())
-
-	go func() {
-		err := app.Start()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	defer func() {
-		err := app.Finish()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
-	_ = death.WaitForDeath()
-}

+ 0 - 75
examples/examples/binding_ds/main.go

@@ -1,75 +0,0 @@
-package main
-
-import (
-	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/application"
-	"git.sxidc.com/go-framework/baize/convenient/entity"
-	"git.sxidc.com/go-framework/baize/examples/example_domain/class"
-	"git.sxidc.com/go-framework/baize/infrastructure"
-	"git.sxidc.com/go-framework/baize/infrastructure/database/data_service"
-	DEATH "github.com/vrecan/death"
-	"syscall"
-)
-
-// curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
-// curl -X PUT -H "Content-Type: application/json" -d '{"id":"92cede40e5464ff79541418a7fc738ec", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
-// curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
-// curl -X GET "http://localhost:10100/test/v1/class/get?id=92cede40e5464ff79541418a7fc738ec"
-// curl -X DELETE "http://localhost:10100/test/v1/class/92cede40e5464ff79541418a7fc738ec/delete"
-
-func main() {
-	app := baize.NewApplication(application.Config{
-		ApiConfig: application.ApiConfig{
-			UrlPrefix: "test",
-			Port:      "10100",
-		},
-		InfrastructureConfig: application.InfrastructureConfig{
-			Database: infrastructure.DatabaseConfig{
-				DataService: &data_service.Config{
-					Token:       "8qe+uPgpQ2JWxu3lSyOx5NjX+INp5WsnoD1ZWb7PBm4=",
-					BaseUrl:     "http://localhost:10000",
-					GrpcAddress: "localhost:10001",
-					Namespace:   "baize",
-					DataSource:  "baize",
-					TimeoutSec:  30,
-				},
-			},
-		},
-	})
-
-	defer func() {
-		baize.DestroyApplication(app)
-	}()
-
-	app.Api().
-		PrefixRouter().
-		RegisterVersionedRouter("v1")
-
-	entity.BindSimple[class.Info](app.Binder(application.RouterPrefix, "v1"), &entity.Simple[class.Info]{
-		Entity:             &class.Entity{},
-		TableName:          class.TableName,
-		DomainPath:         "/class",
-		CreateJsonBody:     &class.CreateJsonBody{},
-		DeleteQueryParams:  &class.DeletePathParams{},
-		UpdateJsonBody:     &class.UpdateJsonBody{},
-		QueryQueryParams:   &class.QueryQueryParams{},
-		GetByIDQueryParams: &class.GetByIDQueryParams{},
-	}, entity.WithUpdateTx[class.Info](), entity.WithDeleteTx[class.Info]())
-
-	go func() {
-		err := app.Start()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	defer func() {
-		err := app.Finish()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
-	_ = death.WaitForDeath()
-}

+ 0 - 64
examples/examples/configuration/main.go

@@ -1,64 +0,0 @@
-package main
-
-import (
-	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/application"
-	"git.sxidc.com/go-framework/baize/convenient/domain/configuration"
-	"git.sxidc.com/go-framework/baize/infrastructure"
-	"git.sxidc.com/go-framework/baize/infrastructure/database/operations"
-	DEATH "github.com/vrecan/death"
-	"syscall"
-)
-
-// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test", "value":"test-value"}' "http://localhost:10100/test/v1/configuration/create"
-// curl -X GET "http://localhost:10100/test/v1/configuration/values?scope=global&group=test&pageNo=1&pageSize=1"
-// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test"}' "http://localhost:10100/test/v1/configuration/delete"
-
-func main() {
-	app := baize.NewApplication(application.Config{
-		ApiConfig: application.ApiConfig{
-			UrlPrefix: "test",
-			Port:      "10100",
-		},
-		InfrastructureConfig: application.InfrastructureConfig{
-			Database: infrastructure.DatabaseConfig{
-				Operations: &operations.Config{
-					UserName:           "test",
-					Password:           "123456",
-					Address:            "localhost",
-					Port:               "30432",
-					Database:           "test",
-					MaxConnections:     40,
-					MaxIdleConnections: 10,
-				},
-			},
-		},
-	})
-
-	defer func() {
-		baize.DestroyApplication(app)
-	}()
-
-	app.Api().
-		PrefixRouter().
-		RegisterVersionedRouter("v1")
-
-	configuration.BindConfiguration(app.Binder("v1"), configuration.WithSchema("test"))
-
-	go func() {
-		err := app.Start()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	defer func() {
-		err := app.Finish()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
-	_ = death.WaitForDeath()
-}

+ 1 - 1
examples/examples/project/application/application.go

@@ -34,7 +34,7 @@ type Service interface {
 }
 
 var applications = []Service{
-	&service.Version{}, &service.Class{},
+	&service.Version{}, &service.Configuration{}, &service.Class{},
 }
 
 func Start() error {

+ 2 - 2
examples/examples/project/application/service/service_info/class.go → examples/examples/project/application/domain/class/info.go

@@ -1,10 +1,10 @@
-package service_info
+package class
 
 import (
 	"git.sxidc.com/go-framework/baize/application"
 )
 
-type ClassInfo struct {
+type Info struct {
 	application.InfoIDField
 	Name       string `json:"name" sqlresult:"column:name"`
 	StudentNum int    `json:"studentNum" sqlresult:"column:student_num"`

+ 0 - 0
examples/example_domain/configuration/entity.go → examples/examples/project/application/domain/configuration/entity.go


+ 0 - 0
examples/example_domain/configuration/info.go → examples/examples/project/application/domain/configuration/info.go


+ 0 - 0
examples/example_domain/configuration/request_params.go → examples/examples/project/application/domain/configuration/request_params.go


+ 1 - 2
examples/examples/project/application/service/class.go

@@ -4,7 +4,6 @@ import (
 	"git.sxidc.com/go-framework/baize/application"
 	"git.sxidc.com/go-framework/baize/convenient/entity"
 	"git.sxidc.com/go-framework/baize/examples/examples/project/application/domain/class"
-	"git.sxidc.com/go-framework/baize/examples/examples/project/application/service/service_info"
 )
 
 type Class struct{}
@@ -21,7 +20,7 @@ func (app *Class) Destroy() error {
 func (app *Class) v1(appInstance *application.App) {
 	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
 
-	entity.BindSimple[service_info.ClassInfo](v1Binder, &entity.Simple[service_info.ClassInfo]{
+	entity.BindSimple[class.Info](v1Binder, &entity.Simple[class.Info]{
 		Entity:             &class.Entity{},
 		TableName:          class.TableName,
 		DomainPath:         "/class",

+ 22 - 0
examples/examples/project/application/service/configuration.go

@@ -0,0 +1,22 @@
+package service
+
+import (
+	"git.sxidc.com/go-framework/baize/application"
+	"git.sxidc.com/go-framework/baize/convenient/domain/configuration"
+)
+
+type Configuration struct{}
+
+func (app *Configuration) Init(appInstance *application.App) error {
+	app.prefixRoot(appInstance)
+	return nil
+}
+
+func (app *Configuration) Destroy() error {
+	return nil
+}
+
+func (app *Configuration) prefixRoot(appInstance *application.App) {
+	prefixRootBinder := appInstance.Binder(application.RouterPrefix, "")
+	configuration.BindConfiguration(prefixRootBinder, configuration.WithSchema("test"))
+}

+ 8 - 0
examples/examples/project/main.go

@@ -6,7 +6,15 @@ import (
 	"syscall"
 )
 
+// Version
 // curl -X GET "http://localhost:31000/example/version"
+
+// Configuration
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test", "value":"test-value"}' "http://localhost:31000/example/v1/configuration/create"
+// curl -X GET "http://localhost:31000/example/v1/configuration/values?scope=global&group=test&pageNo=1&pageSize=1"
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test"}' "http://localhost:31000/example/v1/configuration/delete"
+
+// Class
 // curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:31000/example/v1/class/create"
 // curl -X PUT -H "Content-Type: application/json" -d '{"id":"3ca731da91cc42c9b9f413c1b493133d", "name":"test-new"}' "http://localhost:31000/example/v1/class/update"
 // curl -X GET "http://localhost:31000/example/v1/class/query?name=test-new&pageNo=1&pageSize=1"

+ 43 - 22
examples/examples/quick_start/main.go

@@ -5,6 +5,11 @@ import (
 	"git.sxidc.com/go-framework/baize"
 	"git.sxidc.com/go-framework/baize/api"
 	"git.sxidc.com/go-framework/baize/application"
+	"git.sxidc.com/go-framework/baize/binding"
+	"git.sxidc.com/go-framework/baize/binding/request"
+	"git.sxidc.com/go-framework/baize/binding/response"
+	"git.sxidc.com/go-framework/baize/domain"
+	"git.sxidc.com/go-framework/baize/infrastructure"
 	DEATH "github.com/vrecan/death"
 	"net/http"
 	"syscall"
@@ -28,19 +33,27 @@ func main() {
 			fmt.Println("Global Before2")
 			c.Next()
 			fmt.Println("Global After2")
-		}).
-		AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
-			c.String(http.StatusOK, "pong")
-		}, func(c *api.Context) {
-			fmt.Println("Root Route Before1")
-			c.Next()
-			fmt.Println("Root Route After1")
-		}, func(c *api.Context) {
-			fmt.Println("Root Route Before2")
-			c.Next()
-			fmt.Println("Root Route After2")
 		})
 
+	rootBinder := app.Binder(application.RouterRoot, "")
+
+	binding.GetBind(rootBinder, &binding.SimpleBindItem[any]{
+		Path:         "/ping",
+		ResponseFunc: response.NoResponse,
+		ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+			c.String(http.StatusOK, "pong")
+			return nil, nil
+		},
+	}, func(c *api.Context) {
+		fmt.Println("Root Route Before1")
+		c.Next()
+		fmt.Println("Root Route After1")
+	}, func(c *api.Context) {
+		fmt.Println("Root Route Before2")
+		c.Next()
+		fmt.Println("Root Route After2")
+	})
+
 	app.Api().
 		PrefixRouter().
 		RegisterVersionedRouter("v1", func(c *api.Context) {
@@ -51,19 +64,27 @@ func main() {
 			fmt.Println("Global Before2")
 			c.Next()
 			fmt.Println("Global After2")
-		}).
-		AddRoute(http.MethodGet, "/ping", func(c *api.Context) {
-			c.String(http.StatusOK, "pong")
-		}, func(c *api.Context) {
-			fmt.Println("Versioned Route Before1")
-			c.Next()
-			fmt.Println("Versioned Route After1")
-		}, func(c *api.Context) {
-			fmt.Println("Versioned Route Before2")
-			c.Next()
-			fmt.Println("Versioned Route After2")
 		})
 
+	prefixRootBinder := app.Binder(application.RouterPrefix, "v1")
+
+	binding.GetBind(prefixRootBinder, &binding.SimpleBindItem[any]{
+		Path:         "/ping",
+		ResponseFunc: response.NoResponse,
+		ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
+			c.String(http.StatusOK, "pong")
+			return nil, nil
+		},
+	}, func(c *api.Context) {
+		fmt.Println("Versioned Route Before1")
+		c.Next()
+		fmt.Println("Versioned Route After1")
+	}, func(c *api.Context) {
+		fmt.Println("Versioned Route Before2")
+		c.Next()
+		fmt.Println("Versioned Route After2")
+	})
+
 	go func() {
 		if err := app.Start(); err != nil {
 			panic(err)

+ 0 - 72
examples/examples/value_object/main.go

@@ -1,72 +0,0 @@
-package main
-
-import (
-	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/application"
-	"git.sxidc.com/go-framework/baize/convenient/value_object"
-	"git.sxidc.com/go-framework/baize/examples/example_domain/configuration"
-	"git.sxidc.com/go-framework/baize/infrastructure"
-	"git.sxidc.com/go-framework/baize/infrastructure/database/operations"
-	DEATH "github.com/vrecan/death"
-	"syscall"
-)
-
-// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test", "value":"test-value"}' "http://localhost:10100/test/v1/configuration/create"
-// curl -X GET "http://localhost:10100/test/v1/configuration/query?scope=global&pageNo=1&pageSize=1"
-// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test"}' "http://localhost:10100/test/v1/configuration/delete"
-
-func main() {
-	app := baize.NewApplication(application.Config{
-		ApiConfig: application.ApiConfig{
-			UrlPrefix: "test",
-			Port:      "10100",
-		},
-		InfrastructureConfig: application.InfrastructureConfig{
-			Database: infrastructure.DatabaseConfig{
-				Operations: &operations.Config{
-					UserName:           "test",
-					Password:           "123456",
-					Address:            "localhost",
-					Port:               "30432",
-					Database:           "test",
-					MaxConnections:     40,
-					MaxIdleConnections: 10,
-				},
-			},
-		},
-	})
-
-	defer func() {
-		baize.DestroyApplication(app)
-	}()
-
-	app.Api().
-		PrefixRouter().
-		RegisterVersionedRouter("v1")
-
-	value_object.BindSimple[configuration.Info](app.Binder("v1"), &value_object.Simple[configuration.Info]{
-		ValueObject:       &configuration.Entity{},
-		TableName:         configuration.TableName,
-		DomainPath:        "/configuration",
-		CreateJsonBody:    &configuration.CreateConfigurationJsonBody{},
-		DeleteQueryParams: &configuration.DeleteConfigurationJsonBody{},
-		QueryQueryParams:  &configuration.GetConfigurationValuesQueryParams{},
-	})
-
-	go func() {
-		err := app.Start()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	defer func() {
-		err := app.Finish()
-		if err != nil {
-			panic(err)
-		}
-	}()
-
-	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
-	_ = death.WaitForDeath()
-}

+ 0 - 80
examples/resources.yaml

@@ -1,80 +0,0 @@
-kind: Namespace
-spec:
-  name: baize
-
----
-
-kind: DataSource
-spec:
-  type: database
-  namespace: baize
-  name: baize
-  spec:
-    type: postgres
-    user_name: test
-    password: "123456"
-    address: "10.0.0.84"
-    port: "30432"
-    database: test
-    max_connections: 40
-    max_idle_connections: 10
-
----
-
-kind: DataContainer
-spec:
-  namespace: baize
-  data_source: baize
-  name: test.classes
-  spec:
-    table_name: test.classes
-    columns:
-      - name: id
-        type: varchar(32)
-        comment: id
-        primary_key: true
-      - name: name
-        type: varchar(128)
-        comment: 班名
-        not_null: true
-        index: true
-      - name: student_num
-        type: integer
-        comment: 学生数量
-        not_null: true
-        index: true
-      - name: created_time
-        type: "timestamp with time zone"
-        comment: 创建时间
-        not_null: true
-      - name: last_updated_time
-        type: "timestamp with time zone"
-        comment: 最近更新时间
-        not_null: true
-
----
-
-kind: DataContainer
-spec:
-  namespace: baize
-  data_source: baize
-  name: test.configurations
-  spec:
-    table_name: test.configurations
-    columns:
-      - name: scope
-        type: varchar(256)
-        comment: 范围
-        not_null: true
-        primary_key: true
-      - name: group
-        type: varchar(256)
-        comment: 组
-        not_null: true
-        primary_key: true
-      - name: value
-        type: varchar(256)
-        comment: 值
-        not_null: true
-        index: true
-

+ 7 - 7
infrastructure/database/data_service/data_service.go

@@ -12,12 +12,12 @@ import (
 )
 
 type Config struct {
-	Token       string        `json:"token" yaml:"token"`
-	BaseUrl     string        `json:"base_url" yaml:"base_url"`
-	GrpcAddress string        `json:"grpc_address" yaml:"grpc_address"`
-	Namespace   string        `json:"namespace" yaml:"namespace"`
-	DataSource  string        `json:"data_source" yaml:"data_source"`
-	TimeoutSec  time.Duration `json:"timeout_sec" yaml:"timeout_sec"`
+	Token       string `json:"token" yaml:"token"`
+	BaseUrl     string `json:"base_url" yaml:"base_url"`
+	GrpcAddress string `json:"grpc_address" yaml:"grpc_address"`
+	Namespace   string `json:"namespace" yaml:"namespace"`
+	DataSource  string `json:"data_source" yaml:"data_source"`
+	TimeoutSec  int64  `json:"timeout_sec" yaml:"timeout_sec"`
 }
 
 type DataService struct {
@@ -27,7 +27,7 @@ type DataService struct {
 }
 
 func NewDataService(config *Config) (*DataService, error) {
-	c := client.New(config.TimeoutSec * time.Second)
+	c := client.New(time.Duration(config.TimeoutSec) * time.Second)
 
 	namespaceInfos, err := c.GetNamespaces(config.Token, config.BaseUrl, config.Namespace, 1, 1)
 	if err != nil {