license.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package license
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/uozi-tech/cosy"
  6. "github.com/0xJacky/Nginx-UI/internal/license"
  7. )
  8. type Controller struct{}
  9. func InitRouter(r *gin.RouterGroup) {
  10. c := NewController()
  11. licenseGroup := r.Group("/licenses")
  12. {
  13. licenseGroup.GET("", c.GetLicenses)
  14. licenseGroup.GET("/backend", c.GetBackendLicenses)
  15. licenseGroup.GET("/frontend", c.GetFrontendLicenses)
  16. licenseGroup.GET("/stats", c.GetLicenseStats)
  17. }
  18. }
  19. func NewController() *Controller {
  20. return &Controller{}
  21. }
  22. // GetLicenses godoc
  23. // @Summary Get all open source component licenses
  24. // @Description Returns license information for all backend and frontend components
  25. // @Tags License
  26. // @Accept json
  27. // @Produce json
  28. // @Success 200 {object} license.ComponentInfo "License information"
  29. // @Failure 500 {object} cosy.HTTPError "Internal Server Error"
  30. // @Router /api/licenses [get]
  31. func (c *Controller) GetLicenses(ctx *gin.Context) {
  32. info, err := license.GetLicenseInfo()
  33. if err != nil {
  34. cosy.ErrHandler(ctx, err)
  35. return
  36. }
  37. ctx.JSON(http.StatusOK, info)
  38. }
  39. // GetBackendLicenses godoc
  40. // @Summary Get backend component licenses
  41. // @Description Returns license information for backend Go modules
  42. // @Tags License
  43. // @Accept json
  44. // @Produce json
  45. // @Success 200 {array} license.License "Backend license information"
  46. // @Failure 500 {object} cosy.HTTPError "Internal Server Error"
  47. // @Router /api/licenses/backend [get]
  48. func (c *Controller) GetBackendLicenses(ctx *gin.Context) {
  49. licenses, err := license.GetBackendLicenses()
  50. if err != nil {
  51. cosy.ErrHandler(ctx, err)
  52. return
  53. }
  54. ctx.JSON(http.StatusOK, licenses)
  55. }
  56. // GetFrontendLicenses godoc
  57. // @Summary Get frontend component licenses
  58. // @Description Returns license information for frontend npm packages
  59. // @Tags License
  60. // @Accept json
  61. // @Produce json
  62. // @Success 200 {array} license.License "Frontend license information"
  63. // @Failure 500 {object} cosy.HTTPError "Internal Server Error"
  64. // @Router /api/licenses/frontend [get]
  65. func (c *Controller) GetFrontendLicenses(ctx *gin.Context) {
  66. licenses, err := license.GetFrontendLicenses()
  67. if err != nil {
  68. cosy.ErrHandler(ctx, err)
  69. return
  70. }
  71. ctx.JSON(http.StatusOK, licenses)
  72. }
  73. // GetLicenseStats godoc
  74. // @Summary Get license statistics
  75. // @Description Returns statistics about the distribution of licenses
  76. // @Tags License
  77. // @Accept json
  78. // @Produce json
  79. // @Success 200 {object} map[string]interface{} "License statistics"
  80. // @Failure 500 {object} cosy.HTTPError "Internal Server Error"
  81. // @Router /api/licenses/stats [get]
  82. func (c *Controller) GetLicenseStats(ctx *gin.Context) {
  83. stats, err := license.GetLicenseStats()
  84. if err != nil {
  85. cosy.ErrHandler(ctx, err)
  86. return
  87. }
  88. ctx.JSON(http.StatusOK, stats)
  89. }