svg.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package http
  2. import (
  3. "bytes"
  4. "net/http"
  5. modsvg "go-common/app/admin/main/aegis/model/svg"
  6. bm "go-common/library/net/http/blademaster"
  7. "github.com/pkg/errors"
  8. )
  9. // HTMLContentType
  10. var (
  11. HTMLContentType = []string{"text/html"}
  12. _ Render = HTML{}
  13. )
  14. // HTML str.
  15. type HTML struct {
  16. Content []byte
  17. Title string
  18. }
  19. // WriteContentType fn
  20. func (j HTML) WriteContentType(w http.ResponseWriter) {
  21. writeContentType(w, HTMLContentType, j.Title, "html")
  22. }
  23. // Render (JSON) writes data with json ContentType.
  24. func (j HTML) Render(w http.ResponseWriter) (err error) {
  25. if _, err = w.Write(j.Content); err != nil {
  26. err = errors.WithStack(err)
  27. }
  28. return
  29. }
  30. func svg(c *bm.Context) {
  31. opt := new(struct {
  32. NetID int64 `form:"net_id" validate:"required"`
  33. Debug int8 `form:"debug"`
  34. })
  35. if err := c.Bind(opt); err != nil {
  36. return
  37. }
  38. var (
  39. nv *modsvg.NetView
  40. err error
  41. )
  42. if opt.Debug > 0 {
  43. nv = modsvg.DebugSVG()
  44. } else {
  45. if nv, err = srv.NetSVG(c, opt.NetID); err != nil {
  46. c.JSON(nil, err)
  47. return
  48. }
  49. }
  50. bs := bytes.NewBufferString("")
  51. nv.Execute(c.Writer, nv.Data)
  52. c.Render(http.StatusOK, CSV{
  53. Content: bs.Bytes(),
  54. Title: "流程网图",
  55. })
  56. }