diff --git a/.vscode/launch.json b/.vscode/launch.json index 7ecbabc..dd14571 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -28,7 +28,8 @@ "cwd": "${workspaceFolder}", "buildFlags": "-tags local", "env": { - "STORY_FILENAME": "./internal/tests/story.json" + "STORY_FILENAME": "./internal/tests/story.json", + "DB_FILENAME": "./internal/tests/store.db" } } ] diff --git a/Makefile b/Makefile index fbb7d9f..4d0cbda 100644 --- a/Makefile +++ b/Makefile @@ -19,5 +19,8 @@ text_to_story: rm -f ./cmd/text_to_story/story.json go run ./cmd/text_to_story/main.go +clear: + rm ./internal/tests/store.db + test: go test ./... diff --git a/cmd/evening_detective/main.go b/cmd/evening_detective/main.go index 827fd8e..6ea05bb 100644 --- a/cmd/evening_detective/main.go +++ b/cmd/evening_detective/main.go @@ -27,12 +27,13 @@ func main() { // Create a gRPC server object s := grpc.NewServer() // Attach the Greeter service to the server - repository, err := services.NewRepository() + dbFilepath := config.GetDBFilepath() + repository, err := services.NewRepository(dbFilepath) if err != nil { panic(err) } - storyFilepath := config.GetStoryFilePath() + storyFilepath := config.GetStoryFilepath() storyService, err := story_service.NewStoryService(storyFilepath) if err != nil { log.Fatalln(err) diff --git a/internal/config/config.go b/internal/config/config.go index c16e730..01c2997 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -2,10 +2,18 @@ package config import "os" -func GetStoryFilePath() string { +func GetStoryFilepath() string { storyFilename := os.Getenv("STORY_FILENAME") if storyFilename != "" { return storyFilename } return "./data/story/story.json" } + +func GetDBFilepath() string { + storyFilename := os.Getenv("DB_FILENAME") + if storyFilename != "" { + return storyFilename + } + return "data/db/store.db" +} diff --git a/internal/services/repository.go b/internal/services/repository.go index fb52234..8abc42e 100644 --- a/internal/services/repository.go +++ b/internal/services/repository.go @@ -5,6 +5,7 @@ import ( "database/sql" "errors" "evening_detective/internal/models" + "log" _ "github.com/mattn/go-sqlite3" ) @@ -13,11 +14,12 @@ type Repository struct { db *sql.DB } -func NewRepository() (*Repository, error) { - db, err := sql.Open("sqlite3", "data/db/store.db") +func NewRepository(filepath string) (*Repository, error) { + db, err := sql.Open("sqlite3", filepath) if err != nil { return nil, err } + log.Printf("load db from: %s", filepath) _, err = db.Exec("CREATE TABLE IF NOT EXISTS teams (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT);") if err != nil { return nil, err diff --git a/internal/services/services.go b/internal/services/services.go index e99c08c..2c8df4c 100644 --- a/internal/services/services.go +++ b/internal/services/services.go @@ -81,7 +81,7 @@ func (s *Services) AddAction(ctx context.Context, req *proto.AddActionReq) (*pro if err := s.repository.AddApplications(ctx, actions); err != nil { return nil, status.Errorf(codes.Internal, err.Error()) } - log(team, "add action", actions) + addLog(team, "add action", actions) return &proto.AddActionRsp{}, nil } @@ -190,7 +190,7 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) { return team, nil } -func log(team *models.Team, action string, v any) { +func addLog(team *models.Team, action string, v any) { vJson, err := json.Marshal(v) if err == nil { fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))