template.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package template
  2. import (
  3. "github.com/0xJacky/Nginx-UI/api"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/0xJacky/Nginx-UI/internal/template"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. )
  9. func GetTemplate(c *gin.Context) {
  10. var ngxConfig *nginx.NgxConfig
  11. ngxConfig = &nginx.NgxConfig{
  12. Servers: []*nginx.NgxServer{
  13. {
  14. Directives: []*nginx.NgxDirective{
  15. {
  16. Directive: "listen",
  17. Params: "80",
  18. },
  19. {
  20. Directive: "listen",
  21. Params: "[::]:80",
  22. },
  23. {
  24. Directive: "server_name",
  25. },
  26. {
  27. Directive: "root",
  28. },
  29. {
  30. Directive: "index",
  31. },
  32. },
  33. Locations: []*nginx.NgxLocation{},
  34. },
  35. },
  36. }
  37. c.JSON(http.StatusOK, gin.H{
  38. "message": "ok",
  39. "template": ngxConfig.BuildConfig(),
  40. "tokenized": ngxConfig,
  41. })
  42. }
  43. func GetTemplateConfList(c *gin.Context) {
  44. configList, err := template.GetTemplateList("conf")
  45. if err != nil {
  46. api.ErrHandler(c, err)
  47. return
  48. }
  49. c.JSON(http.StatusOK, gin.H{
  50. "data": configList,
  51. })
  52. }
  53. func GetTemplateBlockList(c *gin.Context) {
  54. configList, err := template.GetTemplateList("block")
  55. if err != nil {
  56. api.ErrHandler(c, err)
  57. return
  58. }
  59. c.JSON(http.StatusOK, gin.H{
  60. "data": configList,
  61. })
  62. }
  63. func GetTemplateBlock(c *gin.Context) {
  64. type resp struct {
  65. template.ConfigInfoItem
  66. template.ConfigDetail
  67. }
  68. var bindData map[string]template.Variable
  69. _ = c.ShouldBindJSON(&bindData)
  70. info := template.GetTemplateInfo("block", c.Param("name"))
  71. if bindData == nil {
  72. bindData = info.Variables
  73. }
  74. detail, err := template.ParseTemplate("block", c.Param("name"), bindData)
  75. if err != nil {
  76. api.ErrHandler(c, err)
  77. return
  78. }
  79. info.Variables = bindData
  80. c.JSON(http.StatusOK, resp{
  81. info,
  82. detail,
  83. })
  84. }