ch prefix
This commit is contained in:
4
backend/internal/admin/auth/logs/all.log
Normal file
4
backend/internal/admin/auth/logs/all.log
Normal file
@@ -0,0 +1,4 @@
|
||||
time="2026-08-05T14:40:26+05:00" level=info msg="read application configuration" func="bankline/internal/config.GetConfig.func1()" file="config.go:40"
|
||||
time="2026-08-05T14:40:26+05:00" level=info func="bankline/internal/config.GetConfig.func1()" file="config.go:44"
|
||||
time="2026-08-05T14:40:26+05:00" level=fatal msg="open ./config.yaml: no such file or directory" func="bankline/internal/config.GetConfig.func1()" file="config.go:45"
|
||||
time="2026-08-05T14:41:20+05:00" level=info msg="read application configuration" func="bankline/internal/config.GetConfig.func1()" file="config.go:40"
|
||||
111
backend/internal/admin/auth/upload_test.go
Normal file
111
backend/internal/admin/auth/upload_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"bankline/internal/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestFileUploadPublicImages(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// package dir -> backend root (for config.yaml)
|
||||
backendRoot := filepath.Clean(filepath.Join(wd, "..", "..", ".."))
|
||||
if err := os.Chdir(backendRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chdir(wd) })
|
||||
|
||||
tmpPublic := t.TempDir()
|
||||
cfg := config.GetConfig()
|
||||
prev := cfg.PublicFilePath
|
||||
cfg.PublicFilePath = tmpPublic
|
||||
t.Cleanup(func() { cfg.PublicFilePath = prev })
|
||||
|
||||
h := &handler{}
|
||||
|
||||
src := filepath.Join(backendRoot, "public", "images", "1698258358562790757.jpg")
|
||||
img, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
img = []byte{
|
||||
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xd9,
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("ok under 10MB", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = multipartRequest(t, "ok.jpg", img)
|
||||
|
||||
h.fileUpload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Data == "" || filepath.Ext(resp.Data) != ".jpg" {
|
||||
t.Fatalf("unexpected data path: %q", resp.Data)
|
||||
}
|
||||
|
||||
local := filepath.Join(tmpPublic, "images", filepath.Base(resp.Data))
|
||||
if _, err := os.Stat(local); err != nil {
|
||||
t.Fatalf("file not saved at %s: %v", local, err)
|
||||
}
|
||||
t.Logf("saved path in response: %s", resp.Data)
|
||||
})
|
||||
|
||||
t.Run("reject over 10MB", func(t *testing.T) {
|
||||
big := bytes.Repeat([]byte("a"), 10<<20+1)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = multipartRequest(t, "big.jpg", big)
|
||||
|
||||
h.fileUpload(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
t.Logf("rejected body: %s", w.Body.String())
|
||||
})
|
||||
}
|
||||
|
||||
func multipartRequest(t *testing.T, filename string, content []byte) *http.Request {
|
||||
t.Helper()
|
||||
var body bytes.Buffer
|
||||
w := multipart.NewWriter(&body)
|
||||
part, err := w.CreateFormFile("file", filename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(part, bytes.NewReader(content)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/file", &body)
|
||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||
return req
|
||||
}
|
||||
@@ -30,7 +30,7 @@ var sortColumns = map[string]string{
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params) (banner.DataBannerDTO, error) {
|
||||
|
||||
ns := []banner.GetBannerDTO{}
|
||||
ns := []banner.BannerListItemDTO{}
|
||||
var (
|
||||
dt banner.DataBannerDTO
|
||||
err error
|
||||
@@ -40,16 +40,16 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (bann
|
||||
where := ""
|
||||
args := []interface{}{}
|
||||
if params.Search != "" {
|
||||
where = ` where title::text ILIKE $1 or content_tm ILIKE $1 or content_ru ILIKE $1 or content_en ILIKE $1 `
|
||||
where = ` where title::text ILIKE $1 `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(*) from banner` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
// List: skip content_* (large HTML). Full body is loaded via GetOneData.
|
||||
q := ` select
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at
|
||||
from banner` +
|
||||
where +
|
||||
@@ -68,10 +68,9 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (bann
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var n banner.GetBannerDTO
|
||||
var n banner.BannerListItemDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Image, &n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
|
||||
@@ -3,8 +3,8 @@ package banner
|
||||
import "time"
|
||||
|
||||
type DataBannerDTO struct {
|
||||
Categories []GetBannerDTO `json:"banners"`
|
||||
Count int `json:"count"`
|
||||
Categories []BannerListItemDTO `json:"banners"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
@@ -13,6 +13,14 @@ type Language struct {
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
// BannerListItemDTO is the list payload — no content (loaded via GetOne).
|
||||
type BannerListItemDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Title Language `json:"title"`
|
||||
Image string `json:"imagePath"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type GetBannerDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Title Language `json:"title"`
|
||||
|
||||
@@ -30,7 +30,7 @@ var sortColumns = map[string]string{
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params) (currency.DataCurrencyDTO, error) {
|
||||
|
||||
ns := []currency.CurrencyDTO{}
|
||||
ns := []currency.CurrencyListItemDTO{}
|
||||
var (
|
||||
dt currency.DataCurrencyDTO
|
||||
err error
|
||||
@@ -40,16 +40,16 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (curr
|
||||
where := ""
|
||||
args := []interface{}{}
|
||||
if params.Search != "" {
|
||||
where = ` where title::text ILIKE $1 or content_tm ILIKE $1 or content_ru ILIKE $1 or content_en ILIKE $1 `
|
||||
where = ` where title::text ILIKE $1 `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(*) from currency` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
// List: skip content_* (large HTML). Full body is loaded via GetData.
|
||||
q := ` select
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
path, created_at
|
||||
from currency` +
|
||||
where +
|
||||
@@ -68,10 +68,9 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (curr
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var n currency.CurrencyDTO
|
||||
var n currency.CurrencyListItemDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.Path, &n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
|
||||
@@ -3,8 +3,8 @@ package currency
|
||||
import "time"
|
||||
|
||||
type DataCurrencyDTO struct {
|
||||
Currencies []CurrencyDTO `json:"currencies"`
|
||||
Count int `json:"count"`
|
||||
Currencies []CurrencyListItemDTO `json:"currencies"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
@@ -13,6 +13,14 @@ type Language struct {
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
// CurrencyListItemDTO is the list payload — no content (loaded via GetData).
|
||||
type CurrencyListItemDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Title Language `json:"title"`
|
||||
Path string `json:"path"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type CurrencyDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Title Language `json:"title"`
|
||||
|
||||
@@ -30,7 +30,7 @@ var sortColumns = map[string]string{
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params) (static.DataStaticDTO, error) {
|
||||
|
||||
ns := []static.StaticDTO{}
|
||||
ns := []static.StaticListItemDTO{}
|
||||
var (
|
||||
dt static.DataStaticDTO
|
||||
err error
|
||||
@@ -40,16 +40,16 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (stat
|
||||
where := ""
|
||||
args := []interface{}{}
|
||||
if params.Search != "" {
|
||||
where = ` where title::text ILIKE $1 or content_tm ILIKE $1 or content_ru ILIKE $1 or content_en ILIKE $1 `
|
||||
where = ` where title::text ILIKE $1 `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(*) from static` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
// List: skip content_* (large HTML). Full body is loaded via GetData.
|
||||
q := ` select
|
||||
id, logo, title,
|
||||
content_tm, content_ru, content_en,
|
||||
created_at
|
||||
from static` +
|
||||
where +
|
||||
@@ -68,10 +68,9 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (stat
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var n static.StaticDTO
|
||||
var n static.StaticListItemDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Logo, &n.Title,
|
||||
&n.ContentTm, &n.ContentRu, &n.ContentEn,
|
||||
&n.CreatedAt,
|
||||
)
|
||||
if errN != nil {
|
||||
|
||||
@@ -3,8 +3,8 @@ package static
|
||||
import "time"
|
||||
|
||||
type DataStaticDTO struct {
|
||||
Statics []StaticDTO `json:"statics"`
|
||||
Count int `json:"count"`
|
||||
Statics []StaticListItemDTO `json:"statics"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
type Language struct {
|
||||
@@ -13,6 +13,14 @@ type Language struct {
|
||||
En string `json:"en"`
|
||||
}
|
||||
|
||||
// StaticListItemDTO is the list payload — no content (loaded via GetData).
|
||||
type StaticListItemDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Logo string `json:"logo"`
|
||||
Title Language `json:"title"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type StaticDTO struct {
|
||||
Id int `json:"Id"`
|
||||
Logo string `json:"logo"`
|
||||
|
||||
@@ -31,9 +31,9 @@ var resources = []Resource{
|
||||
// Register mounts templates, static assets and all dashboard page routes.
|
||||
func Register(router *gin.Engine) {
|
||||
router.LoadHTMLGlob("web/admin/templates/**/*.html")
|
||||
router.Static("/admin/assets", "./web/admin/assets")
|
||||
router.Static("/M0n1t0r/assets", "./web/admin/assets")
|
||||
|
||||
admin := router.Group("/admin")
|
||||
admin := router.Group("/M0n1t0r")
|
||||
|
||||
admin.GET("/login", appresult.AdminGuestOnly(), func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "pages/login", gin.H{
|
||||
@@ -96,7 +96,7 @@ func Register(router *gin.Engine) {
|
||||
}
|
||||
|
||||
router.NoRoute(func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/admin") {
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/M0n1t0r") {
|
||||
c.HTML(http.StatusNotFound, "pages/404", gin.H{
|
||||
"Title": "Page not found",
|
||||
"Page": "404",
|
||||
|
||||
Reference in New Issue
Block a user