Skip to content

Commit

Permalink
go fmt実行
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuyainoue0124 committed Dec 16, 2024
1 parent bfdfdbd commit ceaa28b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 28 deletions.
1 change: 0 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func main() {
mux := router.NewRouter(h)
portStr := ":" + strconv.Itoa(cfg.App.Port)


srv := &http.Server{
Addr: portStr,
Handler: mux,
Expand Down
24 changes: 12 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ type Config struct {
Port int
}
DB struct {
Host string
Port int
User string
Host string
Port int
User string
Password string
Name string
Name string
}
}

func LoadConfig() (*Config, error) {
c := &Config{}

// 環境変数から読み込む例
c.App.Port = getEnvAsInt("PORT", 8080)
c.DB.Host = getEnv("DB_HOST", "db")
c.DB.Port = getEnvAsInt("DB_PORT", 3306)
c.DB.User = getEnv("DB_USER", "todouser")
c.DB.Password = getEnv("DB_PASSWORD", "secret")
c.DB.Name = getEnv("DB_NAME", "todo")
// 環境変数から読み込む例
c.App.Port = getEnvAsInt("PORT", 8080)
c.DB.Host = getEnv("DB_HOST", "db")
c.DB.Port = getEnvAsInt("DB_PORT", 3306)
c.DB.User = getEnv("DB_USER", "todouser")
c.DB.Password = getEnv("DB_PASSWORD", "secret")
c.DB.Name = getEnv("DB_NAME", "todo")

return c, nil
}
Expand All @@ -49,4 +49,4 @@ func getEnvAsInt(key string, defaultValue int) int {
return defaultValue
}
return i
}
}
6 changes: 3 additions & 3 deletions domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package domain
import "errors"

var (
ErrNotFound = errors.New("not found")
ErrInvalid = errors.New("invalid input")
ErrNotFound = errors.New("not found")
ErrInvalid = errors.New("invalid input")
ErrTitleEmpty = errors.New("title cannot be empty")
)
)
8 changes: 4 additions & 4 deletions domain/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func NewTask(title, description string) (*Task, error) {
return nil, ErrTitleEmpty
}
return &Task{
Title: title,
Title: title,
Description: description,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}, nil
}

Expand All @@ -30,4 +30,4 @@ func (t *Task) Update(title, description string) error {
t.Description = description
t.UpdatedAt = time.Now()
return nil
}
}
1 change: 0 additions & 1 deletion domain/task_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ type ITaskRepository interface {
UpdateTask(ctx context.Context, t *Task) error
DeleteTask(ctx context.Context, id int64) error
}

4 changes: 2 additions & 2 deletions infrastructure/db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
func NewMySQLDB(cfg *config.Config) (*sql.DB, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true&loc=Local&charset=utf8mb4",
cfg.DB.User, cfg.DB.Password, cfg.DB.Host, cfg.DB.Port, cfg.DB.Name)

db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}

db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)

if err := db.Ping(); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion presentation/handlers/request_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ type UpdateTaskRequest struct {

type ErrorResponse struct {
Message string `json:"message"`
}
}
8 changes: 4 additions & 4 deletions presentation/handlers/task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ func (h *TaskHandler) GetAllTasks(w http.ResponseWriter, r *http.Request) {

// GetTaskById: GET /tasks/{id}
func (h *TaskHandler) GetTaskById(w http.ResponseWriter, r *http.Request) {
fmt.Printf("GetTaskById called with path: %s\n", r.URL.Path) // デバッグログ追加
fmt.Printf("GetTaskById called with path: %s\n", r.URL.Path) // デバッグログ追加

id, err := extractIdFromPath(r.URL.Path, "/tasks/")
if err != nil {
fmt.Printf("Error extracting ID: %v\n", err) // デバッグログ追加
fmt.Printf("Error extracting ID: %v\n", err) // デバッグログ追加
http.Error(w, "invalid id", http.StatusBadRequest)
return
}

fmt.Printf("Extracted ID: %d\n", id) // デバッグログ追加
fmt.Printf("Extracted ID: %d\n", id) // デバッグログ追加

task, err := h.u.GetTaskById(context.Background(), id)
if err != nil {
fmt.Printf("Error getting task: %v\n", err) // デバッグログ追加
fmt.Printf("Error getting task: %v\n", err) // デバッグログ追加
if errors.Is(err, domain.ErrNotFound) {
http.NotFound(w, r)
return
Expand Down

0 comments on commit ceaa28b

Please sign in to comment.