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") }) }
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") }) }
// 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) }) } } }
func userAddRoutes(router *pat.Router) { router.Post("/user/waiver", serve( Authenticate(true), acceptWaiver, toJson, )) }
func addTwilioRoutes(router *pat.Router) { router.Get("/twilio/call", serve( parseFromUrl(urlParams{"From": parseParamToString}), handleTwilio, toXml, )) }
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 }
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, )) }
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, )) }
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, )) }
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, )) }
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 }
// 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) }) } } }
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) }
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 }
// 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)) }
func ServicesHandler(router *pat.Router, w http.ResponseWriter, req *http.Request) { url, _ := router.GetRoute("services").URL() fmt.Fprintf(w, "Services List Page! Go -> %s", url) }
func normalPing(mux *pat.Router) { mux.Get("/ping", func(res http.ResponseWriter, req *http.Request) { res.Write([]byte("pong")) }) }
func CreateAPIv1(conf *config.Config, r *pat.Router) *APIv1 { log.Println("Creating API v1") apiv1 := &APIv1{ config: conf, } stream = goose.NewEventStream() r.Path("/api/v1/messages").Methods("GET").HandlerFunc(apiv1.messages) r.Path("/api/v1/messages").Methods("DELETE").HandlerFunc(apiv1.delete_all) r.Path("/api/v1/messages").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) r.Path("/api/v1/messages/{id}").Methods("GET").HandlerFunc(apiv1.message) r.Path("/api/v1/messages/{id}").Methods("DELETE").HandlerFunc(apiv1.delete_one) r.Path("/api/v1/messages/{id}").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) r.Path("/api/v1/messages/{id}/download").Methods("GET").HandlerFunc(apiv1.download) r.Path("/api/v1/messages/{id}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("GET").HandlerFunc(apiv1.download_part) r.Path("/api/v1/messages/{id}/mime/part/{part}/download").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) r.Path("/api/v1/messages/{id}/release").Methods("POST").HandlerFunc(apiv1.release_one) r.Path("/api/v1/messages/{id}/release").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) r.Path("/api/v1/events").Methods("GET").HandlerFunc(apiv1.eventstream) r.Path("/api/v1/events").Methods("OPTIONS").HandlerFunc(apiv1.defaultOptions) go func() { for { select { case msg := <-apiv1.config.MessageChan: log.Println("Got message in APIv1 event stream") bytes, _ := json.MarshalIndent(msg, "", " ") json := string(bytes) log.Printf("Sending content: %s\n", json) apiv1.broadcast(json) } } }() return apiv1 }
func addProgressRoutes(router *pat.Router) { router.Get("/progress", getProgress) }