本文整理匯總了Golang中github.com/gorilla/pat.Router類的典型用法代碼示例。如果您正苦於以下問題:Golang Router類的具體用法?Golang Router怎麽用?Golang Router使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Router類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ptyExec
func ptyExec(test *testing.T, mux *pat.Router) {
mux.Post("/exec", func(res http.ResponseWriter, req *http.Request) {
test.Log("got exec")
conn, rw, err := res.(http.Hijacker).Hijack()
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(err.Error()))
return
}
defer conn.Close()
script := req.FormValue("cmd")
if script == "" {
test.Log("missing script")
test.FailNow()
}
test.Log("executing", script)
cmd := exec.Command("/bin/bash", "-c", script)
file, err := pty.Start(cmd)
if err != nil {
test.Log(err)
test.FailNow()
}
test.Log("copying from pty")
go io.Copy(file, rw)
io.Copy(conn, file)
test.Log("finished running")
})
}
示例2: normalExec
func normalExec(test *testing.T, mux *pat.Router) {
mux.Post("/exec", func(res http.ResponseWriter, req *http.Request) {
test.Log("got exec")
conn, rw, err := res.(http.Hijacker).Hijack()
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte(err.Error()))
return
}
defer conn.Close()
script := req.FormValue("cmd")
if script == "" {
test.Log("missing script")
test.FailNow()
}
test.Log("executing", script)
cmd := exec.Command("/bin/bash", "-c", script)
cmd.Stdin = rw
cmd.Stderr = conn
// cmd.Stdout = conn
// cmd.Run()
reader, err := cmd.StdoutPipe()
cmd.Start()
io.Copy(conn, reader)
conn.(*net.TCPConn).CloseWrite()
err = cmd.Wait()
test.Log("finished running")
})
}
示例3: Register
// Register creates routes for each static resource
func Register(config Config, r *pat.Router) {
//log.Debug("registering not found handler for static package", nil)
//r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// render.HTML(w, http.StatusNotFound, "error", render.DefaultVars(req, map[string]interface{}{"error": "Page not found"}))
//})
log.Printf("registering static content handlers for static package\n")
for _, file := range config.AssetNames()() {
if strings.HasPrefix(file, "static/") {
path := strings.TrimPrefix(file, "static")
log.Printf("registering handler for static asset %s\n", path)
b, err := config.Asset()("static" + path)
if err != nil {
panic(err)
}
mimeType := http.DetectContentType(b)
log.Printf("using mime type %s\n", mimeType)
r.Path(path).Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", mimeType)
w.Header().Set("Cache-control", "public, max-age=259200")
w.WriteHeader(200)
w.Write(b)
})
}
}
}
示例4: userAddRoutes
func userAddRoutes(router *pat.Router) {
router.Post("/user/waiver", serve(
Authenticate(true),
acceptWaiver,
toJson,
))
}
示例5: addTwilioRoutes
func addTwilioRoutes(router *pat.Router) {
router.Get("/twilio/call", serve(
parseFromUrl(urlParams{"From": parseParamToString}),
handleTwilio,
toXml,
))
}
示例6: CreateWeb
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
web := &Web{
config: cfg,
asset: asset,
}
pat.Path("/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
pat.Path("/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
pat.Path("/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
pat.Path("/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
pat.Path("/").Methods("GET").HandlerFunc(web.Index())
return web
}
示例7: addDashboardRoutes
func addDashboardRoutes(router *pat.Router) {
router.Get("/dashboard/progress", serve(
dashboardAuth,
getDashboardProgress,
toJson,
))
router.Get("/dashboard/clues", serve(
dashboardAuth,
getDashboardClues,
toJson,
))
router.Put("/dashboard/start_times", serve(
parseRequest(new(setStartTimesReq)),
dashboardAuth,
setStartTimes,
noResponse,
))
router.Put("/dashboard", serve(
parseRequest(new(_DashboardAuthReq)),
dashboardAuth,
))
}
示例8: teamAddRoutes
func teamAddRoutes(router *pat.Router) {
router.Post("/team/{team_id}/finish", serve(
dashboardAuth,
parseFromUrl(urlParams{
"team_id": parseParamToInt64,
}),
setTeamFinish,
toJson,
))
router.Delete("/team/{team_id}/finish", serve(
dashboardAuth,
parseFromUrl(urlParams{
"team_id": parseParamToInt64,
}),
setTeamFinish,
toJson,
))
router.Post("/team/{phrase}", serve(
parseFromUrl(urlParams{
"phrase": parseParamToString,
}),
parseRequest(new(_TeamJoinReq)),
validateJoinTeam,
joinTeam,
SetUserId,
toJson,
))
router.Post("/team", serve(
parseRequest(new(_TeamCreateReq)),
createTeam,
SetUserId,
toJson,
))
}
示例9: addClueRoutes
func addClueRoutes(router *pat.Router) {
router.Get("/clue/{clue_id}/hint", serve(
Authenticate(true),
parseFromUrl(urlParams{
"clue_id": parseParamToInt64,
"lat": parseParamToFloat64,
"lng": parseParamToFloat64,
}),
takeHint,
toJson,
))
router.Get("/clue", serve(
Authenticate(true),
parseFromUrl(urlParams{"clue_id": parseParamToInt64}),
currentClue,
toJson,
))
router.Post("/clue/{clue_id}", serve(
Authenticate(true),
parseFromUrl(urlParams{
"clue_id": parseParamToInt64,
}),
parseRequest(new(_SolveClueReq)),
solveClue,
toJson,
))
router.Put("/clue/{clue_id}", serve(
dashboardAuth,
parseRequest(new(record.Clue)),
updateClue,
toJson,
))
}
示例10: addTrackRoutes
func addTrackRoutes(router *pat.Router) {
router.Get("/tracks", serve(
dashboardAuth,
allTracks,
toJson,
))
router.Put("/track/{track_id}/toggle", serve(
dashboardAuth,
parseFromUrl(urlParams{
"track_id": parseParamToInt64,
}),
toggleTrackActivation,
noResponse,
))
}
示例11: CreateWeb
func CreateWeb(cfg *config.Config, pat *pat.Router, asset func(string) ([]byte, error)) *Web {
web := &Web{
config: cfg,
asset: asset,
}
WebPath = cfg.WebPath
log.Printf("Serving under http://%s%s/", cfg.UIBindAddr, WebPath)
pat.Path(WebPath + "/images/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/images/{{file}}"))
pat.Path(WebPath + "/css/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/css/{{file}}"))
pat.Path(WebPath + "/js/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/js/{{file}}"))
pat.Path(WebPath + "/fonts/{file:.*}").Methods("GET").HandlerFunc(web.Static("assets/fonts/{{file}}"))
pat.StrictSlash(true).Path(WebPath + "/").Methods("GET").HandlerFunc(web.Index())
return web
}
示例12: Register
// Register creates routes for each static resource
func Register(config render.Config, r *pat.Router) {
log.Debug("registering not found handler for static package", nil)
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
render.HTML(w, http.StatusNotFound, "error", render.DefaultVars(req, map[string]interface{}{"error": "Page not found"}))
})
log.Debug("registering static content handlers for static package", nil)
for _, file := range config.AssetNames()() {
if strings.HasPrefix(file, "static/") {
path := strings.TrimPrefix(file, "static")
log.Trace("registering handler for static asset", log.Data{"path": path})
var mimeType string
switch {
case strings.HasSuffix(path, ".css"):
mimeType = "text/css"
case strings.HasSuffix(path, ".js"):
mimeType = "application/javascript"
default:
mimeType = mime.TypeByExtension(path)
}
log.Trace("using mime type", log.Data{"type": mimeType})
r.Path(path).Methods("GET").HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if b, err := config.Asset()("static" + path); err == nil {
w.Header().Set("Content-Type", mimeType)
w.Header().Set("Cache-control", "public, max-age=259200")
w.WriteHeader(200)
w.Write(b)
return
}
// This should never happen!
log.ErrorR(req, errors.New("it happened ¯\\_(ツ)_/¯"), nil)
r.NotFoundHandler.ServeHTTP(w, req)
})
}
}
}
示例13: HomeHandler
func HomeHandler(router *pat.Router, w http.ResponseWriter, req *http.Request) {
url, _ := router.GetRoute("service").URL("service_name", "trolling-and-molling")
fmt.Fprintf(w, "Home handler! %s", url)
}
示例14: CreateAPIv2
func CreateAPIv2(conf *config.Config, r *pat.Router) *APIv2 {
log.Println("Creating API v2")
apiv2 := &APIv2{
config: conf,
}
r.Path("/api/v2/messages").Methods("GET").HandlerFunc(apiv2.messages)
r.Path("/api/v2/messages").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
r.Path("/api/v2/search").Methods("GET").HandlerFunc(apiv2.search)
r.Path("/api/v2/search").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
r.Path("/api/v2/jim").Methods("GET").HandlerFunc(apiv2.jim)
r.Path("/api/v2/jim").Methods("POST").HandlerFunc(apiv2.createJim)
r.Path("/api/v2/jim").Methods("PUT").HandlerFunc(apiv2.updateJim)
r.Path("/api/v2/jim").Methods("DELETE").HandlerFunc(apiv2.deleteJim)
r.Path("/api/v2/jim").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
r.Path("/api/v2/outgoing-smtp").Methods("GET").HandlerFunc(apiv2.listOutgoingSMTP)
r.Path("/api/v2/outgoing-smtp").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)
return apiv2
}
示例15: Register
// Register registers a healthcheck route with the router
func Register(r *pat.Router, path string, healthy func() bool) {
r.Path(path).Methods("GET").HandlerFunc(Handler(healthy))
}