main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package main
  2. import (
  3. "git.sxidc.com/go-framework/baize"
  4. "git.sxidc.com/go-framework/baize/application"
  5. "git.sxidc.com/go-framework/baize/binding"
  6. "git.sxidc.com/go-framework/baize/binding/request"
  7. "git.sxidc.com/go-framework/baize/convenient/entity"
  8. "git.sxidc.com/go-framework/baize/domain"
  9. "git.sxidc.com/go-framework/baize/infrastructure"
  10. "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
  11. DEATH "github.com/vrecan/death"
  12. "syscall"
  13. "time"
  14. )
  15. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
  16. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"1a8d5cf5c4574430903e7cfcf2f13e4f", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
  17. // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
  18. // curl -X GET "http://localhost:10100/test/v1/class/get?id=1a8d5cf5c4574430903e7cfcf2f13e4f"
  19. // curl -X DELETE "http://localhost:10100/test/v1/class/1a8d5cf5c4574430903e7cfcf2f13e4f/delete"
  20. type CreateClassJsonBody struct {
  21. Name string `json:"name" binding:"required" assign:"toField:Name"`
  22. StudentNum int `json:"studentNum" binding:"required" assign:"toField:StudentNum"`
  23. }
  24. type DeleteClassPathParams struct {
  25. request.IDPath
  26. }
  27. type UpdateClassJsonBody struct {
  28. request.IDJsonBody
  29. Name string `json:"name" assign:"toField:Name"`
  30. StudentNum int `json:"studentNum" assign:"toField:StudentNum"`
  31. }
  32. type QueryClassesQueryParams struct {
  33. Name string `form:"name" assign:"toField:Name"`
  34. StudentNum int `form:"studentNum" assign:"toField:StudentNum"`
  35. request.BaseQuery
  36. }
  37. type GetClassQueryParams struct {
  38. request.IDQuery
  39. }
  40. type Class struct {
  41. domain.BaseEntity
  42. Name string `sqlmapping:"column:name"`
  43. StudentNum int `sqlmapping:"column:student_num"`
  44. CreatedTime time.Time
  45. LastUpdatedTime *time.Time
  46. }
  47. func (class *Class) DomainCNName() string {
  48. return "班级"
  49. }
  50. type InfoIDField struct {
  51. ID string `json:"id" sqlresult:"column:id"`
  52. }
  53. type ClassInfo struct {
  54. *InfoIDField
  55. Name string `json:"name" sqlresult:"column:name"`
  56. StudentNum int `json:"studentNum" sqlresult:"column:student_num"`
  57. CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  58. LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  59. }
  60. const (
  61. tableName = "test.classes"
  62. )
  63. func main() {
  64. app := baize.NewApplication(application.Config{
  65. ApiConfig: application.ApiConfig{
  66. UrlPrefix: "test",
  67. Port: "10100",
  68. },
  69. InfrastructureConfig: application.InfrastructureConfig{
  70. Database: infrastructure.DatabaseConfig{
  71. Operations: &operations.Config{
  72. UserName: "test",
  73. Password: "123456",
  74. Address: "localhost",
  75. Port: "30432",
  76. Database: "test",
  77. MaxConnections: 40,
  78. MaxIdleConnections: 10,
  79. },
  80. },
  81. },
  82. })
  83. defer func() {
  84. baize.DestroyApplication(app)
  85. }()
  86. dbOperations := app.Infrastructure().DBOperations()
  87. err := dbOperations.AutoMigrate(operations.Table{
  88. TableName: tableName,
  89. Columns: []operations.TableColumn{
  90. {
  91. Name: "id",
  92. Type: "varchar(32)",
  93. Comment: "id",
  94. PrimaryKey: true,
  95. },
  96. {
  97. Name: "name",
  98. Type: "varchar(128)",
  99. Comment: "名称",
  100. NotNull: true,
  101. Index: true,
  102. },
  103. {
  104. Name: "student_num",
  105. Type: "integer",
  106. Comment: "学生数量",
  107. NotNull: true,
  108. Index: true,
  109. },
  110. {
  111. Name: "created_time",
  112. Type: "timestamp with time zone",
  113. Comment: "创建时间",
  114. NotNull: true,
  115. Index: true,
  116. },
  117. {
  118. Name: "last_updated_time",
  119. Type: "timestamp with time zone",
  120. Comment: "更新时间",
  121. NotNull: true,
  122. Index: true,
  123. },
  124. },
  125. })
  126. if err != nil {
  127. panic(err)
  128. }
  129. app.Api().
  130. PrefixRouter().
  131. RegisterVersionedRouter("v1")
  132. entity.BindSimple[ClassInfo](app.Binder("v1"), &entity.Simple[ClassInfo]{
  133. Entity: &Class{},
  134. TableName: tableName,
  135. DBExecutorType: binding.DBExecutorOperations,
  136. DomainPath: "/class",
  137. CreateJsonBody: &CreateClassJsonBody{},
  138. DeleteQueryParams: &DeleteClassPathParams{},
  139. UpdateJsonBody: &UpdateClassJsonBody{},
  140. QueryParams: &QueryClassesQueryParams{},
  141. GetByIDQueryParams: &GetClassQueryParams{},
  142. }, entity.WithCreateTx[ClassInfo]())
  143. go func() {
  144. err := app.Start()
  145. if err != nil {
  146. panic(err)
  147. }
  148. }()
  149. defer func() {
  150. err := app.Finish()
  151. if err != nil {
  152. panic(err)
  153. }
  154. }()
  155. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  156. _ = death.WaitForDeath()
  157. }