Go to file
Evan Chen 0d61b343a2 add panic json 2021-11-12 14:09:11 +08:00
engine update 2021-11-04 12:00:31 +08:00
test refact: use the new klog 2021-11-05 23:31:55 +08:00
README.md feat: add handle shorthand 2021-11-04 02:09:00 +08:00
error.go add error template 2021-11-12 08:01:02 +08:00
go.mod update 2021-11-11 15:01:29 +08:00
go.sum update 2021-11-11 15:01:29 +08:00
kserver.go update 2021-11-11 15:01:29 +08:00
net.go add panic json 2021-11-12 14:09:11 +08:00
server.go build: use klog instead 2021-11-04 03:08:05 +08:00
util.go update 2021-11-04 01:33:00 +08:00

README.md

ksrv

A extended http.Server with logging and panic recovery

Examples

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().Handle(mux).Listen("0.0.0.0:8080").Serve()
	if err != nil {
		panic(err)
	}
}