123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package main
- import (
- "encoding/xml"
- "fmt"
- "github.com/go-openapi/spec"
- "io/ioutil"
- "net/http"
- "os/exec"
- "runtime"
- "strconv"
- "strings"
- "time"
- )
- func main() {
- projName := "i2-erp-backend"
- app := NewEngineClient(projName, "http://gen.qianqiusoft.com").GenerateCurrentProject()
- if app == nil {
- app = &XmlApplication{}
- bytess, _ := ioutil.ReadFile("test.xml")
- err := xml.Unmarshal(bytess, app)
- if err != nil {
- fmt.Println("xml.Unmarshal(bytess, &app) error " + err.Error())
- return
- }
- }
- swagger := spec.Swagger{}
- swagger.Swagger = "2.0"
- swagger.Info = &spec.Info{
- InfoProps: spec.InfoProps{Title: app.ApplicationName},
- }
- swagger.Host = "localhost:8086"
- swagger.BasePath = "/api/v1/"
- tags := make([]spec.Tag, len(app.Controllers.ControllerList))
- for i, controller := range app.Controllers.ControllerList {
- tags[i].Name = controller.Name
- tags[i].Description = controller.Desc + ",Dir:" + controller.Dir + ",SkipLogin:" + strconv.FormatBool(controller.SkipLogin)
- }
- swagger.Tags = tags
- swagger.Schemes = []string{"http"}
- swagger.Paths = new(spec.Paths)
- swagger.Paths.Paths = make(map[string]spec.PathItem)
- for _, controller := range app.Controllers.ControllerList {
- tag := []string{controller.Name}
- for _, api := range controller.Apis {
- pathItem := new(spec.PathItem)
- operation := new(spec.Operation)
- operation.ID = api.Name
- operation.Summary = api.Desc
- operation.Description = "Function:" + api.Function + ", Table:" + api.Table
- //method
- {
- switch strings.ToLower(api.Method) {
- case "get":
- pathItem.Get = operation
- case "put":
- pathItem.Put = operation
- case "post":
- pathItem.Post = operation
- case "delete":
- pathItem.Delete = operation
- case "options":
- pathItem.Options = operation
- case "head":
- pathItem.Head = operation
- case "patch":
- pathItem.Patch = operation
- }
- }
- //params
- {
- params := make([]spec.Parameter, len(api.ParamList))
- for pi, param := range api.ParamList {
- p := spec.Parameter{}
- //Name
- p.Name = param.Name
- //TransType
- //Type
- p.Type = param.Type + "|" + param.TransType
- //Desc
- //Ref
- p.Description = param.Desc + ", Ref:" + param.Ref
- //Must
- p.AllowEmptyValue = !param.Must
- //DefaultValue
- p.Default = param.DefaultValue
- params[pi] = p
- }
- pathItem.Parameters = params
- }
- //return
- {
- operation.Responses = &spec.Responses{}
- success := spec.Response{}
- success.Description = api.Return.Success.Desc
- success.AddExtension("ref", api.Return.Success.Ref)
- failure := spec.Response{}
- failure.Description = api.Return.Failure.Desc
- failure.AddExtension("ref", api.Return.Failure.Ref)
- operation.Responses.StatusCodeResponses = map[int]spec.Response{
- 200: success,
- -200: failure,
- }
- }
- operation.Tags = tag
- swagger.Paths.Paths["/"+controller.Name+"/"+api.Name] = *pathItem
- }
- }
- json, _ := swagger.MarshalJSON()
- http.Handle("/swagger.json", &Bs{json})
- http.Handle("/", http.FileServer(http.Dir("./vendor/git.i2edu.net/paddy.he/i2-erp-swagger/static")))
- go func() {
- time.Sleep(300)
- err := Open("http://localhost:8087/dist")
- if err != nil {
- fmt.Println(err.Error())
- }
- }()
- fmt.Println("listen port 8087 for swagger")
- err := http.ListenAndServe(":8087", nil)
- if err != nil {
- fmt.Println(err.Error())
- }
- }
- var commands = map[string][]string{
- "windows": {"cmd", "/c", "start"},
- "darwin": {"open"},
- "linux": {"xdg-open"},
- }
- var Version = "0.1.0"
- func Open(uri string) error {
- run, ok := commands[runtime.GOOS]
- if !ok {
- return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS)
- }
- cmd := exec.Command(run[0], append(run[1:], uri)...)
- return cmd.Start()
- }
- type Bs struct {
- V []byte
- }
- func (p *Bs) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- _, err := w.Write(p.V)
- if err != nil {
- fmt.Println(err)
- }
- }
|