How to add a sub router? #1170

Closed
opened 2025-11-20 05:25:20 -05:00 by saavagebueno · 0 comments
Owner

Originally created by @BJorah on GitHub (Aug 22, 2024).

I want add a test router which without using the middleware , I add the code bellow in file netbird-0.28.6/management/server/http/handler.go

func APIHandler(ctx context.Context, accountManager s.AccountManager, LocationManager *geolocation.Geolocation, jwtValidator jwtclaims.JWTValidator, appMetrics telemetry.AppMetrics, authCfg AuthCfg, integratedValidator integrated_validator.IntegratedValidator) (http.Handler, error) {
//other code
.....

      noAuthRouter := rootRouter.PathPrefix("/test").Subrouter()
	      log.Println("working 2...")
	      noAuthRouter.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		      log.Println("responding helloworld...........")
		      w.Header().Set("Content-Type", "application/json")
		      response := map[string]string{
			      "test": "helloworld",
		      }
		      json.NewEncoder(w).Encode(response)
	      }).Methods("GET")

//other code
.....


}

and make a small change here:

func (apiHandler *apiHandler) addPeersEndpoint() {
	log.Println("working 3...")
	peersHandler := NewPeersHandler(apiHandler.AccountManager, apiHandler.AuthCfg)
       // it used to be "apiHandler.Router.HandleFunc("/peers", peersHandler.GetAllPeers).Methods("GET", "OPTIONS")"
	apiHandler.Router.HandleFunc("/hello222", peersHandler.GetAllPeers).Methods("GET", "OPTIONS")
	apiHandler.Router.HandleFunc("/peers/{peerId}", peersHandler.HandlePeer).
		Methods("GET", "PUT", "DELETE", "OPTIONS")
}

I only modified the above two places.
I rebuild it and I run this project locally.
I use postman send GET quests 127.0.0.1:80/test/hello 127.0.0.1:80/api/hello222, and they both reported 404 errors and with no logs were printed.
What is surprising is that the requet 127.0.0.1:80/api/peers is still working like it used to be !!?
I think that I must missing something important or making something wrong.
Would you like to tell me how to make it?

Originally created by @BJorah on GitHub (Aug 22, 2024). I want add a test router which without using the middleware , I add the code bellow in file netbird-0.28.6/management/server/http/handler.go ``` func APIHandler(ctx context.Context, accountManager s.AccountManager, LocationManager *geolocation.Geolocation, jwtValidator jwtclaims.JWTValidator, appMetrics telemetry.AppMetrics, authCfg AuthCfg, integratedValidator integrated_validator.IntegratedValidator) (http.Handler, error) { //other code ..... noAuthRouter := rootRouter.PathPrefix("/test").Subrouter() log.Println("working 2...") noAuthRouter.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { log.Println("responding helloworld...........") w.Header().Set("Content-Type", "application/json") response := map[string]string{ "test": "helloworld", } json.NewEncoder(w).Encode(response) }).Methods("GET") //other code ..... } ``` and make a small change here: ``` func (apiHandler *apiHandler) addPeersEndpoint() { log.Println("working 3...") peersHandler := NewPeersHandler(apiHandler.AccountManager, apiHandler.AuthCfg) // it used to be "apiHandler.Router.HandleFunc("/peers", peersHandler.GetAllPeers).Methods("GET", "OPTIONS")" apiHandler.Router.HandleFunc("/hello222", peersHandler.GetAllPeers).Methods("GET", "OPTIONS") apiHandler.Router.HandleFunc("/peers/{peerId}", peersHandler.HandlePeer). Methods("GET", "PUT", "DELETE", "OPTIONS") } ``` I only modified the above two places. I rebuild it and I run this project locally. I use postman send GET quests `127.0.0.1:80/test/hello` `127.0.0.1:80/api/hello222`, and they both reported 404 errors and with no logs were printed. What is surprising is that the requet `127.0.0.1:80/api/peers` is still working like it used to be !!? I think that I must missing something important or making something wrong. Would you like to tell me how to make it?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: SVI/netbird#1170