main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/request"
  6. "git.sxidc.com/go-framework/baize/convenient/entity"
  7. "git.sxidc.com/go-framework/baize/domain"
  8. "git.sxidc.com/go-framework/baize/infrastructure"
  9. "git.sxidc.com/go-framework/baize/infrastructure/database/operations"
  10. DEATH "github.com/vrecan/death"
  11. "syscall"
  12. "time"
  13. )
  14. // curl -X POST -H "Content-Type: application/json" -d '{"name":"test", "studentNum": 10}' "http://localhost:10100/test/v1/class/create"
  15. // curl -X PUT -H "Content-Type: application/json" -d '{"id":"1a8d5cf5c4574430903e7cfcf2f13e4f", "name":"test-new"}' "http://localhost:10100/test/v1/class/update"
  16. // curl -X GET "http://localhost:10100/test/v1/class/query?name=test-new&pageNo=1&pageSize=1"
  17. // curl -X GET "http://localhost:10100/test/v1/class/get?id=1a8d5cf5c4574430903e7cfcf2f13e4f"
  18. // curl -X DELETE "http://localhost:10100/test/v1/class/1a8d5cf5c4574430903e7cfcf2f13e4f/delete"
  19. type CreateClassJsonBody struct {
  20. Name string `json:"name" binding:"required" assign:"toField:Name"`
  21. StudentNum int `json:"studentNum" binding:"required" assign:"toField:StudentNum"`
  22. }
  23. type DeleteClassPathParams struct {
  24. request.IDPath
  25. }
  26. type UpdateClassJsonBody struct {
  27. request.IDJsonBody
  28. Name string `json:"name" assign:"toField:Name"`
  29. StudentNum int `json:"studentNum" assign:"toField:StudentNum"`
  30. }
  31. type QueryClassesQueryParams struct {
  32. Name string `form:"name" assign:"toField:Name"`
  33. StudentNum int `form:"studentNum" assign:"toField:StudentNum"`
  34. request.BaseQuery
  35. }
  36. type GetClassQueryParams struct {
  37. request.IDQuery
  38. }
  39. type Class struct {
  40. domain.BaseEntity
  41. Name string `sqlmapping:"column:name"`
  42. StudentNum int `sqlmapping:"column:student_num"`
  43. CreatedTime time.Time
  44. LastUpdatedTime *time.Time
  45. }
  46. func (class *Class) DomainCNName() string {
  47. return "班级"
  48. }
  49. type InfoIDField struct {
  50. ID string `json:"id" sqlresult:"column:id"`
  51. }
  52. type ClassInfo struct {
  53. *InfoIDField
  54. Name string `json:"name" sqlresult:"column:name"`
  55. StudentNum int `json:"studentNum" sqlresult:"column:student_num"`
  56. CreatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  57. LastUpdatedTime string `sqlresult:"parseTime:'2006-01-02 15:04:05'"`
  58. }
  59. const (
  60. tableName = "test.classes"
  61. )
  62. func main() {
  63. app := baize.NewApplication(application.Config{
  64. ApiConfig: application.ApiConfig{
  65. UrlPrefix: "test",
  66. Port: "10100",
  67. },
  68. InfrastructureConfig: application.InfrastructureConfig{
  69. Database: infrastructure.DatabaseConfig{
  70. Operations: &operations.Config{
  71. UserName: "test",
  72. Password: "123456",
  73. Address: "localhost",
  74. Port: "30432",
  75. Database: "test",
  76. MaxConnections: 40,
  77. MaxIdleConnections: 10,
  78. },
  79. },
  80. },
  81. })
  82. defer func() {
  83. baize.DestroyApplication(app)
  84. }()
  85. app.Api().
  86. PrefixRouter().
  87. RegisterVersionedRouter("v1")
  88. entity.BindSimple[ClassInfo](app.Binder("v1"), &entity.Simple[ClassInfo]{
  89. Entity: &Class{},
  90. TableName: tableName,
  91. DomainPath: "/class",
  92. CreateJsonBody: &CreateClassJsonBody{},
  93. DeleteQueryParams: &DeleteClassPathParams{},
  94. UpdateJsonBody: &UpdateClassJsonBody{},
  95. QueryParams: &QueryClassesQueryParams{},
  96. GetByIDQueryParams: &GetClassQueryParams{},
  97. }, entity.WithCreateTx[ClassInfo]())
  98. go func() {
  99. err := app.Start()
  100. if err != nil {
  101. panic(err)
  102. }
  103. }()
  104. defer func() {
  105. err := app.Finish()
  106. if err != nil {
  107. panic(err)
  108. }
  109. }()
  110. death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
  111. _ = death.WaitForDeath()
  112. }