2021-08-07 11:16:50 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-03-03 22:40:50 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/helpers"
|
2022-01-02 03:22:58 +03:00
|
|
|
"net/http"
|
|
|
|
|
2021-08-07 11:16:50 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/muety/wakapi/models"
|
2022-12-01 12:57:07 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
2021-08-07 11:16:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type DiagnosticsApiHandler struct {
|
|
|
|
config *conf.Config
|
|
|
|
userSrvc services.IUserService
|
|
|
|
diagnosticsSrvc services.IDiagnosticsService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDiagnosticsApiHandler(userService services.IUserService, diagnosticsService services.IDiagnosticsService) *DiagnosticsApiHandler {
|
|
|
|
return &DiagnosticsApiHandler{
|
|
|
|
config: conf.Get(),
|
|
|
|
userSrvc: userService,
|
|
|
|
diagnosticsSrvc: diagnosticsService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *DiagnosticsApiHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
router.Post("/plugins/errors", h.Post)
|
2021-08-07 11:16:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// @Summary Push a new diagnostics object
|
|
|
|
// @ID post-diagnostics
|
|
|
|
// @Tags diagnostics
|
|
|
|
// @Accept json
|
|
|
|
// @Param diagnostics body models.Diagnostics true "A single diagnostics object sent by WakaTime CLI"
|
|
|
|
// @Success 201
|
2022-01-02 03:22:58 +03:00
|
|
|
// @Router /plugins/errors [post]
|
2021-08-07 11:16:50 +03:00
|
|
|
func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var diagnostics models.Diagnostics
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&diagnostics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte(conf.ErrBadRequest))
|
|
|
|
conf.Log().Request(r).Error("failed to parse diagnostics for user %s - %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := h.diagnosticsSrvc.Create(&diagnostics); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Write([]byte(conf.ErrInternalServerError))
|
|
|
|
conf.Log().Request(r).Error("failed to insert diagnostics for user %s - %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:57:07 +03:00
|
|
|
helpers.RespondJSON(w, r, http.StatusCreated, struct{}{})
|
2021-08-07 11:16:50 +03:00
|
|
|
}
|