template.go 1.7 KB

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