|
|
|
@@ -3,8 +3,10 @@ package ginext
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/mathext"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
@@ -65,16 +67,47 @@ func NewEngine(allowCors bool, ginDebug bool, timeout time.Duration) *GinWrapper
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
if w.ginDebug {
|
|
|
|
|
w.debugPrintRoutes()
|
|
|
|
|
w.DebugPrintRoutes()
|
|
|
|
|
|
|
|
|
|
httpserver := &http.Server{
|
|
|
|
|
Addr: addr,
|
|
|
|
|
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) // the net.Listener a few lines above is at this point actually already buffering requests
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
errChan <- httpserver.Serve(ln)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return errChan, httpserver
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinWrapper) debugPrintRoutes() {
|
|
|
|
|
func (w *GinWrapper) DebugPrintRoutes() {
|
|
|
|
|
if !w.ginDebug {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lines := make([][4]string, 0)
|
|
|
|
|
|
|
|
|
@@ -99,7 +132,7 @@ func (w *GinWrapper) debugPrintRoutes() {
|
|
|
|
|
|
|
|
|
|
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[1], " ", pad[1]),
|
|
|
|
|
langext.StrPadRight(line[2], " ", pad[2]),
|
|
|
|
|