Skip to content

Commit

Permalink
Initialize directory structure (#437)
Browse files Browse the repository at this point in the history
* init directory

* add readme

* Replace empty file to .gitkeep

* Add server config

* Add basic server

* Add routes

* Edit document

- project-root -> backend-go

* Edit document

- yorkie directory explain

* Add graceful shutdown

* Clear server configuration

- This implement is out of this PR's goal

* Apply code rabbit's comment

* Replace net/http to echo

* Remove read timeout in server config

* Refactor server Start and Shutdown

- run gorutine in main.go
- logging in main.go
- server only return error

* Fix directory_structure.md

- domain directory -> core
- remove API documentation in api directory

* Remove graceful shutdown

- this is not needed for now
- this implementation makes more complexity for contribution

* Rename directory name `domain` -> `core`

* Rename `dto` directory to `codepair/v1`

* Remove `auth` directory

* Fix redundant error

* Remove .gitkeep in `api/codepair/v1`

* Change logger

---------

Co-authored-by: devleejb <[email protected]>
  • Loading branch information
window9u and devleejb authored Feb 1, 2025
1 parent 67c689c commit 04b398e
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 8 deletions.
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)
})
}
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
}

0 comments on commit 04b398e

Please sign in to comment.