Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ add logging codes #731

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions router/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/Jomon/ent"
"go.uber.org/zap"
)

func (h Handlers) GetAdmins(c echo.Context) error {
ctx := c.Request().Context()
admins, err := h.Repository.GetAdmins(ctx)
if err != nil {
h.Logger.Error("failed to get admins from repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -26,15 +28,18 @@ func (h Handlers) GetAdmins(c echo.Context) error {
func (h Handlers) PostAdmins(c echo.Context) error {
var admin []uuid.UUID
if err := c.Bind(&admin); err != nil {
h.Logger.Info("failed to get admin id from request", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

ctx := c.Request().Context()
err := h.Repository.AddAdmins(ctx, admin)
if err != nil {
if ent.IsConstraintError(err) {
h.Logger.Info("constraint error while adding admin in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}
h.Logger.Error("failed to add admin in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -44,12 +49,14 @@ func (h Handlers) PostAdmins(c echo.Context) error {
func (h Handlers) DeleteAdmins(c echo.Context) error {
var admin []uuid.UUID
if err := c.Bind(&admin); err != nil {
h.Logger.Info("failed to get admin id from request", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

ctx := c.Request().Context()
err := h.Repository.DeleteAdmins(ctx, admin)
if err != nil {
h.Logger.Error("failed to delete admin from repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand Down
9 changes: 9 additions & 0 deletions router/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/traPtitech/Jomon/ent"
"github.com/traPtitech/Jomon/model"
"github.com/traPtitech/Jomon/service"
"go.uber.org/zap"
)

const (
Expand All @@ -36,6 +37,7 @@ func (h Handlers) AuthCallback(c echo.Context) error {

sess, err := session.Get(h.SessionName, c)
if err != nil {
h.Logger.Error("failed to get session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -52,11 +54,13 @@ func (h Handlers) AuthCallback(c echo.Context) error {

res, err := service.RequestAccessToken(code, codeVerifier)
if err != nil {
h.Logger.Error("failed to get access token", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

u, err := service.FetchTraQUserInfo(res.AccessToken)
if err != nil {
h.Logger.Error("failed to fetch traQ user info", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -66,9 +70,11 @@ func (h Handlers) AuthCallback(c echo.Context) error {
if ent.IsNotFound(err) {
modelUser, err = h.Repository.CreateUser(c.Request().Context(), u.Name, u.DisplayName, false)
if err != nil {
h.Logger.Error("failed to create user", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
} else {
h.Logger.Error("failed to get user by name", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
}
Expand All @@ -83,6 +89,7 @@ func (h Handlers) AuthCallback(c echo.Context) error {
sess.Values[sessionUserKey] = user

if err = sess.Save(c.Request(), c.Response()); err != nil {
h.Logger.Error("failed to save session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -92,6 +99,7 @@ func (h Handlers) AuthCallback(c echo.Context) error {
func (h Handlers) GeneratePKCE(c echo.Context) error {
sess, err := session.Get(h.SessionName, c)
if err != nil {
h.Logger.Error("failed to get session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -111,6 +119,7 @@ func (h Handlers) GeneratePKCE(c echo.Context) error {

err = sess.Save(c.Request(), c.Response())
if err != nil {
h.Logger.Error("failed to save session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand Down
31 changes: 31 additions & 0 deletions router/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/traPtitech/Jomon/ent"
"go.uber.org/zap"
)

type FileResponse struct {
Expand Down Expand Up @@ -36,57 +37,68 @@ var acceptedMimeTypes = map[string]bool{
func (h Handlers) PostFile(c echo.Context) error {
form, err := c.MultipartForm()
if err != nil {
h.Logger.Error("failed to parse request as multipart/form-data", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
files, ok := form.File["file"]
if !ok || len(files) != 1 {
h.Logger.Info("could not find field `file` in request, or its length is not 1")
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file"))
}
reqfile := files[0]
names, ok := form.Value["name"]
if !ok || len(names) != 1 {
h.Logger.Info("could not find field `name` in request, or its length is not 1")
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file name"))
}
name := names[0]
requestIDs, ok := form.Value["request_id"]
if !ok || len(requestIDs) != 1 {
h.Logger.Info("could not find field `request_id` in request, or its length is not 1")
return echo.NewHTTPError(http.StatusBadRequest, fmt.Errorf("invalid file request id"))
}
requestID, err := uuid.Parse(requestIDs[0])
if err != nil {
h.Logger.Info("could not parse request_id as UUID", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

mimetype := reqfile.Header.Get(echo.HeaderContentType)
if !acceptedMimeTypes[mimetype] {
h.Logger.Info("requested unsupported mime type", zap.String("mime-type", mimetype))
return echo.NewHTTPError(http.StatusUnsupportedMediaType, fmt.Errorf("unsupported media type"))
}

src, err := reqfile.Open()
if err != nil {
h.Logger.Error("failed to open requested file", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
defer src.Close()

// get create user
sess, err := session.Get(h.SessionName, c)
if err != nil {
h.Logger.Error("failed to get session", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

user, ok := sess.Values[sessionUserKey].(User)
if !ok {
h.Logger.Error("failed to parse stored session as user info")
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("invalid user"))
}

ctx := c.Request().Context()
file, err := h.Repository.CreateFile(ctx, name, mimetype, requestID, user.ID)
if err != nil {
h.Logger.Error("failed to create file in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

err = h.Storage.Save(file.ID.String(), src)
if err != nil {
h.Logger.Error("failed to save file id in storage", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -96,15 +108,18 @@ func (h Handlers) PostFile(c echo.Context) error {
func (h Handlers) GetFile(c echo.Context) error {
fileID, err := uuid.Parse(c.Param("fileID"))
if err != nil {
h.Logger.Error("could not parse query parameter `fileID` as UUID", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

ctx := c.Request().Context()
file, err := h.Repository.GetFile(ctx, fileID)
if err != nil {
if ent.IsNotFound(err) {
h.Logger.Info("could not find file in repository", zap.String("ID", fileID.String()))
return echo.NewHTTPError(http.StatusNotFound, err)
}
h.Logger.Error("failed to get file from repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -114,15 +129,24 @@ func (h Handlers) GetFile(c echo.Context) error {
if im != "" {
imt, err := http.ParseTime(im)
if err != nil {
h.Logger.Info("could not parse time in request header", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}
if modifiedAt.Before(imt) || modifiedAt.Equal(imt) {
h.Logger.Info(
"content is not modified since the last request",
zap.String("ID", fileID.String()),
zap.Time("If-Modified-Since", imt))
return c.NoContent(http.StatusNotModified)
}
}

f, err := h.Storage.Open(fileID.String())
if err != nil {
h.Logger.Error(
"failed to open file in storage",
zap.String("ID", fileID.String()),
zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
defer f.Close()
Expand All @@ -136,15 +160,18 @@ func (h Handlers) GetFile(c echo.Context) error {
func (h Handlers) GetFileMeta(c echo.Context) error {
fileID, err := uuid.Parse(c.Param("fileID"))
if err != nil {
h.Logger.Info("could not parse query parameter `fileID` as UUID", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

ctx := c.Request().Context()
file, err := h.Repository.GetFile(ctx, fileID)
if err != nil {
if ent.IsNotFound(err) {
h.Logger.Info("could not find file in repository", zap.String("ID", fileID.String()))
return echo.NewHTTPError(http.StatusNotFound, err)
}
h.Logger.Error("failed to get file from repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand All @@ -160,20 +187,24 @@ func (h Handlers) GetFileMeta(c echo.Context) error {
func (h Handlers) DeleteFile(c echo.Context) error {
fileID, err := uuid.Parse(c.Param("fileID"))
if err != nil {
h.Logger.Info("could not parse query parameter `fileID` as UUID", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}

ctx := c.Request().Context()
err = h.Repository.DeleteFile(ctx, fileID)
if err != nil {
if ent.IsConstraintError(err) {
h.Logger.Info("constraint error while deleting file", zap.Error(err))
return echo.NewHTTPError(http.StatusBadRequest, err)
}
h.Logger.Error("failed to delete file in repository", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

err = h.Storage.Delete(fileID.String())
if err != nil {
h.Logger.Error("failed to delete file in storage", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

Expand Down
Loading