template.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 GetDefaultSiteTemplate(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. content, err := ngxConfig.BuildConfig()
  38. if err != nil {
  39. api.ErrHandler(c, err)
  40. return
  41. }
  42. c.JSON(http.StatusOK, gin.H{
  43. "message": "ok",
  44. "template": content,
  45. "tokenized": ngxConfig,
  46. })
  47. }
  48. func GetTemplateConfList(c *gin.Context) {
  49. configList, err := template.GetTemplateList("conf")
  50. if err != nil {
  51. api.ErrHandler(c, err)
  52. return
  53. }
  54. c.JSON(http.StatusOK, gin.H{
  55. "data": configList,
  56. })
  57. }
  58. func GetTemplateBlockList(c *gin.Context) {
  59. configList, err := template.GetTemplateList("block")
  60. if err != nil {
  61. api.ErrHandler(c, err)
  62. return
  63. }
  64. c.JSON(http.StatusOK, gin.H{
  65. "data": configList,
  66. })
  67. }
  68. func GetTemplateBlock(c *gin.Context) {
  69. type resp struct {
  70. template.ConfigInfoItem
  71. template.ConfigDetail
  72. }
  73. var bindData map[string]template.Variable
  74. _ = c.ShouldBindJSON(&bindData)
  75. info := template.GetTemplateInfo("block", c.Param("name"))
  76. if bindData == nil {
  77. bindData = info.Variables
  78. }
  79. detail, err := template.ParseTemplate("block", c.Param("name"), bindData)
  80. if err != nil {
  81. api.ErrHandler(c, err)
  82. return
  83. }
  84. info.Variables = bindData
  85. c.JSON(http.StatusOK, resp{
  86. info,
  87. detail,
  88. })
  89. }