generated from VLADIMIR/template
add db filepath
This commit is contained in:
parent
3a2980e9d0
commit
54706f0aba
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
3
Makefile
3
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 ./...
|
||||
|
@ -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)
|
||||
|
@ -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"
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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))
|
||||
|
Loading…
x
Reference in New Issue
Block a user