Nunu adopts a classic layered architecture. Additionally, to achieve better modularity and decoupling, it utilizes the dependency injection framework Wire
.
.
├── api
│ └── v1
├── cmd
│ ├── migration
│ ├── server
│ │ ├── wire
│ │ │ ├── wire.go
│ │ │ └── wire_gen.go
│ │ └── main.go
│ └── task
├── config
├── deploy
├── docs
├── internal
│ ├── handler
│ ├── middleware
│ ├── model
│ ├── repository
│ ├── server
│ └── service
├── pkg
├── scripts
├── test
│ ├── mocks
│ └── server
├── web
├── Makefile
├── go.mod
└── go.sum
cmd
: Entry point of the application, containing different subcommands.config
: Configuration files.deploy
: Files related to deployment, such as Dockerfile and docker-compose.yml.internal
: Main code of the application, organized according to the layered architecture.pkg
: Common code, including configuration, logging, and HTTP.scripts
: Script files for deployment and other automation tasks.storage
: Storage files, such as log files.test
: Test code.web
: Front-end code.
internal/handler
(orcontroller
): Handles HTTP requests, calls services in the business logic layer, and returns HTTP responses.internal/server
(orrouter
): HTTP server that starts the HTTP service, listens to ports, and handles HTTP requests.internal/service
(orlogic
): Services that implement specific business logic and call the data access layer (repository).internal/model
(orentity
): Data models that define the data structures needed by the business logic layer.internal/repository
(ordao
): Data access objects that encapsulate database operations and provide CRUD operations on the data.
This project utilizes the dependency injection framework Wire
to achieve modularity and decoupling. Wire
generates dependency injection code wire_gen.go
by precompiling wire.go
, simplifying the process of dependency injection.
cmd/job/wire.go
:Wire
configuration file that defines the dependencies required by thejob
subcommand.cmd/migration/wire.go
:Wire
configuration file that defines the dependencies required by themigration
subcommand.cmd/server/wire.go
:Wire
configuration file that defines the dependencies required by theserver
subcommand.
Wire official documentation: https://github.com/google/wire/blob/main/docs/guide.md
Note: The wire_gen.go
file is automatically generated during compilation and should not be manually modified.
To achieve code reuse and centralized management, this project adopts a common code approach, where some common code is placed under the pkg
directory.
pkg/config
: Handles reading and parsing configuration files.pkg/helper
: Contains various utility functions, such as MD5 encryption and UUID generation.pkg/http
: Contains HTTP-related code, such as HTTP clients and HTTP servers.pkg/log
: Contains logging-related code, such as log initialization and writing.more...
: Of course, you can freely add and expand more packages as needed.