當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ServeMux.HandleFunc方法代碼示例

本文整理匯總了Golang中net/http.ServeMux.HandleFunc方法的典型用法代碼示例。如果您正苦於以下問題:Golang ServeMux.HandleFunc方法的具體用法?Golang ServeMux.HandleFunc怎麽用?Golang ServeMux.HandleFunc使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在net/http.ServeMux的用法示例。


在下文中一共展示了ServeMux.HandleFunc方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: InitHTTPFlv

func InitHTTPFlv(mux *http.ServeMux, app string, sources *source.SourceManage, cb callback.FlvCallback) {
	prefix := path.Join("/", app) + "/"
	mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
		glog.Infof("http: get an request: %v", r.RequestURI)
		if r.Method != "GET" {
			return
		}
		r.ParseForm()
		// access check.
		if !cb.FlvAccessCheck(r.RemoteAddr, r.RequestURI, r.URL.Path, r.Form, r.Cookies()) {
			w.WriteHeader(http.StatusForbidden)
			return
		}
		// get path.
		_, file := path.Split(r.URL.Path)
		var key string
		key = file[:strings.Index(file, ".flv")]

		// get live source.
		consumer, err := source.NewConsumer(sources, key)
		if err != nil {
			glog.Info("can not get source", err)
			return
		}
		glog.Info("get source")
		defer consumer.Close()
		// set flv live stream http head.
		// TODO: let browser not cache sources.
		w.Header().Add("Content-Type", "video/x-flv")
		if err := consumer.Live(w); err != nil {
			glog.Info("Live get an client error:", err)
		}
	})
}
開發者ID:Alienero,項目名稱:IamServer,代碼行數:34,代碼來源:http.go

示例2: setupPlugin

func setupPlugin(t *testing.T, name string, mux *http.ServeMux) func() {
	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
		t.Fatal(err)
	}

	server := httptest.NewServer(mux)
	if server == nil {
		t.Fatal("Failed to start an HTTP Server")
	}

	if err := ioutil.WriteFile(fmt.Sprintf("/etc/docker/plugins/%s.spec", name), []byte(server.URL), 0644); err != nil {
		t.Fatal(err)
	}

	mux.HandleFunc("/Plugin.Activate", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/vnd.docker.plugins.v1+json")
		fmt.Fprintf(w, `{"Implements": ["%s"]}`, driverapi.NetworkPluginEndpointType)
	})

	return func() {
		if err := os.RemoveAll("/etc/docker/plugins"); err != nil {
			t.Fatal(err)
		}
		server.Close()
	}
}
開發者ID:YujiOshima,項目名稱:libnetwork,代碼行數:26,代碼來源:driver_test.go

示例3: handleSecurity

func handleSecurity(mux *http.ServeMux, sh *securityHandler) {
	mux.HandleFunc(securityPrefix+"/roles", sh.baseRoles)
	mux.HandleFunc(securityPrefix+"/roles/", sh.handleRoles)
	mux.HandleFunc(securityPrefix+"/users", sh.baseUsers)
	mux.HandleFunc(securityPrefix+"/users/", sh.handleUsers)
	mux.HandleFunc(securityPrefix+"/enable", sh.enableDisable)
}
開發者ID:rfrangio,項目名稱:etcd-starter,代碼行數:7,代碼來源:client_security.go

示例4: HandleFuncLoginOptional

func HandleFuncLoginOptional(
	mux *http.ServeMux, path string, s *Sessions, f HandlerFuncWithUser) {
	wrapped := func(w http.ResponseWriter, r *http.Request) {
		f(w, r, s.GetUser(r))
	}
	mux.HandleFunc(path, wrapped)
}
開發者ID:tstranex,項目名稱:carpcomm,代碼行數:7,代碼來源:sessions.go

示例5: staticDirHandler

func staticDirHandler(mux *http.ServeMux, prefix string, staticDir string) {
	mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
		file := staticDir + r.URL.Path[len(prefix)-1:]
		fmt.Println(file)
		http.ServeFile(w, r, file)
	})
}
開發者ID:sgp2004,項目名稱:WeiboLibrary,代碼行數:7,代碼來源:WeiboLibraryServer.go

示例6: Handler

func Handler(mux *http.ServeMux) {
	mux.HandleFunc("/debug", debug)
	mux.HandleFunc("/archive", archiveHandler)
	mux.HandleFunc("/delete", deleteHandler)
	mux.HandleFunc("/star", starHandler)
	mux.HandleFunc("/", serve)
}
開發者ID:chzyer,項目名稱:pocket,代碼行數:7,代碼來源:handler.go

示例7: handleAuth

func handleAuth(mux *http.ServeMux, sh *authHandler) {
	mux.HandleFunc(authPrefix+"/roles", capabilityHandler(authCapability, sh.baseRoles))
	mux.HandleFunc(authPrefix+"/roles/", capabilityHandler(authCapability, sh.handleRoles))
	mux.HandleFunc(authPrefix+"/users", capabilityHandler(authCapability, sh.baseUsers))
	mux.HandleFunc(authPrefix+"/users/", capabilityHandler(authCapability, sh.handleUsers))
	mux.HandleFunc(authPrefix+"/enable", capabilityHandler(authCapability, sh.enableDisable))
}
開發者ID:EricMountain-1A,項目名稱:openshift-origin,代碼行數:7,代碼來源:client_auth.go

示例8: RedirectHandler

func RedirectHandler(mux *http.ServeMux) {
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		req.URL.Host = req.Host
		req.URL.Scheme = "https"
		http.Redirect(w, req, req.URL.String(), 301)
	})
}
開發者ID:chzyer,項目名稱:pocket,代碼行數:7,代碼來源:handler.go

示例9: setMux

func setMux(t *testing.T, mux *http.ServeMux, path string, thing interface{}) {
	mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
		var data []byte
		var err error

		switch thing := thing.(type) {
		default:
			t.Errorf("Unexpected object type in SetMux: %v", thing)
		case *github.Issue:
			data, err = json.Marshal(thing)
		case *github.PullRequest:
			data, err = json.Marshal(thing)
		case []github.IssueEvent:
			data, err = json.Marshal(thing)
		case []github.RepositoryCommit:
			data, err = json.Marshal(thing)
		case github.RepositoryCommit:
			data, err = json.Marshal(thing)
		case *github.CombinedStatus:
			data, err = json.Marshal(thing)
		case []github.User:
			data, err = json.Marshal(thing)
		}
		if err != nil {
			t.Errorf("%v", err)
		}
		if r.Method != "GET" {
			t.Errorf("Unexpected method: expected: GET got: %s", r.Method)
		}
		w.WriteHeader(http.StatusOK)
		w.Write(data)
	})
}
開發者ID:krancour,項目名稱:kubernetes-contrib,代碼行數:33,代碼來源:github.go

示例10: HTTPTransport

// HTTPTransport creates an ingress bridge from the outside world to the passed
// server, by installing handlers for all the necessary RPCs to the passed mux.
func HTTPTransport(mux *http.ServeMux, s *Server) {
	mux.HandleFunc(IDPath, idHandler(s))
	mux.HandleFunc(AppendEntriesPath, appendEntriesHandler(s))
	mux.HandleFunc(RequestVotePath, requestVoteHandler(s))
	mux.HandleFunc(CommandPath, commandHandler(s))
	mux.HandleFunc(SetConfigurationPath, setConfigurationHandler(s))
}
開發者ID:0x434D53,項目名稱:raft,代碼行數:9,代碼來源:transport.go

示例11: handleItems

func handleItems(mux *http.ServeMux) {
	mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) {
		switch r.Method {
		case "POST":
			defer r.Body.Close()

			b, err := ioutil.ReadAll(r.Body)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			if len(b) == 0 {
				testutil.ResponseAPIError(w, 500, api.ResponseError{
					Type:    "fatal",
					Message: "empty body",
				})
				return
			}

			type Options struct {
				Tweet *bool `json:"tweet"`
				Gist  *bool `json:"gist"`
			}
			var options Options
			err = json.Unmarshal(b, &options)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			if options.Tweet == nil || options.Gist == nil {
				testutil.ResponseError(w, 500, errors.New("tweet or gist is required"))
				return
			}

			var post model.Post
			err = json.Unmarshal(b, &post)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			post.ID = "4bd431809afb1bb99e4f"
			post.URL = "https://qiita.com/yaotti/items/4bd431809afb1bb99e4f"
			post.CreatedAt = model.Time{Time: time.Date(2016, 2, 1, 12, 51, 42, 0, time.UTC)}
			post.UpdatedAt = post.CreatedAt
			b, err = json.Marshal(post)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}
			_, err = w.Write(b)
			if err != nil {
				testutil.ResponseError(w, 500, err)
				return
			}

		default:
			w.WriteHeader(405)
		}
	})
}
開發者ID:minodisk,項目名稱:qiitactl,代碼行數:60,代碼來源:post_test.go

示例12: InstallDebugHandlers

//TODO(jdef) we use a Locker to guard against concurrent task state changes, but it would be
//really, really nice to avoid doing this. Maybe someday the registry won't return data ptrs
//but plain structs instead.
func InstallDebugHandlers(reg Registry, mux *http.ServeMux) {
	mux.HandleFunc("/debug/registry/tasks", func(w http.ResponseWriter, r *http.Request) {
		//TODO(jdef) support filtering tasks based on status
		alltasks := reg.List(nil)
		io.WriteString(w, fmt.Sprintf("task_count=%d\n", len(alltasks)))
		for _, task := range alltasks {
			if err := func() (err error) {
				podName := task.Pod.Name
				podNamespace := task.Pod.Namespace
				offerId := ""
				if task.Offer != nil {
					offerId = task.Offer.Id()
				}
				_, err = io.WriteString(w, fmt.Sprintf("%v\t%v/%v\t%v\t%v\n", task.ID, podNamespace, podName, task.State, offerId))
				return
			}(); err != nil {
				log.Warningf("aborting debug handler: %v", err)
				break // stop listing on I/O errors
			}
		}
		if flusher, ok := w.(http.Flusher); ok {
			flusher.Flush()
		}
	})
}
開發者ID:CodeJuan,項目名稱:kubernetes,代碼行數:28,代碼來源:debug.go

示例13: RegesterHandlers

func (s *Service) RegesterHandlers(mux *http.ServeMux) error {
	mux.HandleFunc("/file/", func(w http.ResponseWriter, req *http.Request) {
		s.file(w, req)
	})
	mux.HandleFunc("/get/", func(w http.ResponseWriter, req *http.Request) {
		// o, _ := httputil.DumpRequest(req, false)
		// log.Println(string(o))
		if !s.auth.Auth(w, req) {
			log.Println("401 Unauthorized")
			return
		}
		s.get(w, req)
	})

	mux.HandleFunc("/upload/", func(w http.ResponseWriter, req *http.Request) {
		s.upload(w, req)
	})
	mux.HandleFunc("/put-auth", func(w http.ResponseWriter, req *http.Request) {
		if !s.auth.Auth(w, req) {
			log.Println("401 Unauthorized")
			return
		}
		s.putAuth(w, req)
	})

	mux.HandleFunc("/delete/", func(w http.ResponseWriter, req *http.Request) {
		if !s.auth.Auth(w, req) {
			log.Println("401 Unauthorized")
			return
		}
		s.delete(w, req)
	})
	return nil
}
開發者ID:hyperpro,項目名稱:SoftPracPj,代碼行數:34,代碼來源:vio.go

示例14: loadAPIEndpoints

// Set up default Tyk control API endpoints - these are global, so need to be added first
func loadAPIEndpoints(Muxer *http.ServeMux) {
	// set up main API handlers
	Muxer.HandleFunc("/tyk/keys/create", CheckIsAPIOwner(createKeyHandler))
	Muxer.HandleFunc("/tyk/keys/", CheckIsAPIOwner(keyHandler))
	Muxer.HandleFunc("/tyk/reload/", CheckIsAPIOwner(resetHandler))
	Muxer.HandleFunc("/tyk/oauth/clients/create", CheckIsAPIOwner(createOauthClient))
	Muxer.HandleFunc("/tyk/oauth/clients/", CheckIsAPIOwner(oAuthClientHandler))
}
開發者ID:rmg,項目名稱:tyk,代碼行數:9,代碼來源:main.go

示例15: Register

func Register(mux *http.ServeMux) {
	if mux == nil {
		mux = http.DefaultServeMux
	}
	mux.HandleFunc("/weechat", handleHome)
	mux.HandleFunc("/weechat/buflines", handleLines)
	mux.Handle("/weechat/ws", ws.Handler(handleWebsocket))
}
開發者ID:lyuyun,項目名稱:loggregator,代碼行數:8,代碼來源:weechat.go


注:本文中的net/http.ServeMux.HandleFunc方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。