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

refactor: Remove redundant function call, remove single call variable #34

Merged
merged 1 commit into from
Dec 30, 2023
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
13 changes: 4 additions & 9 deletions cmd/files/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ import (

func HandleDeleteFile(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
fileName = ctx.Params("filename")
storeCtx = context.Background()
filePath = fmt.Sprintf("%s/%s", username, fileName)
filePath = fmt.Sprintf("%s/%s", ctx.Params("username"), fileName)
)

storeCtx, cancel := context.WithTimeout(storeCtx, store.DefaultTimeoutCtx)
storeCtx, cancel := context.WithTimeout(context.Background(), store.DefaultTimeoutCtx)
defer cancel()

if _, err := store.GetObject(storeCtx, filePath); err != nil {
Expand All @@ -43,12 +41,9 @@ func HandleDeleteFile(ctx *fiber.Ctx) error {
}

func HandleDeleteAllFile(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
storeCtx = context.Background()
)
username := ctx.Params("username")

storeCtx, cancel := context.WithTimeout(storeCtx, store.DefaultTimeoutCtx)
storeCtx, cancel := context.WithTimeout(context.Background(), store.DefaultTimeoutCtx)
defer cancel()

filesData, err := store.GetAllObject(storeCtx, username)
Expand Down
11 changes: 5 additions & 6 deletions cmd/files/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"testing"
"time"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/models"
store "github.com/afifurrohman-id/tempsy/internal/storage"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"testing"
"time"
)

func TestHandleDelete(test *testing.T) {

var (
app = fiber.New()
storeCtx = context.Background()
Expand All @@ -36,7 +36,6 @@ func TestHandleDelete(test *testing.T) {
for _, dataFile := range dataFiles {
internal.LogErr(store.DeleteObject(storeCtx, dataFile.Name))
}

})

for i := 1; i <= 3; i++ {
Expand Down
19 changes: 7 additions & 12 deletions cmd/files/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ func HandleGetPublicFile(ctx *fiber.Ctx) error {
defer cancel()

var (
username = ctx.Params("username")
fileName = ctx.Params("filename")
filePath = fmt.Sprintf("%s/%s", username, fileName)
filePath = fmt.Sprintf("%s/%s", ctx.Params("username"), fileName)
)

fileData, err := store.GetObject(storeCtx, filePath)
Expand Down Expand Up @@ -62,13 +61,11 @@ func HandleGetPublicFile(ctx *fiber.Ctx) error {

func HandleGetFileData(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
fileName = ctx.Params("filename")
filePath = fmt.Sprintf("%s/%s", username, fileName)
filePath = fmt.Sprintf("%s/%s", ctx.Params("username"), fileName)
)

storeCtx := context.Background()
storeCtx, cancel := context.WithTimeout(storeCtx, store.DefaultTimeoutCtx)
storeCtx, cancel := context.WithTimeout(context.Background(), store.DefaultTimeoutCtx)
defer cancel()

fileData, err := store.GetObject(storeCtx, filePath)
Expand All @@ -88,16 +85,14 @@ func HandleGetFileData(ctx *fiber.Ctx) error {

func HandleGetAllFileData(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
storeCtx = context.Background()
mu = new(sync.Mutex)
wg = new(sync.WaitGroup)
mu = new(sync.Mutex)
wg = new(sync.WaitGroup)
)

storeCtx, cancel := context.WithTimeout(storeCtx, store.DefaultTimeoutCtx)
storeCtx, cancel := context.WithTimeout(context.Background(), store.DefaultTimeoutCtx)
defer cancel()

filesData, err := store.GetAllObject(storeCtx, username)
filesData, err := store.GetAllObject(storeCtx, ctx.Params("username"))
internal.Check(err)

wg.Add(1)
Expand Down
14 changes: 7 additions & 7 deletions cmd/files/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"path"
"strings"
"testing"
"time"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/models"
store "github.com/afifurrohman-id/tempsy/internal/storage"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"path"
"strings"
"testing"
"time"
)

func init() {
Expand Down Expand Up @@ -276,5 +277,4 @@ func TestHandleGetPublicFile(test *testing.T) {
assert.Equal(test, internal.ErrorTypeFileNotPublic, apiErr.Type)
})
}

}
4 changes: 2 additions & 2 deletions cmd/files/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package middleware

import (
"fmt"
"strings"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/auth"
"github.com/afifurrohman-id/tempsy/internal/auth/guest"
Expand All @@ -10,12 +12,10 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"golang.org/x/exp/slices"
"strings"
)

func CheckHttpMethod(ctx *fiber.Ctx) error {
if !slices.Contains(auth.AllowedHttpMethod, ctx.Method()) {

return ctx.Status(fiber.StatusMethodNotAllowed).JSON(&models.ApiError{
Type: "method_not_allowed",
Description: fmt.Sprintf("Http Method: %s Is Not Allowed", ctx.Method()),
Expand Down
10 changes: 5 additions & 5 deletions cmd/files/middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package middleware

import (
"encoding/json"
"io"
"net/http/httptest"
"os"
"testing"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/auth"
"github.com/afifurrohman-id/tempsy/internal/auth/guest"
"github.com/afifurrohman-id/tempsy/internal/auth/oauth2"
"github.com/afifurrohman-id/tempsy/internal/models"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"os"
"testing"
)

func TestCheckHttpMethod(test *testing.T) {
Expand Down Expand Up @@ -124,5 +125,4 @@ func TestCheckAuth(test *testing.T) {
require.Equal(test, fiber.StatusUnauthorized, res.StatusCode)
})
})

}
3 changes: 1 addition & 2 deletions cmd/files/middleware/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"errors"
"fmt"

"github.com/afifurrohman-id/tempsy/internal/auth"
"github.com/afifurrohman-id/tempsy/internal/models"
"github.com/gofiber/fiber/v2"
Expand All @@ -11,7 +12,6 @@ import (
)

func CatchServerError(ctx *fiber.Ctx, err error) error {

fiberErr := new(fiber.Error)
if errors.As(err, &fiberErr) {
log.Error("Fiber - ", fiberErr)
Expand Down Expand Up @@ -41,5 +41,4 @@ func CatchServerError(ctx *fiber.Ctx, err error) error {
Type: "unknown_server_error",
Description: "Unknown Internal Server Error",
})

}
7 changes: 4 additions & 3 deletions cmd/files/middleware/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package middleware
import (
"bytes"
"encoding/json"
"io"
"net/http/httptest"
"testing"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/models"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"testing"
)

func TestErrorServer(test *testing.T) {
Expand Down
13 changes: 9 additions & 4 deletions cmd/files/middleware/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ var RateLimiterProcessing = limiter.New(limiter.Config{
var RateLimiterGuestToken = limiter.New(limiter.Config{
Max: MaxReqGuestTokenPerSeconds,
KeyGenerator: func(ctx *fiber.Ctx) string {
if ctx.Get(auth.HeaderRealIp) != "" {
return utils.CopyString(ctx.Get(auth.HeaderRealIp))
var (
realIp = ctx.Get(auth.HeaderRealIp)
xRealIp = ctx.Get(auth.HeaderXRealIp)
)

if realIp != "" {
return utils.CopyString(realIp)
}

if ctx.Get(auth.HeaderXRealIp) != "" {
return utils.CopyString(ctx.Get(auth.HeaderXRealIp))
if xRealIp != "" {
return utils.CopyString(xRealIp)
}

// ctx.IP() is copy by default
Expand Down
6 changes: 3 additions & 3 deletions cmd/files/middleware/limiter_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package middleware

import (
"net/http/httptest"
"testing"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/auth"
"github.com/afifurrohman-id/tempsy/internal/auth/guest"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"
)

func TestLimitAuthTokenProcess(test *testing.T) {
Expand Down Expand Up @@ -118,6 +119,5 @@ func TestLimitGuestToken(test *testing.T) {
}
})
}

})
}
16 changes: 4 additions & 12 deletions cmd/files/middleware/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ import (
)

func PurgeAnonymousAccount(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
storeCtx = context.Background()
)
username := ctx.Params("username")

if strings.HasPrefix(username, guest.UsernamePrefix) {
if nameSplit := strings.SplitN(username, "-", 3); len(nameSplit) > 2 {
autoDeletedAccount, err := strconv.ParseInt(nameSplit[1], 10, 64)
if err == nil {
if autoDeletedAccount < time.Now().UnixMilli() {
timeout := 15 * time.Second
storeCtx, cancel := context.WithTimeout(storeCtx, timeout)
storeCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

filesData, err := store.GetAllObject(storeCtx, username)
Expand Down Expand Up @@ -68,15 +65,10 @@ func PurgeAnonymousAccount(ctx *fiber.Ctx) error {
}

func AutoDeleteScheduler(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
storeCtx = context.Background()
)

storeCtx, cancel := context.WithTimeout(storeCtx, 10*time.Second)
storeCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

filesData, err := store.GetAllObject(storeCtx, username)
filesData, err := store.GetAllObject(storeCtx, ctx.Params("username"))
if err != nil {
log.Error(err)
return ctx.Next()
Expand Down
13 changes: 7 additions & 6 deletions cmd/files/middleware/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"path"
"strings"
"testing"
"time"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/auth/guest"
"github.com/afifurrohman-id/tempsy/internal/models"
store "github.com/afifurrohman-id/tempsy/internal/storage"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"path"
"strings"
"testing"
"time"
)

func init() {
Expand Down
6 changes: 2 additions & 4 deletions cmd/files/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import (
// HandleUpdateFile Updates single file by name
func HandleUpdateFile(ctx *fiber.Ctx) error {
var (
username = ctx.Params("username")
fileName = ctx.Params("filename")
filePath = fmt.Sprintf("%s/%s", username, fileName)
storeCtx = context.Background()
filePath = fmt.Sprintf("%s/%s", ctx.Params("username"), fileName)
)

storeCtx, cancel := context.WithTimeout(storeCtx, store.DefaultTimeoutCtx)
storeCtx, cancel := context.WithTimeout(context.Background(), store.DefaultTimeoutCtx)
defer cancel()

if len(ctx.Body()) < 1 {
Expand Down
12 changes: 6 additions & 6 deletions cmd/files/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/afifurrohman-id/tempsy/internal"
"github.com/afifurrohman-id/tempsy/internal/models"
store "github.com/afifurrohman-id/tempsy/internal/storage"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http/httptest"
"strings"
"testing"
"time"
)

func TestHandleUpdateFile(test *testing.T) {
Expand Down Expand Up @@ -196,5 +197,4 @@ func TestHandleUpdateFile(test *testing.T) {
require.NotEmpty(test, apiRes)
require.Equal(test, internal.ErrorTypeMismatchType, apiRes.Type)
})

}
Loading