add file prefix

This commit is contained in:
2026-07-19 21:18:16 +07:00
parent ed59b558ea
commit 2522a928de
6 changed files with 33 additions and 9 deletions
+2
View File
@@ -11,3 +11,5 @@ S3_HOST=http://0.0.0.0:9000
S3_USER=rustfs S3_USER=rustfs
S3_PASSWORD=your_password S3_PASSWORD=your_password
S3_BUCKET=evening-detective S3_BUCKET=evening-detective
FILE_PREFIX_DOMAIN=http://0.0.0.0:8090/api/files/
+4 -1
View File
@@ -72,7 +72,10 @@ func main() {
} }
fileService := file_service.NewFileService(fileStorage) fileService := file_service.NewFileService(fileStorage)
scenariosRepo := scenarios_repo.NewScenariosRepo(dbpool) scenariosRepo := scenarios_repo.NewScenariosRepo(dbpool)
scenarioService := scenarios_service.NewScenarioService(scenariosRepo) scenarioService := scenarios_service.NewScenarioService(
scenariosRepo,
os.Getenv("FILE_PREFIX_DOMAIN"),
)
// Create a listener on TCP port // Create a listener on TCP port
lis, err := net.Listen("tcp", ":8080") lis, err := net.Listen("tcp", ":8080")
+1
View File
@@ -22,6 +22,7 @@ services:
S3_USER: rustfs S3_USER: rustfs
S3_PASSWORD: your_password S3_PASSWORD: your_password
S3_BUCKET: evening-detective S3_BUCKET: evening-detective
FILE_PREFIX_DOMAIN: http://0.0.0.0:8090/api/files/
depends_on: depends_on:
- postgres - postgres
restart: unless-stopped restart: unless-stopped
@@ -6,10 +6,13 @@ import (
"evening_detective_server/internal/repos" "evening_detective_server/internal/repos"
) )
func mapScenarios(o []*repos.Scenario) ([]*Scenario, error) { func mapScenarios(
o []*repos.Scenario,
domain string,
) ([]*Scenario, error) {
res := make([]*Scenario, 0, len(o)) res := make([]*Scenario, 0, len(o))
for _, item := range o { for _, item := range o {
scenario, err := mapScenario(item) scenario, err := mapScenario(item, domain)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -18,7 +21,10 @@ func mapScenarios(o []*repos.Scenario) ([]*Scenario, error) {
return res, nil return res, nil
} }
func mapScenario(o *repos.Scenario) (*Scenario, error) { func mapScenario(
o *repos.Scenario,
domain string,
) (*Scenario, error) {
story, err := mapStory(o.Scenario) story, err := mapStory(o.Scenario)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -27,7 +33,7 @@ func mapScenario(o *repos.Scenario) (*Scenario, error) {
ID: o.ID, ID: o.ID,
Name: o.Name, Name: o.Name,
Description: o.Description, Description: o.Description,
Image: o.Image, Image: mapImage(o.Image, domain),
Story: story, Story: story,
Author: o.Author, Author: o.Author,
Status: o.Status, Status: o.Status,
@@ -53,3 +59,12 @@ func convertStory(o *storytelling.Story) (string, error) {
} }
return string(b), nil return string(b), nil
} }
func mapImage(image *string, domain string) *string {
if image == nil {
u := domain + "question.png"
return &u
}
u := domain + *image
return &u
}
@@ -9,13 +9,16 @@ import (
type ScenarioService struct { type ScenarioService struct {
scenariosRepo *scenarios_repo.ScenariosRepo scenariosRepo *scenarios_repo.ScenariosRepo
domain string
} }
func NewScenarioService( func NewScenarioService(
scenariosRepo *scenarios_repo.ScenariosRepo, scenariosRepo *scenarios_repo.ScenariosRepo,
domain string,
) *ScenarioService { ) *ScenarioService {
return &ScenarioService{ return &ScenarioService{
scenariosRepo: scenariosRepo, scenariosRepo: scenariosRepo,
domain: domain,
} }
} }
@@ -39,7 +42,7 @@ func (s *ScenarioService) GetScenariosByAuthorID(
if err != nil { if err != nil {
return nil, err return nil, err
} }
return mapScenarios(scenarios) return mapScenarios(scenarios, s.domain)
} }
func (s *ScenarioService) GetScenarioByID( func (s *ScenarioService) GetScenarioByID(
@@ -50,7 +53,7 @@ func (s *ScenarioService) GetScenarioByID(
if err != nil { if err != nil {
return nil, err return nil, err
} }
return mapScenario(scenario) return mapScenario(scenario, s.domain)
} }
func (s *ScenarioService) UpdateScenarioByID( func (s *ScenarioService) UpdateScenarioByID(
@@ -2,7 +2,7 @@
CREATE TABLE CREATE TABLE
IF NOT EXISTS scenarios ( IF NOT EXISTS scenarios (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE, name VARCHAR(255) NOT NULL,
description TEXT, description TEXT,
image TEXT, image TEXT,
scenario JSONB NOT NULL DEFAULT '{}'::jsonb, scenario JSONB NOT NULL DEFAULT '{}'::jsonb,