34 lines
762 B
Markdown
34 lines
762 B
Markdown
|
# ksrv
|
||
|
|
||
|
A extended http.Server with logging and panic recovery
|
||
|
|
||
|
## Examples
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
|
"kumoly.io/core/log"
|
||
|
"kumoly.io/lib/ksrv"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
log.PROD = false
|
||
|
mux := http.NewServeMux()
|
||
|
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("/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]) })
|
||
|
log.Info("start")
|
||
|
|
||
|
err := ksrv.New().Listen("0.0.0.0:8080").Serve()
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
```
|