This commit is contained in:
Mike Schwörer 2023-07-24 18:34:56 +02:00
parent 8ae9a0f107
commit d04ce18eb0
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 39 additions and 9 deletions

View File

@ -3,8 +3,10 @@ package ginext
import ( import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/mathext" "gogs.mikescher.com/BlackForestBytes/goext/mathext"
"net"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -65,16 +67,44 @@ func NewEngine(allowCors bool, ginDebug bool, timeout time.Duration) *GinWrapper
return wrapper return wrapper
} }
func (w *GinWrapper) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (w *GinWrapper) ListenAndServeHTTP(addr string, postInit func(port string)) (chan error, *http.Server) {
httpserver := &http.Server{
if w.ginDebug { Addr: addr,
w.debugPrintRoutes() Handler: w.engine,
} }
w.engine.ServeHTTP(writer, request) errChan := make(chan error)
go func() {
ln, err := net.Listen("tcp", httpserver.Addr)
if err != nil {
errChan <- err
return
}
_, port, err := net.SplitHostPort(ln.Addr().String())
if err != nil {
errChan <- err
return
}
log.Info().Str("address", httpserver.Addr).Msg("HTTP-Server started on http://localhost:" + port)
if postInit != nil {
postInit(port)
}
errChan <- httpserver.Serve(ln)
}()
return errChan, httpserver
} }
func (w *GinWrapper) debugPrintRoutes() { func (w *GinWrapper) DebugPrintRoutes() {
if !w.ginDebug {
return
}
lines := make([][4]string, 0) lines := make([][4]string, 0)
@ -99,7 +129,7 @@ func (w *GinWrapper) debugPrintRoutes() {
for _, line := range lines { for _, line := range lines {
fmt.Printf("Gin-Route: [%s] @ %s --> %s --> %s", fmt.Printf("Gin-Route: [%s] @ %s --> %s --> %s\n",
langext.StrPadRight(line[0], " ", pad[0]), langext.StrPadRight(line[0], " ", pad[0]),
langext.StrPadRight(line[1], " ", pad[1]), langext.StrPadRight(line[1], " ", pad[1]),
langext.StrPadRight(line[2], " ", pad[2]), langext.StrPadRight(line[2], " ", pad[2]),

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.201" const GoextVersion = "0.0.202"
const GoextVersionTimestamp = "2023-07-24T18:22:36+0200" const GoextVersionTimestamp = "2023-07-24T18:34:56+0200"