template.go 605 B

12345678910111213141516171819202122232425262728293031
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "io/ioutil"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. )
  9. func GetTemplate(c *gin.Context) {
  10. name := c.Param("name")
  11. path := filepath.Join("template", name)
  12. content, err := ioutil.ReadFile(path)
  13. if err != nil {
  14. if os.IsNotExist(err) {
  15. c.JSON(http.StatusNotFound, gin.H{
  16. "message": err.Error(),
  17. })
  18. return
  19. }
  20. ErrorHandler(c, err)
  21. return
  22. }
  23. c.JSON(http.StatusOK, gin.H{
  24. "message": "ok",
  25. "template": string(content),
  26. })
  27. }