Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize directory structure #437

Merged
merged 22 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions backend-go/cmd/codepair/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package main

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
"github.com/yorkie-team/codepair/backend/internal/config"
"github.com/yorkie-team/codepair/backend/internal/server"
)

func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
err := c.String(http.StatusOK, "Hello, World!")
return fmt.Errorf("error: %w", err)
})
e.Logger.Fatal(e.Start(":3001"))
conf := config.LoadConfig()
cp := server.New(e, conf)

if err := cp.Start(); err != nil {
e.Logger.Fatal(err)
}
}
Empty file added backend-go/config.yaml
Empty file.
74 changes: 74 additions & 0 deletions backend-go/design/directory_structure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Directory Structure

This document explains the purpose and structure of the project's directories.

## Project Structure

```
backend-go/
├── api # DTO definitions
├── bin # Compiled binaries
├── cmd # Application entry points
│ └── codepair # Main application entry point (main.go)
├── design # Design documents and architecture references
└── internal # Core application logic (business logic, infrastructure, middleware)
```
### Structure of `internal`

```
internal
├── config # Configuration loading & validation
├── core # Business logic and domain entities
│ ├── workspace # Workspace management module
│ ├── user # User management module
│ └── ... # Other domain-specific modules
├── infra # Infrastructure dependencies (DB, storage, external services)
│ ├── database # Database integrations
│ │ └── mongo # MongoDB implementation
│ ├── storage # Object storage (MinIO, S3, etc.)
│ │ ├── minio # MinIO storage implementation
│ │ └── s3 # AWS S3 storage implementation
│ └── yorkie # Yorkie admin client implementation
├── middleware # Shared middleware (logging, auth, etc.)
└── server # Server setup, routing, and bootstrapping
```

### Explanation of Each Component

#### 1. `internal/config`

- Responsible for **loading and validating configurations** (e.g., using `viper` or `os.Getenv`).
- Contains separate configuration files for authentication, database, and other settings.

#### 2. `internal/core`

- Contains the **business logic and domain models**.
- Each domain (e.g., `workspace`, `user`) includes:
- `model.go` → Defines entity structures (e.g., `User`, `Workspace`).
- `repository.go` → Interface for database operations.
- `service.go` → Implementation of business logic.
- `handler.go` → Presentation layer handlers for processing HTTP requests.

#### 3. `internal/infra`

- Represents the **infrastructure layer** responsible for handling external dependencies such as databases, storage, and external services.
- Organized into:
- `database/mongo/` → MongoDB implementation for repositories.
- `storage/` → Object storage implementations (MinIO, S3).
- `yorkie/` → Contains the Yorkie admin client, which handles administrative functions.

#### 4. `internal/middleware`

- Contains **shared middleware** applied across the application.
- Common middleware includes:
- Logging
- Authentication & authorization
- Error handling & panic recovery
- Request validation

#### 5. `internal/server`

- Handles **server setup and routing**.
- Includes:
- `routes.go` → Defines API endpoints and middleware.
- `server.go` → Initializes the server, sets up dependencies, and starts the application.
13 changes: 13 additions & 0 deletions backend-go/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

type Config struct {
Server *Server
}

func LoadConfig() *Config {
return &Config{
Server: &Server{
Port: DefaultServerPort,
},
}
}
9 changes: 9 additions & 0 deletions backend-go/internal/config/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package config

const (
DefaultServerPort = 3001
)

type Server struct {
Port int
}
1 change: 1 addition & 0 deletions backend-go/internal/core/user/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package user
1 change: 1 addition & 0 deletions backend-go/internal/core/user/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package user
1 change: 1 addition & 0 deletions backend-go/internal/core/user/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package user
1 change: 1 addition & 0 deletions backend-go/internal/infra/database/mongo/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file.
Empty file.
Empty file.
Empty file.
15 changes: 15 additions & 0 deletions backend-go/internal/server/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

import (
"fmt"
"net/http"

"github.com/labstack/echo/v4"
)

func RegisterRoutes(e *echo.Echo) {
e.GET("/", func(c echo.Context) error {
err := c.String(http.StatusOK, "Hello, World!")
return fmt.Errorf("error: %w", err)
})
}
devleejb marked this conversation as resolved.
Show resolved Hide resolved
34 changes: 34 additions & 0 deletions backend-go/internal/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package server

import (
"errors"
"fmt"
"net/http"

"github.com/labstack/echo/v4"

"github.com/yorkie-team/codepair/backend/internal/config"
)

type CodePair struct {
config *config.Config
echo *echo.Echo
}

func New(e *echo.Echo, conf *config.Config) *CodePair {
RegisterRoutes(e)

cp := &CodePair{
config: conf,
echo: e,
}
return cp
}

func (c *CodePair) Start() error {
addr := fmt.Sprintf(":%d", c.config.Server.Port)
if err := c.echo.Start(addr); !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("failed to start server: %w", err)
}
return nil
}