Refactory packages
This commit is contained in:
parent
3596f8597b
commit
3879c9bd24
@ -1,42 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"pink_fox/internal/app"
|
||||
conf2 "pink_fox/internal/app/config"
|
||||
"pink_fox/internal/app/db"
|
||||
"pink_fox/internal/app/log"
|
||||
"pink_fox/internal/http_server"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{}
|
||||
}
|
||||
|
||||
func (it *Server) Execute(defaultConfig string, defaultPort int) error {
|
||||
conf, err := conf2.LoadConfig(defaultConfig, defaultPort)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
|
||||
conn, err := db.CreateConnection(&conf.Db)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
defer func(conn *sql.DB) {
|
||||
_ = conn.Close()
|
||||
}(conn)
|
||||
|
||||
logger, err := log.NewLogger(conf.LogFile)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
defer func(logger *log.Logger) {
|
||||
_ = logger.Close()
|
||||
}(logger)
|
||||
|
||||
return http_server.StartServer(app.New(conf, conn, logger))
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"pink_fox/internal/http_server/http"
|
||||
)
|
||||
|
||||
type IndexController struct {
|
||||
responseFactory *http.ResponseFactory
|
||||
}
|
||||
|
||||
type IndexControllerDI interface {
|
||||
MakeResponseFactory() *http.ResponseFactory
|
||||
}
|
||||
|
||||
func NewIndexController(di IndexControllerDI) *IndexController {
|
||||
return &IndexController{
|
||||
responseFactory: di.MakeResponseFactory(),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *IndexController) ActionIndex() http.Response {
|
||||
return it.responseFactory.String("Hello world!")
|
||||
}
|
||||
@ -1,115 +0,0 @@
|
||||
package manager_route
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pink_fox/internal/app"
|
||||
"pink_fox/internal/http_server/http"
|
||||
"pink_fox/internal/inject"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
engine *gin.Engine
|
||||
application *app.Application
|
||||
middlewares []inject.MiddlewareFunc
|
||||
}
|
||||
|
||||
func NewManager(e *gin.Engine, application *app.Application) *Manager {
|
||||
return &Manager{
|
||||
engine: e,
|
||||
application: application,
|
||||
middlewares: make([]inject.MiddlewareFunc, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Manager) Group(relativePath string, middlewares ...inject.MiddlewareFunc) *Group {
|
||||
return NewGroup(it, relativePath, middlewares...)
|
||||
}
|
||||
|
||||
func (it *Manager) Use(middlewares ...inject.MiddlewareFunc) {
|
||||
it.middlewares = append(it.middlewares, middlewares...)
|
||||
}
|
||||
|
||||
func (it *Manager) ANY(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.GET(relativePath, handlerForGin)
|
||||
it.engine.POST(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Manager) GET(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.GET(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Manager) POST(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.POST(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
manager *Manager
|
||||
relativePath string
|
||||
middlewares []inject.MiddlewareFunc
|
||||
}
|
||||
|
||||
func NewGroup(route *Manager, relativePath string, middlewares ...inject.MiddlewareFunc) *Group {
|
||||
return &Group{
|
||||
manager: route,
|
||||
relativePath: relativePath,
|
||||
middlewares: append(route.middlewares, middlewares...),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Group) Group(relativePath string, middlewares ...inject.MiddlewareFunc) *Group {
|
||||
return &Group{
|
||||
manager: it.manager,
|
||||
relativePath: it.relativePath + relativePath,
|
||||
middlewares: append(it.middlewares, middlewares...),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Group) Use(middlewares ...inject.MiddlewareFunc) {
|
||||
it.middlewares = append(it.middlewares, middlewares...)
|
||||
}
|
||||
|
||||
func (it *Group) ANY(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.manager.application)
|
||||
it.manager.engine.GET(it.relativePath+relativePath, handlerForGin)
|
||||
it.manager.engine.POST(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Group) GET(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.manager.application)
|
||||
it.manager.engine.GET(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Group) POST(relativePath string, handler inject.HandlerFunc, middlewares ...inject.MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.manager.application)
|
||||
it.manager.engine.POST(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func makeHandlerGin(handler inject.HandlerFunc, middlewares []inject.MiddlewareFunc, application *app.Application) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
di := inject.NewDI(application, ctx)
|
||||
defer di.Close()
|
||||
res := pipeline(handler, middlewares, di)
|
||||
res.Render()
|
||||
}
|
||||
}
|
||||
|
||||
func pipeline(endpoint inject.HandlerFunc, middlewares []inject.MiddlewareFunc, di *inject.DI) http.Response {
|
||||
handler := func() http.Response {
|
||||
return endpoint(di)
|
||||
}
|
||||
|
||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||
handler = createMiddleware(handler, middlewares[i], di)
|
||||
}
|
||||
|
||||
return handler()
|
||||
}
|
||||
|
||||
func createMiddleware(next inject.ActionFunc, middleware inject.MiddlewareFunc, di *inject.DI) inject.ActionFunc {
|
||||
return func() http.Response {
|
||||
return middleware(di, next)
|
||||
}
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"pink_fox/internal/controllers"
|
||||
"pink_fox/internal/http_server/http"
|
||||
"pink_fox/internal/http_server/manager_route"
|
||||
"pink_fox/internal/inject"
|
||||
)
|
||||
|
||||
func RegistrationRoutes(r *manager_route.Manager) {
|
||||
r.GET("/", IndexController)
|
||||
}
|
||||
|
||||
func IndexController(di *inject.DI) http.Response {
|
||||
return controllers.NewIndexController(di).ActionIndex()
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"pink_fox/internal/app"
|
||||
"pink_fox/internal/http_server/manager_route"
|
||||
)
|
||||
|
||||
func StartServer(application *app.Application) error {
|
||||
r := gin.Default()
|
||||
_ = r.SetTrustedProxies(nil)
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "ok")
|
||||
})
|
||||
|
||||
RegistrationRoutes(manager_route.NewManager(r, application))
|
||||
|
||||
fmt.Printf("Starting http_server at port %d...", application.Conf.Port)
|
||||
err := r.Run(fmt.Sprintf(":%d", application.Conf.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
package inject
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pink_fox/internal/app"
|
||||
"pink_fox/internal/http_server/http"
|
||||
)
|
||||
|
||||
type DI struct {
|
||||
application *app.Application
|
||||
context *gin.Context
|
||||
}
|
||||
|
||||
func NewDI(application *app.Application, ctx *gin.Context) *DI {
|
||||
return &DI{
|
||||
application: application,
|
||||
context: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (it *DI) Close() {
|
||||
}
|
||||
|
||||
func (it *DI) MakeResponseFactory() *http.ResponseFactory {
|
||||
return http.NewResponseFactory(it.context)
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
package inject
|
||||
|
||||
import (
|
||||
"pink_fox/internal/http_server/http"
|
||||
)
|
||||
|
||||
type MiddlewareFunc func(*DI, ActionFunc) http.Response
|
||||
|
||||
type HandlerFunc func(*DI) http.Response
|
||||
|
||||
type ActionFunc func() http.Response
|
||||
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"pink_fox/internal/app/cmd"
|
||||
"pink_fox/src/app/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
@ -2,17 +2,17 @@ package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"pink_fox/internal/app/config"
|
||||
"pink_fox/internal/app/log"
|
||||
"pink_fox/src/app/config"
|
||||
"pink_fox/src/app/logger"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
Conf *config.Config
|
||||
Conn *sql.DB
|
||||
Logger *log.Logger
|
||||
Logger *logger.Logger
|
||||
}
|
||||
|
||||
func New(conf *config.Config, conn *sql.DB, logger *log.Logger) *Application {
|
||||
func New(conf *config.Config, conn *sql.DB, logger *logger.Logger) *Application {
|
||||
return &Application{
|
||||
Conf: conf,
|
||||
Conn: conn,
|
||||
64
pink_fox_app/src/app/cmd/server.go
Normal file
64
pink_fox_app/src/app/cmd/server.go
Normal file
@ -0,0 +1,64 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"pink_fox/src/app"
|
||||
c "pink_fox/src/app/config"
|
||||
"pink_fox/src/app/db"
|
||||
l "pink_fox/src/app/logger"
|
||||
"pink_fox/src/app/routes_manager"
|
||||
"pink_fox/src/routes"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
return &Server{}
|
||||
}
|
||||
|
||||
func (it *Server) Execute(defaultConfig string, defaultPort int) error {
|
||||
conf, err := c.LoadConfig(defaultConfig, defaultPort)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
|
||||
conn, err := db.CreateConnection(&conf.Db)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
defer func(conn *sql.DB) {
|
||||
_ = conn.Close()
|
||||
}(conn)
|
||||
|
||||
logger, err := l.NewLogger(conf.LogFile)
|
||||
if err != nil {
|
||||
exit(err)
|
||||
}
|
||||
defer func(logger *l.Logger) {
|
||||
_ = logger.Close()
|
||||
}(logger)
|
||||
|
||||
return it.StartServer(app.New(conf, conn, logger))
|
||||
}
|
||||
|
||||
func (it *Server) StartServer(application *app.Application) error {
|
||||
r := gin.Default()
|
||||
_ = r.SetTrustedProxies(nil)
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, "ok")
|
||||
})
|
||||
|
||||
routes.RegistrationRoutes(routes_manager.NewManager(r, application))
|
||||
|
||||
fmt.Printf("Starting http_server at port %d...", application.Conf.Port)
|
||||
err := r.Run(fmt.Sprintf(":%d", application.Conf.Port))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -38,7 +38,7 @@ func setDefaultValue(conf *Config, defaultPort int) *Config {
|
||||
conf.Port = defaultPort
|
||||
}
|
||||
if conf.LogFile == "" {
|
||||
conf.LogFile = "/var/log/pink_fox/app.log"
|
||||
conf.LogFile = "/var/logger/pink_fox/app.logger"
|
||||
}
|
||||
return conf
|
||||
}
|
||||
@ -4,7 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
"pink_fox/internal/app/config"
|
||||
"pink_fox/src/app/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package http
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -1,4 +1,4 @@
|
||||
package http
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -1,4 +1,4 @@
|
||||
package log
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
121
pink_fox_app/src/app/routes_manager/manager.go
Normal file
121
pink_fox_app/src/app/routes_manager/manager.go
Normal file
@ -0,0 +1,121 @@
|
||||
package routes_manager
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pink_fox/src/app"
|
||||
"pink_fox/src/app/http_server"
|
||||
"pink_fox/src/dependence"
|
||||
)
|
||||
|
||||
type RoutesManager struct {
|
||||
engine *gin.Engine
|
||||
application *app.Application
|
||||
middlewares []MiddlewareFunc
|
||||
}
|
||||
|
||||
func NewManager(e *gin.Engine, application *app.Application) *RoutesManager {
|
||||
return &RoutesManager{
|
||||
engine: e,
|
||||
application: application,
|
||||
middlewares: make([]MiddlewareFunc, 0),
|
||||
}
|
||||
}
|
||||
|
||||
type MiddlewareFunc func(*dependence.Container, ActionFunc) http_server.Response
|
||||
|
||||
type HandlerFunc func(container *dependence.Container) http_server.Response
|
||||
|
||||
type ActionFunc func() http_server.Response
|
||||
|
||||
func (it *RoutesManager) Group(relativePath string, middlewares ...MiddlewareFunc) *Group {
|
||||
return NewGroup(it, relativePath, middlewares...)
|
||||
}
|
||||
|
||||
func (it *RoutesManager) Use(middlewares ...MiddlewareFunc) {
|
||||
it.middlewares = append(it.middlewares, middlewares...)
|
||||
}
|
||||
|
||||
func (it *RoutesManager) ANY(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.GET(relativePath, handlerForGin)
|
||||
it.engine.POST(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *RoutesManager) GET(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.GET(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *RoutesManager) POST(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.application)
|
||||
it.engine.POST(relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
routesManager *RoutesManager
|
||||
relativePath string
|
||||
middlewares []MiddlewareFunc
|
||||
}
|
||||
|
||||
func NewGroup(route *RoutesManager, relativePath string, middlewares ...MiddlewareFunc) *Group {
|
||||
return &Group{
|
||||
routesManager: route,
|
||||
relativePath: relativePath,
|
||||
middlewares: append(route.middlewares, middlewares...),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Group) Group(relativePath string, middlewares ...MiddlewareFunc) *Group {
|
||||
return &Group{
|
||||
routesManager: it.routesManager,
|
||||
relativePath: it.relativePath + relativePath,
|
||||
middlewares: append(it.middlewares, middlewares...),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Group) Use(middlewares ...MiddlewareFunc) {
|
||||
it.middlewares = append(it.middlewares, middlewares...)
|
||||
}
|
||||
|
||||
func (it *Group) ANY(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.routesManager.application)
|
||||
it.routesManager.engine.GET(it.relativePath+relativePath, handlerForGin)
|
||||
it.routesManager.engine.POST(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Group) GET(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.routesManager.application)
|
||||
it.routesManager.engine.GET(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func (it *Group) POST(relativePath string, handler HandlerFunc, middlewares ...MiddlewareFunc) {
|
||||
handlerForGin := makeHandlerGin(handler, append(it.middlewares, middlewares...), it.routesManager.application)
|
||||
it.routesManager.engine.POST(it.relativePath+relativePath, handlerForGin)
|
||||
}
|
||||
|
||||
func makeHandlerGin(handler HandlerFunc, middlewares []MiddlewareFunc, application *app.Application) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
dic := dependence.NewContainer(application, ctx)
|
||||
defer dic.Close()
|
||||
res := pipeline(handler, middlewares, dic)
|
||||
res.Render()
|
||||
}
|
||||
}
|
||||
|
||||
func pipeline(endpoint HandlerFunc, middlewares []MiddlewareFunc, di *dependence.Container) http_server.Response {
|
||||
handler := func() http_server.Response {
|
||||
return endpoint(di)
|
||||
}
|
||||
|
||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||
handler = createMiddleware(handler, middlewares[i], di)
|
||||
}
|
||||
|
||||
return handler()
|
||||
}
|
||||
|
||||
func createMiddleware(next ActionFunc, middleware MiddlewareFunc, di *dependence.Container) ActionFunc {
|
||||
return func() http_server.Response {
|
||||
return middleware(di, next)
|
||||
}
|
||||
}
|
||||
23
pink_fox_app/src/controllers/Index.go
Normal file
23
pink_fox_app/src/controllers/Index.go
Normal file
@ -0,0 +1,23 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"pink_fox/src/app/http_server"
|
||||
)
|
||||
|
||||
type IndexController struct {
|
||||
responseFactory *http_server.ResponseFactory
|
||||
}
|
||||
|
||||
type IndexControllerContainer interface {
|
||||
MakeResponseFactory() *http_server.ResponseFactory
|
||||
}
|
||||
|
||||
func NewIndexController(di IndexControllerContainer) *IndexController {
|
||||
return &IndexController{
|
||||
responseFactory: di.MakeResponseFactory(),
|
||||
}
|
||||
}
|
||||
|
||||
func (it *IndexController) ActionIndex() http_server.Response {
|
||||
return it.responseFactory.String("Hello world!")
|
||||
}
|
||||
26
pink_fox_app/src/dependence/container.go
Normal file
26
pink_fox_app/src/dependence/container.go
Normal file
@ -0,0 +1,26 @@
|
||||
package dependence
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pink_fox/src/app"
|
||||
"pink_fox/src/app/http_server"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
application *app.Application
|
||||
context *gin.Context
|
||||
}
|
||||
|
||||
func NewContainer(application *app.Application, context *gin.Context) *Container {
|
||||
return &Container{
|
||||
application: application,
|
||||
context: context,
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Container) Close() {
|
||||
}
|
||||
|
||||
func (it *Container) MakeResponseFactory() *http_server.ResponseFactory {
|
||||
return nil
|
||||
}
|
||||
16
pink_fox_app/src/routes/registration.go
Normal file
16
pink_fox_app/src/routes/registration.go
Normal file
@ -0,0 +1,16 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"pink_fox/src/app/http_server"
|
||||
"pink_fox/src/app/routes_manager"
|
||||
"pink_fox/src/controllers"
|
||||
di "pink_fox/src/dependence"
|
||||
)
|
||||
|
||||
func RegistrationRoutes(r *routes_manager.RoutesManager) {
|
||||
r.GET("/", IndexController)
|
||||
}
|
||||
|
||||
func IndexController(dic *di.Container) http_server.Response {
|
||||
return controllers.NewIndexController(dic).ActionIndex()
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user