Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tarsgo 好像不能使用 http middleware? #100

Closed
Clark-zhang opened this issue Feb 13, 2019 · 4 comments
Closed

tarsgo 好像不能使用 http middleware? #100

Clark-zhang opened this issue Feb 13, 2019 · 4 comments
Assignees

Comments

@Clark-zhang
Copy link
Contributor

Clark-zhang commented Feb 13, 2019

tars.AddHttpServant() 的第一个参数是 TarsHttpMux struct
所以好像不能使用像 Alice - https://github.com/justinas/alice
或者 gorilla - https://github.com/gorilla/mux
这种middleware?

能改成interface吗?这样用户可以定制自己的TarsHttpMux来使用 自己想要的middleware?

@Clark-zhang Clark-zhang changed the title middleware tarsgo 好像不能使用 http middleware? Feb 13, 2019
@defool
Copy link
Collaborator

defool commented Feb 15, 2019

可以用,下面是个例子:

package main

import (
	"fmt"
	"net/http"

	"github.com/TarsCloud/TarsGo/tars"
	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "Home")
	})
	r.HandleFunc("/products/{key}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "product: %v\n", vars["key"])
	})

	cfg := tars.GetServerConfig()
	tarsMux := tars.TarsHttpMux{}
	tarsMux.Handle("/", r)
	tars.AddHttpServant(&tarsMux, cfg.App+"."+cfg.Server+".MainObjObj")

	tars.Run()
}

@Clark-zhang
Copy link
Contributor Author

可以用,下面是个例子:

package main

import (
	"fmt"
	"net/http"

	"github.com/TarsCloud/TarsGo/tars"
	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "Home")
	})
	r.HandleFunc("/products/{key}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, "product: %v\n", vars["key"])
	})

	cfg := tars.GetServerConfig()
	tarsMux := tars.TarsHttpMux{}
	tarsMux.Handle("/", r)
	tars.AddHttpServant(&tarsMux, cfg.App+"."+cfg.Server+".MainObjObj")

	tars.Run()
}

好的,感谢 🙏

@defool defool closed this as completed Feb 17, 2019
@Clark-zhang
Copy link
Contributor Author

Clark-zhang commented Mar 8, 2019

Hi, @defool @sandyskies

这种方式虽然可以使用中间件,但是目前tars监控上,所有的 “接口名”,都变成了 /。
可能是因为tarsHttp里只注册了一个 / 导致的?

能帮忙看下吗?

@Clark-zhang
Copy link
Contributor Author

Hi, @defool @sandyskies

这种方式虽然可以使用中间件,但是目前tars监控上,所有的 “接口名”,都变成了 /。
可能是因为tarsHttp里只注册了一个 / 导致的?

能帮忙看下吗?

可以把注册在gorilla上的 route,再多注册一次到 TarsHtppMux上就可以部分解决该问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants