Skip to content

Commit

Permalink
fix: Beta = #11 ,Make more logs #10 ,Limit Auto Delete #7 ,JSON camel…
Browse files Browse the repository at this point in the history
…Case #3
  • Loading branch information
afifurrohman-id committed Dec 4, 2023
1 parent 59cf153 commit 2b0a33a
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 62 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "web"]
path = web
url = https://github.com/afifurrohman-id/tempsy-client.git
92 changes: 44 additions & 48 deletions api/openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.3
info:
title: Tempsy RESTful API
description: Temporary Files Storage API Documentation
version: 0.2.0
version: 0.3.0
license:
name: MIT
url: https://opensource.org/license/mit
Expand Down Expand Up @@ -48,21 +48,21 @@ paths:
value:
- name: hello.txt
url: https://api.tempsy.afifurrohman.my.id/files/afif/hello.txt
auto_deleted_at: 1634179200000
private_url_expires: 100
is_public: false
uploaded_at: 1634179200000
updated_at: 1634179200000
autoDeletedAt: 1634179200000
privateUrlExpires: 100
isPublic: false
uploadedAt: 1634179200000
updatedAt: 1634179200000
size: 100
type: text/plain; charset=utf-8

- name: example.png
url: https://api.tempsy.afifurrohman.my.id/files/afif/public/example.png
auto_deleted_at: 1634179200000
private_url_expires: 100
is_public: true
uploaded_at: 1634179200000
updated_at: 1634179200000
autoDeletedAt: 1634179200000
privateUrlExpires: 100
isPublic: true
uploadedAt: 1634179200000
updatedAt: 1634179200000
size: 100
type: image/png
500:
Expand Down Expand Up @@ -118,8 +118,8 @@ paths:
invalidFileName:
summary: Bad Request invalid file name
value:
error_type: invalid_file_name
error_description: File name must be alphanumeric and contain extension separated by dot, underscore, or dash
errorType: invalid_file_name
errorDescription: File name must be alphanumeric and contain extension separated by dot, underscore, or dash
errorEmptyFileUpload:
$ref: '#/components/examples/invalidEmptyFile'
422:
Expand All @@ -141,8 +141,8 @@ paths:
error:
summary: Unsupported Media Type
value:
error_type: unsupported_content_type
error_description: 'Unsupported Content-Type: application/octet-stream'
errorType: unsupported_content_type
errorDescription: 'Unsupported Content-Type: application/octet-stream'
409:
description: Conflict
content:
Expand All @@ -153,8 +153,8 @@ paths:
error:
summary: File name Conflict
value:
error_type: file_already_exists
error_description: 'File: hello.txt already exists'
errorType: file_already_exists
errorDescription: 'File: hello.txt already exists'
500:
description: Unknown Internal Server Error
content:
Expand Down Expand Up @@ -186,8 +186,8 @@ paths:
error:
summary: Bad Request delete empty files
value:
error_type: delete_empty_data
error_description: 'Cannot delete empty data files, no data for user: afif'
errorType: delete_empty_data
errorDescription: 'Cannot delete empty data files, no data for user: afif'

500:
description: Unknown Internal Server Error
Expand Down Expand Up @@ -310,8 +310,8 @@ paths:
errorMismatchContentType:
summary: Bad Request mismatch content type
value:
error_type: mismatch_content_type
error_description: Please use the same content type as the original file
errorType: mismatch_content_type
errorDescription: Please use the same content type as the original file
errorUpdateWithEmptyFile:
$ref: '#/components/examples/invalidEmptyFile'
422:
Expand Down Expand Up @@ -394,7 +394,7 @@ paths:
type: string
description: Username of the user
example: afif
total_files:
totalFiles:
type: string
description: Email of the user
example: 100
Expand All @@ -403,7 +403,7 @@ paths:
ok:
value:
username: afif
total_files: 100
totalFiles: 100
400:
description: Bad Request
content:
Expand All @@ -415,8 +415,8 @@ paths:
summary: Bad Request invalid token
description: Invalid Token cause by authorization header have invalid token, maybe expires or have been revoked
value:
error_type: invalid_token
error_description: Invalid Token, cannot get user info
errorType: invalid_token
errorDescription: Invalid Token, cannot get user info

500:
description: Unknown Internal Server Error
Expand All @@ -442,14 +442,10 @@ paths:
type: object
description: api data guest token
properties:
access_token:
accessToken:
type: string
description: Guest token
token_type:
type: string
description: Token type, currently only Bearer
example: Bearer
expires_in:
expiresIn:
type: integer
description: Token expires in seconds
example: 3600
Expand All @@ -465,8 +461,8 @@ paths:
summary: Bad Request already have token
description: Already have a token
value:
error_type: already_have_token
error_description: You already have token
errorType: already_have_token
errorDescription: You already have token
500:
description: Unknown Internal Server Error
content:
Expand Down Expand Up @@ -571,10 +567,10 @@ components:
description: Error Response body, without data
type: object
properties:
error_type:
errorType:
type: string
description: 'Error type: unknown_server_error'
error_description:
errorDescription:
type: string
description: Error message description
fileData:
Expand Down Expand Up @@ -619,39 +615,39 @@ components:
summary: File Not Found
description: File Not Found Error Response
value:
error_type: file_not_found
error_description: 'File: hello.txt Not Found'
errorType: file_not_found
errorDescription: 'File: hello.txt Not Found'
missingHeaderMetadata:
summary: Missing Header Metadata
description: Error Missing file metadata
value:
error_type: invalid_header_file
error_description: Cannot Parse NaN as int64
errorType: invalid_header_file
errorDescription: Cannot Parse NaN as int64
dataResponse:
summary: Data Response Body
description: Single File Data for Response Body
value:
name: hello.webp
url: https://api.tempsy.afifurrohman.my.id/files/afif/hello.webp
auto_deleted_at: 1634179200000
private_url_expires: 100
is_public: false
uploaded_at: 1634179200000
updated_at: 1634179200000
autoDeletedAt: 1634179200000
privateUrlExpires: 100
isPublic: false
uploadedAt: 1634179200000
updatedAt: 1634179200000
size: 100
type: text/plain; charset=utf-8
internalServer:
summary: Internal Server Error
description: Internal Server Error for Response Body
value:
error_type: unknown_server_error
error_description: Unknown Internal Server Error. Unexpected Error
errorType: unknown_server_error
errorDescription: Unknown Internal Server Error. Unexpected Error
invalidEmptyFile:
summary: Invalid Empty File
description: Invalid Empty File Upload Error Response
value:
error_type: invalid_empty_file
error_description: File cannot be empty
errorType: invalid_empty_file
errorDescription: File cannot be empty
requestBodies:
uploadFile:
required: true
Expand Down
3 changes: 2 additions & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ LABEL authors=afif
WORKDIR /src
COPY . .

RUN go mod tidy && go fmt ./...

# Install CA certificates and update them
RUN apk update && apk add --no-cache ca-certificates && update-ca-certificates

RUN go mod tidy && go fmt ./...
ENV CGO_ENABLED=0

# Reduce binary size by removing debug information
Expand Down
8 changes: 2 additions & 6 deletions cmd/files/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ import (
"github.com/afifurrohman-id/tempsy/internal/models"
store "github.com/afifurrohman-id/tempsy/internal/storage"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"strings"
)

func HandleGetGuestToken(ctx *fiber.Ctx) error {
if _, err := guest.ParseToken(strings.TrimPrefix(ctx.Get(fiber.HeaderAuthorization), auth.BearerPrefix)); err == nil {
log.Errorf("Error Parse Token: %s", err.Error())
return ctx.Status(fiber.StatusBadRequest).JSON(&models.ApiError{
Type: internal.ErrorTypeHaveToken,
Description: "You already have valid token",
})
}

if _, err := oauth2.GetGoogleAccountInfo(strings.TrimPrefix(ctx.Get(fiber.HeaderAuthorization), auth.BearerPrefix)); err == nil {
log.Errorf("Error Token, Cannot Get Google Account info: %s", err.Error())
return ctx.Status(fiber.StatusBadRequest).JSON(&models.ApiError{
Type: internal.ErrorTypeHaveToken,
Description: "You already have valid token",
Expand All @@ -34,9 +31,8 @@ func HandleGetGuestToken(ctx *fiber.Ctx) error {
token, err := guest.CreateToken(username)
internal.Check(err)

return ctx.JSON(&models.Token{
return ctx.JSON(&models.GuestToken{
AccessToken: token,
TokenType: strings.TrimSpace(auth.BearerPrefix),
ExpiresIn: 604800, // 7 days in seconds
})
}
Expand All @@ -55,7 +51,7 @@ func HandleGetUserInfo(ctx *fiber.Ctx) error {
if err != nil {
return ctx.Status(fiber.StatusBadRequest).JSON(&models.ApiError{
Type: internal.ErrorTypeInvalidToken,
Description: "Token is not valid, Cannot get user info",
Description: "GuestToken is not valid, Cannot get user info",
})
}
userinfo.UserName = goUser.UserName
Expand Down
4 changes: 1 addition & 3 deletions cmd/files/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"io"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
)
Expand All @@ -34,12 +33,11 @@ func TestGetGuestToken(test *testing.T) {
body, err := io.ReadAll(res.Body)
require.NoError(test, err)

apiRes := new(models.Token)
apiRes := new(models.GuestToken)
require.NoError(test, json.Unmarshal(body, &apiRes))

assert.NotNil(test, apiRes)
assert.NotEmpty(test, apiRes.AccessToken)
assert.Equal(test, strings.TrimSpace(auth.BearerPrefix), apiRes.TokenType)
assert.Greater(test, time.Now().Add(time.Duration(apiRes.ExpiresIn)*time.Second).UnixMilli(), time.Now().UnixMilli())
assert.Equal(test, fiber.StatusOK, res.StatusCode)
})
Expand Down
1 change: 0 additions & 1 deletion internal/auth/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func GetAccessToken(refreshToken string) (*models.GOAuth2Token, error) {
if len(errs) > 0 {
return nil, errs[0]
}

if statusCode != fiber.StatusOK {
log.Errorf("access_token_error_not_ok_status_code_%d_body_%s", statusCode, body)
return nil, GOAuth2Error
Expand Down
7 changes: 4 additions & 3 deletions internal/models/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ type GoogleAccountInfo struct {
VerifiedEmail bool `json:"verified_email"`
}

type Token struct {
type GuestToken struct {
AccessToken string `json:"accessToken"`
ExpiresIn int `json:"expiresIn"` // in seconds
TokenType string `json:"tokenType"`
}

// GOAuth2Token
// For unmarshal purpose
type GOAuth2Token struct {
*Token
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"` // in seconds
TokenType string `json:"token_type"`
Scopes string `json:"scope"` // separated by space
IdToken string `json:"id_token"`
RefreshToken string `json:"refresh_token"`
Expand Down
1 change: 1 addition & 0 deletions web
Submodule web added at 153b55

0 comments on commit 2b0a33a

Please sign in to comment.