|
| 1 | +package pkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + gps "github.com/kevincobain2000/go-progress-svg" |
| 7 | + "github.com/labstack/echo/v4" |
| 8 | + "github.com/mcuadros/go-defaults" |
| 9 | +) |
| 10 | + |
| 11 | +type APIHandler struct { |
| 12 | +} |
| 13 | + |
| 14 | +func NewAPIHandler() *APIHandler { |
| 15 | + return &APIHandler{} |
| 16 | +} |
| 17 | + |
| 18 | +type APIRequest struct { |
| 19 | + Style string `json:"style" query:"style" default:"circle" validate:"required,ascii,oneof=circle battery bar" message:"style is required"` |
| 20 | + |
| 21 | + // common on all styles |
| 22 | + Progress int `json:"progress" query:"progress" default:"1" validate:"required,min=0,max=100" message:"progress is required"` |
| 23 | + |
| 24 | + // only circle style |
| 25 | + CircleSize int `json:"size" query:"size" default:"100" validate:"required,min=50,max=500" message:"size is required"` |
| 26 | + CircleWidth int `json:"circle_width" query:"circle_width" default:"15" validate:"required,min=1,max=200" message:"circle_width is required"` |
| 27 | + ProgressWidth int `json:"progress_width" query:"progress_width" default:"15" validate:"required,min=1,max=50" message:"progress_width is required"` |
| 28 | + CircleColor string `json:"circle_color" query:"circle_color" default:"e0e0e0" validate:"required" message:"circle_color is required"` |
| 29 | + |
| 30 | + // circle style |
| 31 | + ProgressColor string `json:"progress_color" query:"progress_color" default:"76e5b1" validate:"required" message:"progress_color is required"` |
| 32 | + TextColor string `json:"text_color" query:"text_color" default:"6bdba7" validate:"required" message:"text_color is required"` |
| 33 | + TextSize int `json:"text_size" query:"text_size" default:"52" validate:"required,min=10,max=100" message:"text_size is required"` |
| 34 | + |
| 35 | + BackgroundColor string `json:"background_color" query:"background_color" default:"e0e0e0" validate:"required" message:"background_color is required"` |
| 36 | + CaptionSize int `json:"caption_size" query:"caption_size" default:"25" validate:"required,min=2,max=100" message:"caption_size is required"` |
| 37 | + CaptionColor string `json:"caption_color" query:"caption_color" default:"000000" validate:"required" message:"caption_color is required"` |
| 38 | + SegmentCount int `json:"segment_count" query:"segment_count" default:"1" validate:"min=0,max=100" message:"segment_count min 0 and max 100"` |
| 39 | + SegmentGap int `json:"segment_gap" query:"segment_gap" default:"0" validate:"min=0,max=10" message:"segment_gap min 0 and max 10"` |
| 40 | + Caption string `json:"caption" query:"caption" default:"" validate:"ascii,min=0,max=20" message:"caption min 0 and max 20"` |
| 41 | + CornerRadius int `json:"corner_radius" query:"corner_radius" default:"10" validate:"required,min=1,max=50" message:"corner_radius is required"` |
| 42 | + |
| 43 | + // only bar style |
| 44 | + Width int `json:"width" query:"width" default:"200" validate:"required,min=50,max=500" message:"width is required"` |
| 45 | + Height int `json:"height" query:"height" default:"20" validate:"required,min=20,max=500" message:"height is required"` |
| 46 | +} |
| 47 | + |
| 48 | +func (h *APIHandler) Get(c echo.Context) error { |
| 49 | + req := new(APIRequest) |
| 50 | + if err := BindRequest(c, req); err != nil { |
| 51 | + return echo.NewHTTPError(http.StatusUnprocessableEntity, err) |
| 52 | + } |
| 53 | + defaults.SetDefaults(req) |
| 54 | + msgs, err := ValidateRequest(req) |
| 55 | + if err != nil { |
| 56 | + return echo.NewHTTPError(http.StatusUnprocessableEntity, msgs) |
| 57 | + } |
| 58 | + |
| 59 | + if req.Style == "circle" { |
| 60 | + circular, err := gps.NewCircular(func(o *gps.CircularOptions) error { |
| 61 | + o.Progress = req.Progress |
| 62 | + o.CircleSize = req.CircleSize |
| 63 | + o.CircleWidth = req.CircleWidth |
| 64 | + o.ProgressWidth = req.ProgressWidth |
| 65 | + o.CircleColor = "#" + req.CircleColor |
| 66 | + o.ProgressColor = "#" + req.ProgressColor |
| 67 | + o.TextColor = "#" + req.TextColor |
| 68 | + o.TextSize = req.TextSize |
| 69 | + o.BackgroundColor = "#" + req.BackgroundColor |
| 70 | + o.Caption = req.Caption |
| 71 | + o.CaptionSize = req.CaptionSize |
| 72 | + o.CaptionColor = "#" + req.CaptionColor |
| 73 | + o.SegmentCount = req.SegmentCount |
| 74 | + o.SegmentGap = float64(req.SegmentGap) |
| 75 | + |
| 76 | + return nil |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return echo.NewHTTPError(http.StatusInternalServerError, err) |
| 80 | + } |
| 81 | + content := circular.SVG() |
| 82 | + SetHeadersResponseSvg(c.Response().Header()) |
| 83 | + return c.Blob(http.StatusOK, "image/svg+xml", []byte(content)) |
| 84 | + } |
| 85 | + |
| 86 | + if req.Style == "bar" { |
| 87 | + bar, err := gps.NewBar(func(o *gps.BarOptions) error { |
| 88 | + o.Progress = req.Progress |
| 89 | + o.Caption = req.Caption |
| 90 | + o.Width = req.Width |
| 91 | + o.Height = req.Height |
| 92 | + o.ProgressColor = "#" + req.ProgressColor |
| 93 | + o.TextColor = "#" + req.TextColor |
| 94 | + o.TextSize = req.TextSize |
| 95 | + o.Caption = req.Caption |
| 96 | + o.CaptionSize = req.CaptionSize |
| 97 | + o.CaptionColor = "#" + req.CaptionColor |
| 98 | + o.BackgroundColor = "#" + req.BackgroundColor |
| 99 | + o.CornerRadius = req.CornerRadius |
| 100 | + return nil |
| 101 | + }) |
| 102 | + if err != nil { |
| 103 | + return echo.NewHTTPError(http.StatusInternalServerError, err) |
| 104 | + } |
| 105 | + content := bar.SVG() |
| 106 | + SetHeadersResponseSvg(c.Response().Header()) |
| 107 | + return c.Blob(http.StatusOK, "image/svg+xml", []byte(content)) |
| 108 | + } |
| 109 | + |
| 110 | + if req.Style == "battery" { |
| 111 | + battery, err := gps.NewBattery(func(o *gps.BatteryOptions) error { |
| 112 | + o.Progress = req.Progress |
| 113 | + o.Caption = req.Caption |
| 114 | + o.Width = req.Width |
| 115 | + o.Height = req.Height |
| 116 | + o.ProgressColor = "#" + req.ProgressColor |
| 117 | + o.TextColor = "#" + req.TextColor |
| 118 | + o.TextSize = req.TextSize |
| 119 | + o.Caption = req.Caption |
| 120 | + o.CaptionSize = req.CaptionSize |
| 121 | + o.CaptionColor = "#" + req.CaptionColor |
| 122 | + o.BackgroundColor = "#" + req.BackgroundColor |
| 123 | + o.CornerRadius = req.CornerRadius |
| 124 | + return nil |
| 125 | + }) |
| 126 | + if err != nil { |
| 127 | + return echo.NewHTTPError(http.StatusInternalServerError, err) |
| 128 | + } |
| 129 | + content := battery.SVG() |
| 130 | + SetHeadersResponseSvg(c.Response().Header()) |
| 131 | + return c.Blob(http.StatusOK, "image/svg+xml", []byte(content)) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
0 commit comments