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}",
|
"cwd": "${workspaceFolder}",
|
||||||
"buildFlags": "-tags local",
|
"buildFlags": "-tags local",
|
||||||
"env": {
|
"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
|
rm -f ./cmd/text_to_story/story.json
|
||||||
go run ./cmd/text_to_story/main.go
|
go run ./cmd/text_to_story/main.go
|
||||||
|
|
||||||
|
clear:
|
||||||
|
rm ./internal/tests/store.db
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test ./...
|
go test ./...
|
||||||
|
@ -27,12 +27,13 @@ func main() {
|
|||||||
// Create a gRPC server object
|
// Create a gRPC server object
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
// Attach the Greeter service to the server
|
// Attach the Greeter service to the server
|
||||||
repository, err := services.NewRepository()
|
dbFilepath := config.GetDBFilepath()
|
||||||
|
repository, err := services.NewRepository(dbFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
storyFilepath := config.GetStoryFilePath()
|
storyFilepath := config.GetStoryFilepath()
|
||||||
storyService, err := story_service.NewStoryService(storyFilepath)
|
storyService, err := story_service.NewStoryService(storyFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
|
@ -2,10 +2,18 @@ package config
|
|||||||
|
|
||||||
import "os"
|
import "os"
|
||||||
|
|
||||||
func GetStoryFilePath() string {
|
func GetStoryFilepath() string {
|
||||||
storyFilename := os.Getenv("STORY_FILENAME")
|
storyFilename := os.Getenv("STORY_FILENAME")
|
||||||
if storyFilename != "" {
|
if storyFilename != "" {
|
||||||
return storyFilename
|
return storyFilename
|
||||||
}
|
}
|
||||||
return "./data/story/story.json"
|
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"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"evening_detective/internal/models"
|
"evening_detective/internal/models"
|
||||||
|
"log"
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
@ -13,11 +14,12 @@ type Repository struct {
|
|||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRepository() (*Repository, error) {
|
func NewRepository(filepath string) (*Repository, error) {
|
||||||
db, err := sql.Open("sqlite3", "data/db/store.db")
|
db, err := sql.Open("sqlite3", filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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);")
|
_, err = db.Exec("CREATE TABLE IF NOT EXISTS teams (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password TEXT);")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err := s.repository.AddApplications(ctx, actions); err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, err.Error())
|
return nil, status.Errorf(codes.Internal, err.Error())
|
||||||
}
|
}
|
||||||
log(team, "add action", actions)
|
addLog(team, "add action", actions)
|
||||||
return &proto.AddActionRsp{}, nil
|
return &proto.AddActionRsp{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +190,7 @@ func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
|
|||||||
return team, nil
|
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)
|
vJson, err := json.Marshal(v)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
|
fmt.Printf("Team %s: %s %s\n", team.Name, action, string(vJson))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user