Новая ошибка и собрана первая версия роутинга

This commit is contained in:
Michael Makarochkin 2025-03-03 19:58:03 +03:00
parent f6521b9332
commit 3596f8597b
19 changed files with 462 additions and 26 deletions

View File

@ -10,9 +10,9 @@ services:
- "12002:2345"
volumes:
- ./pink_fox_app:/app
- ./pink_fox_app/log:/var/log/pink_fox
- ./dist:/dist
- ./environment:/var/environment/pink_fox
- ./log:/var/log/pink_fox
- ./.storage/go:/go
environment:
- ARG1

View File

@ -1,2 +1,3 @@
out.log
err.log
err.log
pink_fox

2
pink_fox_app/dlv.log Normal file
View File

@ -0,0 +1,2 @@
2025-03-03T16:51:40Z warning layer=rpc Listening for remote connections (connections are not authenticated nor encrypted)
не удалось открыть файл лога: open : no such file or directory

View File

@ -3,9 +3,35 @@ module pink_fox
go 1.24.0
require (
github.com/bytedance/sonic v1.12.9 // indirect
github.com/bytedance/sonic/loader v0.2.3 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/gin-gonic/gin v1.10.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.25.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/spf13/cobra v1.9.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.14.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -1,13 +1,88 @@
github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ=
github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.3 h1:yctD0Q3v2NOGfSWPLPvG2ggA2kV6TS6s4wioyEqssH0=
github.com/bytedance/sonic/loader v0.2.3/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.14.0 h1:z9JUEZWr8x4rR0OU6c4/4t6E6jOZ8/QBS2bBYBm4tx4=
golang.org/x/arch v0.14.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
google.golang.org/protobuf v1.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM=
google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@ -0,0 +1,21 @@
package app
import (
"database/sql"
"pink_fox/internal/app/config"
"pink_fox/internal/app/log"
)
type Application struct {
Conf *config.Config
Conn *sql.DB
Logger *log.Logger
}
func New(conf *config.Config, conn *sql.DB, logger *log.Logger) *Application {
return &Application{
Conf: conf,
Conn: conn,
Logger: logger,
}
}

View File

@ -2,11 +2,11 @@ package cmd
import (
"database/sql"
"fmt"
"net/http"
"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 {
@ -38,20 +38,5 @@ func (it *Server) Execute(defaultConfig string, defaultPort int) error {
_ = logger.Close()
}(logger)
// TODO доделать
http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "ok")
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "Hello world!")
})
fmt.Printf("Starting server at port %d...", conf.Port)
if err = http.ListenAndServe(fmt.Sprintf(":%d", conf.Port), nil); err != nil {
return err
}
return nil
return http_server.StartServer(app.New(conf, conn, logger))
}

View File

@ -33,9 +33,12 @@ func LoadConfig(defaultConfig string, defaultPort int) (*Config, error) {
return setDefaultValue(&config, defaultPort), nil
}
func setDefaultValue(config *Config, defaultPort int) *Config {
if config.Port == 0 {
config.Port = defaultPort
func setDefaultValue(conf *Config, defaultPort int) *Config {
if conf.Port == 0 {
conf.Port = defaultPort
}
return config
if conf.LogFile == "" {
conf.LogFile = "/var/log/pink_fox/app.log"
}
return conf
}

View File

@ -8,7 +8,7 @@ import (
type Logger struct {
file *os.File
Logger *slog.Logger
logger *slog.Logger
}
func NewLogger(path string) (*Logger, error) {
@ -21,7 +21,7 @@ func NewLogger(path string) (*Logger, error) {
return &Logger{
file: file,
Logger: logger,
logger: logger,
}, nil
}

View File

@ -0,0 +1,23 @@
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!")
}

View File

@ -0,0 +1,17 @@
package http
import (
"github.com/gin-gonic/gin"
)
type ResponseFactory struct {
ctx *gin.Context
}
func NewResponseFactory(ctx *gin.Context) *ResponseFactory {
return &ResponseFactory{ctx: ctx}
}
func (it *ResponseFactory) String(s string) Response {
return NewStringResponse(s, it.ctx)
}

View File

@ -0,0 +1,23 @@
package http
import (
"github.com/gin-gonic/gin"
"net/http"
)
type Response interface {
Render()
}
type StringResponse struct {
str string
ctx *gin.Context
}
func NewStringResponse(s string, ctx *gin.Context) *StringResponse {
return &StringResponse{str: s, ctx: ctx}
}
func (it *StringResponse) Render() {
it.ctx.String(http.StatusOK, it.str)
}

View File

@ -0,0 +1,115 @@
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)
}
}

View File

@ -0,0 +1,16 @@
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()
}

View File

@ -0,0 +1,27 @@
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
}

View File

@ -0,0 +1,26 @@
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)
}

View File

@ -0,0 +1,11 @@
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

View File

@ -0,0 +1,65 @@
package lerror
import (
"fmt"
"os"
"path/filepath"
"runtime"
)
var (
lenBasePath int = 0
)
func init() {
path, err := os.Executable()
if err != nil {
path = ""
}
lenBasePath = len(filepath.Dir(path))
}
type Error struct {
msg string
trace []string
}
func NewString(msg string) *Error {
return &Error{
msg: msg,
trace: []string{trace()},
}
}
func NewError(err error) *Error {
return &Error{
msg: err.Error(),
trace: []string{trace()},
}
}
func (it *Error) Add(msg string) *Error {
it.trace = append(it.trace, trace())
it.msg = msg + ": " + it.msg
return it
}
func (it *Error) String() string {
result := it.msg
for _, line := range it.trace {
result += "\n" + line
}
return result
}
func trace() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return "not possible to recover the information"
}
if lenBasePath < len(file) {
return fmt.Sprintf("%s:%d", file[lenBasePath:], line)
} else {
return fmt.Sprintf("%s:%d", file, line)
}
}