Compare commits
No commits in common. "6cae096a9b6ec023d9628c93f59b4c55ddfb7acb" and "08739831d327d65ff4d97666ef3b6fac74d62755" have entirely different histories.
6cae096a9b
...
08739831d3
7
go.mod
7
go.mod
|
@ -1,8 +1,11 @@
|
||||||
module kumoly.io/lib/ksrv
|
module kumoly.io/lib/kmux
|
||||||
|
|
||||||
go 1.17
|
go 1.17
|
||||||
|
|
||||||
require kumoly.io/core/log v0.1.7
|
require (
|
||||||
|
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,3 +1,5 @@
|
||||||
|
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,8 +1,9 @@
|
||||||
package ksrv
|
package kmux
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"kumoly.io/core/log"
|
"kumoly.io/core/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,17 +15,31 @@ const (
|
||||||
`{{if .Stack}}{{.Stack|redl}}{{end}}`
|
`{{if .Stack}}{{.Stack|redl}}{{end}}`
|
||||||
)
|
)
|
||||||
|
|
||||||
func (k *kserver) SetLogger(l *log.Logger) {
|
type kmux struct {
|
||||||
|
mux.Router
|
||||||
|
l *log.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *kmux) SetLogger(l *log.Logger) {
|
||||||
k.l = l
|
k.l = l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kserver) GetLogger() *log.Logger {
|
func (k *kmux) GetLogger() *log.Logger {
|
||||||
return k.l
|
return k.l
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a extended http.Server
|
func (k *kmux) Server() *kserver {
|
||||||
func New() *kserver {
|
return &kserver{
|
||||||
l := log.Sub("KSRV")
|
http.Server{
|
||||||
|
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
|
||||||
|
@ -67,12 +82,11 @@ func New() *kserver {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
k := &kserver{}
|
k := &kmux{*mux.NewRouter(), l}
|
||||||
k.l = l
|
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kserver) Middleware(next http.Handler) http.Handler {
|
func (k *kmux) 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, ""}
|
||||||
|
@ -82,7 +96,7 @@ func (k *kserver) Middleware(next http.Handler) http.Handler {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *kserver) catch(rw *responseWriter, r *http.Request) {
|
func (k *kmux) 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,14 +1,11 @@
|
||||||
package ksrv
|
package kmux
|
||||||
|
|
||||||
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
|
||||||
|
@ -23,11 +20,5 @@ 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,19 +6,18 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"kumoly.io/core/log"
|
"kumoly.io/core/log"
|
||||||
"kumoly.io/lib/ksrv"
|
"kumoly.io/lib/kmux"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.PROD = false
|
log.PROD = false
|
||||||
mux := http.NewServeMux()
|
mux := kmux.NewRouter()
|
||||||
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) { ksrv.Abort(rw, errors.New("small err")) })
|
mux.HandleFunc("/err", func(rw http.ResponseWriter, r *http.Request) { kmux.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