Compare commits
No commits in common. "08739831d327d65ff4d97666ef3b6fac74d62755" and "6cae096a9b6ec023d9628c93f59b4c55ddfb7acb" have entirely different histories.
08739831d3
...
6cae096a9b
7
go.mod
7
go.mod
|
@ -1,11 +1,8 @@
|
||||||
module kumoly.io/lib/kmux
|
module kumoly.io/lib/ksrv
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require (
|
require kumoly.io/core/log v0.1.7
|
||||||
github.com/gorilla/mux v1.8.0
|
|
||||||
kumoly.io/core/log v0.1.7
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,3 @@
|
||||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
|
||||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
|
||||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package kmux
|
package ksrv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"kumoly.io/core/log"
|
"kumoly.io/core/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,31 +14,17 @@ const (
|
||||||
`{{if .Stack}}{{.Stack|redl}}{{end}}`
|
`{{if .Stack}}{{.Stack|redl}}{{end}}`
|
||||||
)
|
)
|
||||||
|
|
||||||
type kmux struct {
|
func (k *kserver) SetLogger(l *log.Logger) {
|
||||||
mux.Router
|
|
||||||
l *log.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *kmux) SetLogger(l *log.Logger) {
|
|
||||||
k.l = l
|
k.l = l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kmux) GetLogger() *log.Logger {
|
func (k *kserver) GetLogger() *log.Logger {
|
||||||
return k.l
|
return k.l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kmux) Server() *kserver {
|
// New returns a extended http.Server
|
||||||
return &kserver{
|
func New() *kserver {
|
||||||
http.Server{
|
l := log.Sub("KSRV")
|
||||||
Handler: k.middleware(k),
|
|
||||||
Addr: "0.0.0.0:8080",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRouter returns a extended gorilla mux
|
|
||||||
func NewRouter() *kmux {
|
|
||||||
l := log.Sub("KMUX")
|
|
||||||
tmpl := log.NewLogFormater()
|
tmpl := log.NewLogFormater()
|
||||||
tmpl.InfoTmplStr = DEFAULT_INFO
|
tmpl.InfoTmplStr = DEFAULT_INFO
|
||||||
tmpl.ErrTmplStr = DEFAULT_ERR
|
tmpl.ErrTmplStr = DEFAULT_ERR
|
||||||
|
@ -82,11 +67,12 @@ func NewRouter() *kmux {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
k := &kmux{*mux.NewRouter(), l}
|
k := &kserver{}
|
||||||
|
k.l = l
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kmux) middleware(next http.Handler) http.Handler {
|
func (k *kserver) Middleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
rw := &responseWriter{w, 0, ""}
|
rw := &responseWriter{w, 0, ""}
|
||||||
|
@ -96,7 +82,7 @@ func (k *kmux) middleware(next http.Handler) http.Handler {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kmux) catch(rw *responseWriter, r *http.Request) {
|
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
|
||||||
ex := recover()
|
ex := recover()
|
||||||
if ex != nil {
|
if ex != nil {
|
||||||
Abort(rw, ex)
|
Abort(rw, ex)
|
11
server.go
11
server.go
|
@ -1,11 +1,14 @@
|
||||||
package kmux
|
package ksrv
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"kumoly.io/core/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type kserver struct {
|
type kserver struct {
|
||||||
http.Server
|
http.Server
|
||||||
|
l *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Listen to addr
|
// Listen to addr
|
||||||
|
@ -20,5 +23,11 @@ func (s *kserver) Serve() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *kserver) ListenAndServe() error {
|
func (s *kserver) ListenAndServe() error {
|
||||||
|
s.Handler = s.Middleware(s.Handler)
|
||||||
return s.Server.ListenAndServe()
|
return s.Server.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *kserver) ListenAndServeTLS(certFile string, keyFile string) error {
|
||||||
|
s.Handler = s.Middleware(s.Handler)
|
||||||
|
return s.Server.ListenAndServeTLS(certFile, keyFile)
|
||||||
|
}
|
||||||
|
|
|
@ -6,18 +6,19 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"kumoly.io/core/log"
|
"kumoly.io/core/log"
|
||||||
"kumoly.io/lib/kmux"
|
"kumoly.io/lib/ksrv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.PROD = false
|
log.PROD = false
|
||||||
mux := kmux.NewRouter()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("ok")) })
|
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) { rw.Write([]byte("ok")) })
|
||||||
mux.HandleFunc("/err", func(rw http.ResponseWriter, r *http.Request) { kmux.Abort(rw, errors.New("small err")) })
|
mux.HandleFunc("/err", func(rw http.ResponseWriter, r *http.Request) { ksrv.Abort(rw, errors.New("small err")) })
|
||||||
mux.HandleFunc("/panic", func(rw http.ResponseWriter, r *http.Request) { panic(500) })
|
mux.HandleFunc("/panic", func(rw http.ResponseWriter, r *http.Request) { panic(500) })
|
||||||
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
mux.HandleFunc("/out", func(rw http.ResponseWriter, r *http.Request) { arr := []int{0, 1}; fmt.Print(arr[9]) })
|
||||||
log.Info("start")
|
log.Info("start")
|
||||||
err := mux.Server().Listen("0.0.0.0:8081").Serve()
|
|
||||||
|
err := ksrv.New().Listen("0.0.0.0:8081").Serve()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue