Skip to content

muxplus基于mux实现了http直接调用任意函数的功能

Notifications You must be signed in to change notification settings

metooweb/muxplus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

muxplus

muxplus基于mux实现了http直接调用任意函数的功能

example

type OptionHello struct {
	Name string `json:"name" form:"name"`
}

type ResHello struct {
	Msg string
}

func Hello(ctx context.Context, options *OptionHello) (res *ResHello, err error) {

	res = &ResHello{}

	res.Msg = "hello world " + options.Name

	return
}

func ResponseHandler(next muxplus.Handler) muxplus.Handler {

	return muxplus.HandlerFunc(func(w http.ResponseWriter, req *http.Request, fv *muxplus.FuncVal) {

		next.Deal(w, req, fv)

		if resErr := fv.Outs[ 1 ]; !resErr.IsNil() {

			http.Error(w, resErr.String(), http.StatusInternalServerError)

		} else {
			
			if err := json.NewEncoder(w).Encode(fv.Outs[0].Interface()); err != nil {
				panic(err)
			}

		}

	})

}

func main() {

	handler := muxplus.DefaultArgsParseHandler(
		ResponseHandler(
			muxplus.DefaultServerHandler{},
		),
	)

	router := mux.NewRouter()

	muxplus.HandleFuncPlus(router, "/", Hello, handler)

	http.ListenAndServe(":8081", router)
}

About

muxplus基于mux实现了http直接调用任意函数的功能

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages