From 91dea6d44464d2bc11682def05ba1250f203a20c Mon Sep 17 00:00:00 2001 From: Zeev Diukman Date: Sat, 24 Aug 2024 16:12:40 +0300 Subject: [PATCH] helpers and FX --- app/go.mod | 20 ++++++ app/go.sum | 41 +++++++++++ app/helpers/helpers.go | 22 ++++++ app/main.go | 160 +++++++++++++---------------------------- app/main.go.bkup1 | 138 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 82 ++++++++++----------- 6 files changed, 310 insertions(+), 153 deletions(-) create mode 100644 app/go.sum create mode 100644 app/helpers/helpers.go create mode 100644 app/main.go.bkup1 diff --git a/app/go.mod b/app/go.mod index a905de5..c9544e5 100644 --- a/app/go.mod +++ b/app/go.mod @@ -1,3 +1,23 @@ module github.com/zeevdiukman/zprox go 1.23.0 + +require go.uber.org/fx v1.22.2 + +require ( + github.com/gookit/color v1.5.4 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/text v0.16.0 // indirect +) + +require ( + github.com/fatih/color v1.17.0 + github.com/gookit/goutil v0.6.16 + go.uber.org/dig v1.18.0 // indirect + go.uber.org/multierr v1.10.0 // indirect + go.uber.org/zap v1.26.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/term v0.23.0 +) diff --git a/app/go.sum b/app/go.sum new file mode 100644 index 0000000..34a6d1f --- /dev/null +++ b/app/go.sum @@ -0,0 +1,41 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= +github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= +github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= +github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= +github.com/gookit/goutil v0.6.16 h1:9fRMCF4X9abdRD5+2HhBS/GwafjBlTUBjRtA5dgkvuw= +github.com/gookit/goutil v0.6.16/go.mod h1:op2q8AoPDFSiY2+qkHxcBWQMYxOLQ1GbLXqe7vrwscI= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw= +go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE= +go.uber.org/fx v1.22.2 h1:iPW+OPxv0G8w75OemJ1RAnTUrF55zOJlXlo1TbJ0Buw= +go.uber.org/fx v1.22.2/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= +go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0= +golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU= +golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/app/helpers/helpers.go b/app/helpers/helpers.go new file mode 100644 index 0000000..718f9ce --- /dev/null +++ b/app/helpers/helpers.go @@ -0,0 +1,22 @@ +package helpers + +import ( + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/gookit/goutil/dump" + "golang.org/x/term" +) + +func Lines(v ...any) { + w, _, _ := term.GetSize(0) + line := strings.Repeat(">", w-10) + color.HiYellow(line) + fmt.Println("") + for _, a := range v { + dump.Println(a) + } + fmt.Println("") + color.HiYellow(line) +} diff --git a/app/main.go b/app/main.go index a5066b5..da018a2 100644 --- a/app/main.go +++ b/app/main.go @@ -3,12 +3,11 @@ package main import ( "context" "fmt" - "log" + "net" "net/http" - "os" - "os/signal" - "syscall" - "time" + + "github.com/zeevdiukman/zprox/helpers" + "go.uber.org/fx" ) const ( @@ -19,117 +18,54 @@ const ( func init() { fmt.Println("ZPROX started") } +func StartNewServer(lc fx.Lifecycle) *http.Server { + s := &http.Server{ + Addr: ":8080", + } + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + ln, err := net.Listen("tcp", s.Addr) + if err != nil { + return err + } -// type Matcher struct { -// r *http.Request -// w http.ResponseWriter -// } - -// func (m *Matcher) MatchHost(str string, f ...func(w http.ResponseWriter, r *http.Request)) { -// if m.r.Host == "test.gotest.com" || m.r.Host == "localhost" { -// m.w.Write([]byte("test")) -// } else { -// m.w.Write([]byte("404 page not found")) -// m.w.WriteHeader(http.StatusNotFound) -// } -// } - -type HandlerFunc func(http.ResponseWriter, *http.Request) - -// func (h *HandlerFunc) Handle(f func(w http.ResponseWriter, r *http.Request)) { -// h = h.HandlerFunc() - -// // &h.f() -// // if r.Host == "test.gotest.com" { -// // w.Write([]byte("test")) -// // } else { -// // w.Write([]byte("404 page not found")) - -// // w.WriteHeader(http.StatusNotFound) -// // } -// } - -// func test(w http.ResponseWriter, r *http.Request) { -// if r.Host == "test.gotest.com" { -// w.Write([]byte("login")) -// } else { -// w.Write([]byte("404 page not found")) -// w.WriteHeader(http.StatusNotFound) -// } -// } - -// func handle(f HandlerFunc) HandlerFunc { -// HandlerFunc{}.f[0] -// return func(w http.ResponseWriter, r *http.Request) { -// f -// } -// } - -// type Mux struct { -// *Handlers -// *Router -// } -// type Handlers []struct{ http.Handler } -// type Router struct{ *http.ServeMux } - -// func NewMux() *Mux { -// r := new(Router) -// h := new(Handlers) -// return &Mux{Handlers:h,Router:r} -// } - -// func (m *Handlers) Add() { -// h := new(Handlers) -// m = &Mux{h, r} -// &Handlers{f} -// } -type Router struct{ http.ServeMux } - -func (r Router) Host(string) { - - // router.HandleFunc("") + helpers.Lines("Starting HTTP server at", s.Addr) + go s.ServeTLS(ln, sslCert, sslKey) + return nil + }, + OnStop: func(ctx context.Context) error { + return s.Shutdown(ctx) + }, + }) + return s } +func StartNewRouter(lc fx.Lifecycle) *http.ServeMux { + r := http.NewServeMux() + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + fmt.Println("HTTP router started.") + return nil + }, + OnStop: func(ctx context.Context) error { + fmt.Println("HTTP router stoped.") + return nil + }, + }) + return r +} + +// func GetContructors(lc fx.Lifecycle) any { +// constructors := make(...interface{}, 0) +// constructors = append(constructors, StartNewServer, StartNewRouter) +// return constructors +// } func main() { + + fx.New( + fx.Provide(StartNewServer, StartNewRouter), + fx.Invoke(func(*http.Server) {}), + ).Run() // var h HandlerFunc // h:=func(a HandlerFunc)HandlerFunc{ - router := http.NewServeMux() - router.H - // h := Handler.Add(func(w http.ResponseWriter, r *http.Request) {}) - // http.Handler.ServeHTTP() - - router.HandleFunc("test.gotest.com/", h) - // router.HandleFunc("login.gotest.com/", login) - - srv := &http.Server{ - Addr: ":8443", - Handler: router, - } - - go func() { - if err := srv.ListenAndServeTLS(sslCert, sslKey); err != nil && err != http.ErrServerClosed { - log.Fatalf("Failed to initialize server: %v\n", err) - } - }() - - log.Printf("Listening on port %v\n", srv.Addr) - - // Wait for kill signal of channel - quit := make(chan os.Signal, 1) - - signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) - - // This blocks until a signal is passed into the quit channel - <-quit - - // The context is used to inform the server it has 5 seconds to finish - // the request it is currently handling - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - // Shutdown server - log.Println("\nShutting down server...") - if err := srv.Shutdown(ctx); err != nil { - log.Fatalf("Server forced to shutdown: %v\n", err) - } } diff --git a/app/main.go.bkup1 b/app/main.go.bkup1 new file mode 100644 index 0000000..c7554eb --- /dev/null +++ b/app/main.go.bkup1 @@ -0,0 +1,138 @@ +package main + +import ( + "context" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "go.uber.org/fx" +) + +const ( + sslCert = "./certs/ssl.crt" + sslKey = "./certs/ssl.key" +) + +func init() { + fmt.Println("ZPROX started") +} + +// type Matcher struct { +// r *http.Request +// w http.ResponseWriter +// } + +// func (m *Matcher) MatchHost(str string, f ...func(w http.ResponseWriter, r *http.Request)) { +// if m.r.Host == "test.gotest.com" || m.r.Host == "localhost" { +// m.w.Write([]byte("test")) +// } else { +// m.w.Write([]byte("404 page not found")) +// m.w.WriteHeader(http.StatusNotFound) +// } +// } + +type HandlerFunc func(http.ResponseWriter, *http.Request) + +// func (h *HandlerFunc) Handle(f func(w http.ResponseWriter, r *http.Request)) { +// h = h.HandlerFunc() + +// // &h.f() +// // if r.Host == "test.gotest.com" { +// // w.Write([]byte("test")) +// // } else { +// // w.Write([]byte("404 page not found")) + +// // w.WriteHeader(http.StatusNotFound) +// // } +// } + +// func test(w http.ResponseWriter, r *http.Request) { +// if r.Host == "test.gotest.com" { +// w.Write([]byte("login")) +// } else { +// w.Write([]byte("404 page not found")) +// w.WriteHeader(http.StatusNotFound) +// } +// } + +// func handle(f HandlerFunc) HandlerFunc { +// HandlerFunc{}.f[0] +// return func(w http.ResponseWriter, r *http.Request) { +// f +// } +// } + +// type Mux struct { +// *Handlers +// *Router +// } +// type Handlers []struct{ http.Handler } +// type Router struct{ *http.ServeMux } + +// func NewMux() *Mux { +// r := new(Router) +// h := new(Handlers) +// return &Mux{Handlers:h,Router:r} +// } + +// func (m *Handlers) Add() { +// h := new(Handlers) +// m = &Mux{h, r} +// &Handlers{f} +// } +type Router struct{ http.ServeMux } + +func (r Router) Host(string) { + + // router.HandleFunc("") +} +func main() { + app := fx.New() + app.Run() + // var h HandlerFunc + // h:=func(a HandlerFunc)HandlerFunc{ + + router := http.NewServeMux() + // h := Handler.Add(func(w http.ResponseWriter, r *http.Request) {}) + // http.Handler.ServeHTTP() + + router.HandleFunc("test.gotest.com/", h) + // router.HandleFunc("login.gotest.com/", login) + + srv := &http.Server{ + Addr: ":8443", + Handler: router, + } + + go func() { + if err := srv.ListenAndServeTLS(sslCert, sslKey); err != nil && err != http.ErrServerClosed { + log.Fatalf("Failed to initialize server: %v\n", err) + } + }() + + log.Printf("Listening on port %v\n", srv.Addr) + + // Wait for kill signal of channel + quit := make(chan os.Signal, 1) + + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + + // This blocks until a signal is passed into the quit channel + <-quit + + // The context is used to inform the server it has 5 seconds to finish + // the request it is currently handling + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // Shutdown server + log.Println("\nShutting down server...") + if err := srv.Shutdown(ctx); err != nil { + log.Fatalf("Server forced to shutdown: %v\n", err) + } +} diff --git a/docker-compose.yml b/docker-compose.yml index 5ee753d..3b5a1c2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,47 +14,47 @@ services: # have to use $$ (double-dollar) so docker doesn't try to substitute a variable command: reflex -r "\.go$$" -s -- sh -c "go run ./" - postgres: - image: postgres:latest - restart: always - environment: - - POSTGRES_USER=keycloak - - POSTGRES_DB=keycloak - - POSTGRES_PASSWORD=keycloak - volumes: - - './docker/keycloak/postgres:/var/lib/postgresql/data' - networks: - postgres: - keycloak: - depends_on: - - postgres - image: quay.io/keycloak/keycloak:latest - restart: unless-stopped - command: start - ports: - - 4000:8080 - networks: - postgres: - environment: - - KC_PROXY_ADDRESS_FORWARDING=true - - KC_HOSTNAME_STRICT=false - - KC_HOSTNAME=keycloak.test.com - # - KC_HOSTNAME_URL=10.0.0.2:4000 - # - KC_HOSTNAME_ADMIN_URL=keycloak.diukman.com - # - KC_PROXY=edge - - KC_HTTP_ENABLED=true - - KC_DB=postgres - - KC_DB_USERNAME=keycloak - - KC_DB_PASSWORD=keycloak - - KC_DB_URL_HOST=postgres - - KC_DB_URL_PORT=5432 - - KC_DB_URL_DATABASE=keycloak - - KEYCLOAK_ADMIN=admin - - KEYCLOAK_ADMIN_PASSWORD=admin - labels: - - "quarkus.transaction-manager.enable-recovery=true" -networks: - postgres: +# postgres: +# image: postgres:latest +# restart: always +# environment: +# - POSTGRES_USER=keycloak +# - POSTGRES_DB=keycloak +# - POSTGRES_PASSWORD=keycloak +# volumes: +# - './docker/keycloak/postgres:/var/lib/postgresql/data' +# networks: +# postgres: +# keycloak: +# depends_on: +# - postgres +# image: quay.io/keycloak/keycloak:latest +# restart: unless-stopped +# command: start +# ports: +# - 4000:8080 +# networks: +# postgres: +# environment: +# - KC_PROXY_ADDRESS_FORWARDING=true +# - KC_HOSTNAME_STRICT=false +# - KC_HOSTNAME=keycloak.test.com +# # - KC_HOSTNAME_URL=10.0.0.2:4000 +# # - KC_HOSTNAME_ADMIN_URL=keycloak.diukman.com +# # - KC_PROXY=edge +# - KC_HTTP_ENABLED=true +# - KC_DB=postgres +# - KC_DB_USERNAME=keycloak +# - KC_DB_PASSWORD=keycloak +# - KC_DB_URL_HOST=postgres +# - KC_DB_URL_PORT=5432 +# - KC_DB_URL_DATABASE=keycloak +# - KEYCLOAK_ADMIN=admin +# - KEYCLOAK_ADMIN_PASSWORD=admin +# labels: +# - "quarkus.transaction-manager.enable-recovery=true" +# networks: +# postgres: # networks: # - host \ No newline at end of file