main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/go-openapi/spec"
  6. "io/ioutil"
  7. "net/http"
  8. "os/exec"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. func main() {
  15. projName := "i2-erp-backend"
  16. app := NewEngineClient(projName, "http://gen.qianqiusoft.com").GenerateCurrentProject()
  17. if app == nil {
  18. app = &XmlApplication{}
  19. bytess, _ := ioutil.ReadFile("test.xml")
  20. err := xml.Unmarshal(bytess, app)
  21. if err != nil {
  22. fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
  23. return
  24. }
  25. }
  26. swagger := spec.Swagger{}
  27. swagger.Swagger = "2.0"
  28. swagger.Info = &spec.Info{
  29. InfoProps: spec.InfoProps{Title: app.ApplicationName},
  30. }
  31. swagger.Host = "localhost:8086"
  32. swagger.BasePath = "/api/v1/"
  33. tags := make([]spec.Tag, len(app.Controllers.ControllerList))
  34. for i, controller := range app.Controllers.ControllerList {
  35. tags[i].Name = controller.Name
  36. tags[i].Description = controller.Desc + ",Dir:" + controller.Dir + ",SkipLogin:" + strconv.FormatBool(controller.SkipLogin)
  37. }
  38. swagger.Tags = tags
  39. swagger.Schemes = []string{"http"}
  40. swagger.Paths = new(spec.Paths)
  41. swagger.Paths.Paths = make(map[string]spec.PathItem)
  42. for _, controller := range app.Controllers.ControllerList {
  43. tag := []string{controller.Name}
  44. for _, api := range controller.Apis {
  45. pathItem := new(spec.PathItem)
  46. operation := new(spec.Operation)
  47. operation.ID = api.Name
  48. operation.Summary = api.Desc
  49. operation.Description = "Function:" + api.Function + ", Table:" + api.Table
  50. //method
  51. {
  52. switch strings.ToLower(api.Method) {
  53. case "get":
  54. pathItem.Get = operation
  55. case "put":
  56. pathItem.Put = operation
  57. case "post":
  58. pathItem.Post = operation
  59. case "delete":
  60. pathItem.Delete = operation
  61. case "options":
  62. pathItem.Options = operation
  63. case "head":
  64. pathItem.Head = operation
  65. case "patch":
  66. pathItem.Patch = operation
  67. }
  68. }
  69. //params
  70. {
  71. params := make([]spec.Parameter, len(api.ParamList))
  72. for pi, param := range api.ParamList {
  73. p := spec.Parameter{}
  74. //Name
  75. p.Name = param.Name
  76. //TransType
  77. //Type
  78. p.Type = param.Type + "|" + param.TransType
  79. //Desc
  80. //Ref
  81. p.Description = param.Desc + ", Ref:" + param.Ref
  82. //Must
  83. p.AllowEmptyValue = !param.Must
  84. //DefaultValue
  85. p.Default = param.DefaultValue
  86. params[pi] = p
  87. }
  88. pathItem.Parameters = params
  89. }
  90. //return
  91. {
  92. operation.Responses = &spec.Responses{}
  93. success := spec.Response{}
  94. success.Description = api.Return.Success.Desc
  95. success.AddExtension("ref", api.Return.Success.Ref)
  96. failure := spec.Response{}
  97. failure.Description = api.Return.Failure.Desc
  98. failure.AddExtension("ref", api.Return.Failure.Ref)
  99. operation.Responses.StatusCodeResponses = map[int]spec.Response{
  100. 200: success,
  101. -200: failure,
  102. }
  103. }
  104. operation.Tags = tag
  105. swagger.Paths.Paths["/"+controller.Name+"/"+api.Name] = *pathItem
  106. }
  107. }
  108. json, _ := swagger.MarshalJSON()
  109. http.Handle("/swagger.json", &Bs{json})
  110. http.Handle("/", http.FileServer(http.Dir("./vendor/git.i2edu.net/paddy.he/i2-erp-swagger/static")))
  111. go func() {
  112. time.Sleep(300)
  113. err := Open("http://localhost:8087/dist")
  114. if err != nil {
  115. fmt.Println(err.Error())
  116. }
  117. }()
  118. fmt.Println("listen port 8087 for swagger")
  119. err := http.ListenAndServe(":8087", nil)
  120. if err != nil {
  121. fmt.Println(err.Error())
  122. }
  123. }
  124. var commands = map[string][]string{
  125. "windows": {"cmd", "/c", "start"},
  126. "darwin": {"open"},
  127. "linux": {"xdg-open"},
  128. }
  129. var Version = "0.1.0"
  130. func Open(uri string) error {
  131. run, ok := commands[runtime.GOOS]
  132. if !ok {
  133. return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
  134. }
  135. cmd := exec.Command(run[0], append(run[1:], uri)...)
  136. return cmd.Start()
  137. }
  138. type Bs struct {
  139. V []byte
  140. }
  141. func (p *Bs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  142. _, err := w.Write(p.V)
  143. if err != nil {
  144. fmt.Println(err)
  145. }
  146. }