-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize directory structure (#437)
* 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
Showing
15 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package config | ||
|
||
const ( | ||
DefaultServerPort = 3001 | ||
) | ||
|
||
type Server struct { | ||
Port int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |