From aa0f4d292e0200f7c2d5d34542673ee53d2a028d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 18 Feb 2025 23:18:38 +0800 Subject: [PATCH] :sparkles: Billing status displaying --- pkg/internal/server/api/attachments_api.go | 20 ++++++++++++++++++++ pkg/internal/server/api/index.go | 1 + 2 files changed, 21 insertions(+) diff --git a/pkg/internal/server/api/attachments_api.go b/pkg/internal/server/api/attachments_api.go index a2d349d..641a6c1 100644 --- a/pkg/internal/server/api/attachments_api.go +++ b/pkg/internal/server/api/attachments_api.go @@ -2,6 +2,7 @@ package api import ( "fmt" + "github.com/spf13/viper" "strings" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" @@ -13,6 +14,25 @@ import ( "github.com/gofiber/fiber/v2" ) +func getBillingStatus(c *fiber.Ctx) error { + if err := sec.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("nex_user").(*sec.UserInfo) + + currentBytes, err := services.GetLastDayUploadedBytes(user.ID) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + discountFileSize := viper.GetInt64("payment.discount") + + return c.JSON(fiber.Map{ + "current_bytes": currentBytes, + "discount_file_size": discountFileSize, + "included_ratio": float64(currentBytes) / float64(discountFileSize), + }) +} + func openAttachment(c *fiber.Ctx) error { id := c.Params("id") region := c.Query("region") diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index b256fea..1e63709 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -9,6 +9,7 @@ func MapAPIs(app *fiber.App, baseURL string) { api := app.Group(baseURL).Name("API") { api.Get("/destinations", listDestination) + api.Get("/billing", getBillingStatus) boost := api.Group("/boosts").Name("Boosts API") {