// Handle writes the error from the context into the HttpResponseWriter with a // 500 http.StatusInternalServerError status code. func (h *DefaultErrorHandler) Handle(ctx context.Context) (stop bool, err error) { var handlerError HandlerError = ctx.Data().Get(DataKeyForError).(HandlerError) hostname, _ := os.Hostname() w := ctx.HttpResponseWriter() // write the error out w.Header().Set("Content-Type", "text/html") w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("<!DOCTYPE html><html><head>")) w.Write([]byte("<style>")) w.Write([]byte("h1 { font-size: 17px }")) w.Write([]byte("h1 strong {text-decoration:underline}")) w.Write([]byte("h2 { background-color: #ffd; padding: 20px }")) w.Write([]byte("footer { margin-top: 20px; border-top:1px solid black; padding:10px; font-size:0.9em }")) w.Write([]byte("</style>")) w.Write([]byte("</head><body>")) w.Write([]byte(fmt.Sprintf("<h1>Error in <code>%s</code></h1><h2>%s</h2>", handlerError.Handler, handlerError))) w.Write([]byte(fmt.Sprintf("<h3><code>%s</code> error in Handler <code>%v</code></h3> <code><pre>%s</pre></code>", reflect.TypeOf(handlerError.OriginalError), &handlerError.Handler, handlerError.Handler))) w.Write([]byte(fmt.Sprintf("on %s", hostname))) w.Write([]byte("<footer>Learn more about <a href='http://github.com/stretchr/goweb' target='_blank'>Goweb</a></footer>")) w.Write([]byte("</body></html>")) // responses are actually ignored return false, nil }
//helper function to translate args in URL query to samtools args //manual: http://samtools.sourceforge.net/samtools.shtml func ParseSamtoolsArgs(ctx context.Context) (argv []string, err error) { query := ctx.HttpRequest().URL.Query() var ( filter_options = map[string]string{ "head": "-h", "headonly": "-H", "count": "-c", } valued_options = map[string]string{ "flag": "-f", "lib": "-l", "mapq": "-q", "readgroup": "-r", } ) for src, des := range filter_options { if _, ok := query[src]; ok { argv = append(argv, des) } } for src, des := range valued_options { if _, ok := query[src]; ok { if val := query.Get(src); val != "" { argv = append(argv, des) argv = append(argv, val) } else { return nil, errors.New(fmt.Sprintf("required value not found for query arg: %s ", src)) } } } return argv, nil }
// Before gets called before any other method. func (r *BooksController) Before(ctx context.Context) error { // set a Books specific header ctx.HttpResponseWriter().Header().Set("X-Books-Controller", "true") return nil }
// Before gets called before any other method. func (r *DevelopersController) Before(ctx context.Context) error { // set a Developers specific header ctx.HttpResponseWriter().Header().Set("X-Developers-Controller", "true") ctx.HttpResponseWriter().Header().Set("Connection", "keep-alive") return nil }
// GET /api/session func (ctrl *SessionController) ReadMany(ctx context.Context) (err error) { session, _ := session_store.Get(ctx.HttpRequest(), session_name) if session.Values["username"] != nil { return goweb.API.RespondWithError(ctx, http.StatusBadRequest, "has session") } // check form value(not empty?) id := ctx.FormValue("id") passwd := ctx.FormValue("password") if id == "" || passwd == "" { return goweb.API.RespondWithError(ctx, http.StatusBadRequest, "not enough query") } // get userdata from database user, err := GetUser(id, passwd) if err != nil { return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, "error on database") } if user == nil { return goweb.API.RespondWithError(ctx, http.StatusUnauthorized, "user not found") } session.Values["username"] = user.Name session.Save(ctx.HttpRequest(), ctx.HttpResponseWriter()) return goweb.API.RespondWithData(ctx, nil) }
// WriteResponseObject writes the status code and response object to the HttpResponseWriter in // the specified context, in the format best suited based on the request. // // Goweb uses the WebCodecService to decide which codec to use when responding // see http://godoc.org/github.com/stretchr/codecs/services#WebCodecService for more information. // // This method should be used when the Goweb Standard Response Object does not satisfy the needs of // the API, but other Respond* methods are recommended. func (a *GowebAPIResponder) WriteResponseObject(ctx context.Context, status int, responseObject interface{}) error { service := a.GetCodecService() acceptHeader := ctx.HttpRequest().Header.Get("Accept") extension := ctx.FileExtension() hasCallback := len(ctx.QueryValue(CallbackParameter)) > 0 codec, codecError := service.GetCodecForResponding(acceptHeader, extension, hasCallback) if codecError != nil { return codecError } var options map[string]interface{} // do we need to add some options? if hasCallback { options = map[string]interface{}{constants.OptionKeyClientCallback: ctx.QueryValue(CallbackParameter)} } output, marshalErr := service.MarshalWithCodec(codec, responseObject, options) if marshalErr != nil { return marshalErr } // use the HTTP responder to respond ctx.HttpResponseWriter().Header().Set("Content-Type", codec.ContentType()) // TODO: test me a.httpResponder.With(ctx, status, output) return nil }
func PreAuthRequest(ctx context.Context) { id := ctx.PathValue("id") if p, err := preauth.Load(id); err != nil { err_msg := "err:@preAuth load: " + err.Error() logger.Error(err_msg) responder.RespondWithError(ctx, 500, err_msg) return } else { if n, err := node.LoadUnauth(p.NodeId); err == nil { switch p.Type { case "download": filename := n.Id if fn, has := p.Options["filename"]; has { filename = fn } streamDownload(ctx, n, filename) preauth.Delete(id) return default: responder.RespondWithError(ctx, 500, "Preauthorization type not supported: "+p.Type) } } else { err_msg := "err:@preAuth loadnode: " + err.Error() logger.Error(err_msg) responder.RespondWithError(ctx, 500, err_msg) } } return }
func (c *AccountTransactions) Create(ctx context.Context) (err error) { accountId := ctx.PathValue("account_id") data, err := ctx.RequestData() if err != nil { return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, err.Error()) } dataMap := data.(map[string]interface{}) transaction := models.Transaction{ AccountId: accountId, Debit: dataMap["debit"].(float64), Credit: dataMap["credit"].(float64), Kind: dataMap["kind"].(string), } createServ := &transactions.CreateServ{} transactionOut, err := createServ.Run(c.DbSession, transaction) if err != nil { log.Print(err) return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, err.Error()) } return goweb.API.RespondWithData(ctx, transactionOut) }
func serializeGowebResponse( c context.Context, syntaxName string, statements chan *goraptor.Statement) error { var str string if syntaxName == "trig" { // real trig mode is crashing serializer := goraptor.NewSerializer("ntriples") defer serializer.Free() ntriples, err := serializer.Serialize(statements, "") if err != nil { panic(err) } log.Printf("got %d bytes of ntriples", len(ntriples)) str = "<http://projects.bigasterisk.com/room/laundryDoor> { " + ntriples + "}" log.Printf("str now %d bytes", len(str)) } else { serializer := goraptor.NewSerializer(syntaxName) defer serializer.Free() var err error str, err = serializer.Serialize(statements, "") if err != nil { panic(err) } } c.HttpResponseWriter().Header().Set("Content-Type", goraptor.SerializerSyntax[syntaxName].MimeType) return goweb.Respond.With(c, 200, []byte(str)) }
func GetPopularLocations(ctx context.Context) error { var query bson.M isPersonal, err := strconv.ParseBool(ctx.FormValue("personal")) if err != nil { isPersonal = false } if isPersonal { var result m.PopularLocations var currentUser, ok = ctx.Data()["user"].(m.User) if ok { query = bson.M{"user": currentUser.Id} if err := m.GetDB("RecommendUser").Find(query).One(&result); err != nil { //respond with empty arrays it no recommendations/popular location for current user result = m.PopularLocations{PopularMarkers: make([]m.Marker, 0), Recommendations: make([]m.Recommendation, 0)} //fmt.Printf("%v %v", err, result) } return goweb.API.RespondWithData(ctx, result) } } var trendResult []m.TrendingAllUsersEntry var recomResult []m.Recommendation m.GetDB("TrendingAllUsers").Find(query).All(&trendResult) m.GetDB("RecommendAllUsers").Find(query).All(&recomResult) result := m.AllUsersTrendingAndRecom{Trend: trendResult, Recom: recomResult} return goweb.API.RespondWithData(ctx, result) }
func DeleteImage(ctx context.Context) error { var currentUser, ok = ctx.Data()["user"].(m.User) if ok { imageId := ctx.FormValue("imageId") if imageId == "" { return goweb.API.Respond(ctx, 200, nil, []string{"You have to specify an image to be deleted."}) } var image m.Image err := m.GetDB("Image").FindId(bson.ObjectIdHex(imageId)).One(&image) if err != nil { log.Error("DeleteImage, remove query " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"You have to specify an image to be deleted."}) } if image.User.Hex() != currentUser.Id.Hex() { return goweb.API.Respond(ctx, 200, nil, []string{"You cannot delete this image."}) } else { m.GetDB("Image").RemoveId(image.Id) err = os.Remove(image.Url) } return goweb.API.RespondWithData(ctx, "Image deleted.") } else { return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to delete an images."}) } }
func GetUserInfo(ctx context.Context) error { var currentUser, ok = ctx.Data()["user"].(m.User) if ok { return goweb.API.RespondWithData(ctx, currentUser) } else { return goweb.API.Respond(ctx, 200, nil, []string{"Please log in."}) } }
// With writes a response to the request in the specified context. func (r *GowebHTTPResponder) With(ctx context.Context, httpStatus int, body []byte) error { r.WithStatus(ctx, httpStatus) _, writeErr := ctx.HttpResponseWriter().Write(body) return writeErr }
func getUserNameFromCtx(ctx context.Context) (name string) { session, _ := session_store.Get(ctx.HttpRequest(), session_name) user_raw := session.Values["username"] if user_raw == nil { return "" } name = user_raw.(string) return }
func nuttyGetLength(c context.Context) error { sessionid := c.PathValue("sessionid") bs, err := ioutil.ReadFile(basedir + sessionid + "/rec.json") if err != nil { log.Println(err) return goweb.API.RespondWithError(c, http.StatusInternalServerError, "Unable to ReadFile") } return goweb.Respond.With(c, 200, bs) }
func AowQuote(ctx context.Context) error { index := randInt(len(quotes)) quote := quotes[index] key := ctx.PathParams().Get("apiKey").Str() if valid := isKeyValid(key); valid == true { return goweb.API.RespondWithData(ctx, objx.MSI("quote", quote)) } return goweb.API.RespondWithError(ctx, 400, "Access Denied: Invalid API Key") }
// WithStatus writes the specified HTTP Status Code to the Context's ResponseWriter. // // If the Always200ParamName parameter is present, it will ignore the httpStatus argument, // and always write net/http.StatusOK (200). func (r *GowebHTTPResponder) WithStatus(ctx context.Context, httpStatus int) error { // check for always200 if len(ctx.FormValue(Always200ParamName)) > 0 { // always return OK httpStatus = http.StatusOK } ctx.HttpResponseWriter().WriteHeader(httpStatus) return nil }
func UploadZip(ctx context.Context) error { var currentUser, ok = ctx.Data()["user"].(m.User) if ok { file, _, err := ctx.HttpRequest().FormFile("file") if err != nil { log.Error("Error on zip upload FormFile " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."}) } defer file.Close() outputZipPath := "../uploads/temp/" + base64.StdEncoding.EncodeToString([]byte(currentUser.Username+time.Now().String())) if err := os.MkdirAll("../uploads/temp/", 0777); err != nil { log.Error("Error in creating output dir " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."}) } outputZipFile, err := os.Create(outputZipPath) if err != nil { log.Error("Error in creating output file in zip " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."}) } defer outputZipFile.Close() if _, err = io.Copy(outputZipFile, file); err != nil { log.Error("Error in copying file " + err.Error()) os.Remove(outputZipPath) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the zip."}) } var extention = "" if runtime.GOOS == "windows" { extention = ".exe" } extractPath := "../uploads/temp/" + base64.StdEncoding.EncodeToString([]byte(currentUser.Username+time.Now().String())) commandString := fmt.Sprintf(`7za%s e %s -o%s -p%s -aoa`, extention, outputZipPath, extractPath, "SuperSecurePassword") commandSlice := strings.Fields(commandString) commandCall := exec.Command(commandSlice[0], commandSlice[1:]...) // err = commandCall.Run() value, err := commandCall.Output() if err != nil { log.Error("in unarchiving zip file: " + err.Error()) log.Error("Full info about the error: " + string(value)) } go utils.ProcessZip(currentUser, extractPath) return goweb.API.Respond(ctx, 200, nil, nil) } else { return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to upload a zip."}) } }
// WithStatusText writes the specified HTTP Status Code to the Context's ResponseWriter and // includes a body with the default status text. func (r *GowebHTTPResponder) WithStatusText(ctx context.Context, httpStatus int) error { writeStatusErr := r.WithStatus(ctx, httpStatus) if writeStatusErr != nil { return writeStatusErr } // write the body header _, writeErr := ctx.HttpResponseWriter().Write([]byte(http.StatusText(httpStatus))) return writeErr }
func nuttyGet(c context.Context) error { sessionid := c.PathValue("sessionid") tindex := c.PathValue("tindex") log.Println("GET ", sessionid, " ", tindex) bs, err := ioutil.ReadFile(basedir + sessionid + "/" + tindex) if err != nil { log.Println(err) return goweb.API.RespondWithError(c, http.StatusInternalServerError, "Unable to ReadFile") } return goweb.Respond.With(c, 200, bs) }
// GET /api/statuses func (ctrl *StatusesController) ReadMany(ctx context.Context) (err error) { count, err := strconv.Atoi(ctx.FormValue("count")) if err != nil { count = 15 } dat, err := GetStatuses(count) if err != nil { logger.Debug(err) return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, "error on db") } return goweb.API.RespondWithData(ctx, dat) }
func UploadImgData(ctx context.Context) error { var currentUser, ok = ctx.Data()["user"].(m.User) if ok { file, _, err := ctx.HttpRequest().FormFile("file") if err != nil { log.Error("Error on image upload FormFile " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to upload the image."}) } defer file.Close() utils.ParseImageTable(currentUser, file) return goweb.API.Respond(ctx, 200, nil, nil) } else { return goweb.API.Respond(ctx, 200, nil, []string{"Please log in to upload your images."}) } }
func (c *Accounts) Adjust(ctx context.Context) (err error) { id := ctx.PathParams().Get("id").Str() getServ := &accounts.GetServ{} account, err := getServ.Run(c.DbSession, id) if err != nil { return goweb.API.RespondWithError(ctx, http.StatusNotFound, err.Error()) } adjustServ := &account_balances.AdjustServ{} account, _, err = adjustServ.Run(c.DbSession, account) if err != nil { return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, err.Error()) } return goweb.API.RespondWithData(ctx, account) }
func GetVideos(ctx context.Context) error { var result []m.Video // Get the info from the context fromDate, fromDateExists := utils.ParseDateFromContext("from_", ctx) toDate, toDateExists := utils.ParseDateFromContext("to_", ctx) limit, err := strconv.Atoi(ctx.FormValue("limit")) if err != nil { limit = 5 } skip, err := strconv.Atoi(ctx.FormValue("skip")) if err != nil { skip = 0 } var currentUser, ok = ctx.Data()["user"].(m.User) isPersonal, err := strconv.ParseBool(ctx.FormValue("personal")) if err != nil { isPersonal = false } query := bson.M{} if ok && isPersonal { if fromDateExists || toDateExists { query["date"] = bson.M{"$gte": fromDate, "$lte": toDate} query["user"] = currentUser.Id } else { query["user"] = currentUser.Id } } else { isPersonal = false if fromDateExists || toDateExists { query["date"] = bson.M{"$gte": fromDate, "$lte": toDate} } } if !fromDateExists && !toDateExists { var lastVideo m.Image var oneQuery = bson.M{} if isPersonal { oneQuery["user"] = currentUser.Id } if err = m.GetDB("Video").Find(oneQuery).Sort("-date").One(&lastVideo); err == nil { query["date"] = bson.M{"$gte": lastVideo.Date.AddDate(0, 0, -1), "$lte": lastVideo.Date.AddDate(0, 0, 1)} } } if err := m.GetDB("Video").Find(query).Skip(skip).Limit(limit).All(&result); err != nil { log.Error("Failed to search for tracks: " + err.Error()) return goweb.API.Respond(ctx, 200, nil, []string{"Failed to find the tracks."}) } return goweb.API.RespondWithData(ctx, result) }
func ParseDateFromContext(prefix string, ctx context.Context) (time.Time, bool) { now := time.Now() if strings.Contains(prefix, "from") { now = now.AddDate(0, 0, -1) } if strings.Contains(prefix, "to") { now = now.AddDate(0, 0, 1) } anythingSelected := false year, err := strconv.Atoi(ctx.FormValue(prefix + "year")) if err != nil { year = now.Year() } else { anythingSelected = true } var month time.Month month_int, err := strconv.Atoi(ctx.FormValue(prefix + "month")) if err != nil { month = now.Month() } else { month = time.Month(month_int) anythingSelected = true } day, err := strconv.Atoi(ctx.FormValue(prefix + "day")) if err != nil { day = now.Day() } else { anythingSelected = true } hour, err := strconv.Atoi(ctx.FormValue(prefix + "hour")) if err != nil { hour = now.Hour() } else { anythingSelected = true } min, err := strconv.Atoi(ctx.FormValue(prefix + "min")) if err != nil { min = now.Minute() } else { anythingSelected = true } return time.Date(year, month, day, hour, min, 0, 0, time.UTC), anythingSelected }
func CheckHeaderIsValidWithBasicAuth(c context.Context) (didpass bool, applicationid int, username string, password string) { data, dataError := c.RequestData() if dataError != nil { log.Println("Data error %s", dataError.Error()) return false, 0, "", "" } dataMap := data.(map[string]interface{}) appd, ok := dataMap["applicationid"] if !ok { log.Println("Header missing applicationid") return false, 0, "", "" } basicd, ok := dataMap["authorization"] if !ok { log.Println("Header missing authorization") return false, 0, "", "" } if basicd != "Basic" { log.Println("Header missing basic Authorization") return false, 0, "", "" } userd, ok := dataMap["username"] if !ok { log.Println("Header missing username") return false, 0, "", "" } passd, ok := dataMap["password"] if !ok { log.Println("Header missing password") return false, 0, "", "" } appi, _ := strconv.Atoi(appd.(string)) return true, appi, userd.(string), passd.(string) }
func CheckHeaderIsValidWithBasicAuthAndRawData(c context.Context) (didpass bool, applicationid int, username string, password string, raw string) { data, dataError := c.RequestData() if dataError != nil { log.Println("Data error %s", dataError.Error()) return false, 0, "", "", "" } dataMap := data.(map[string]interface{}) rawd, ok := dataMap["raw"] if !ok { log.Println("Header missing raw data") return false, 0, "", "", "" } valid, appid, userd, passd := CheckHeaderIsValidWithBasicAuth(c) return valid, appid, userd, passd, rawd.(string) }
func isSessionValid(ctx context.Context) bool { session, _ := session_store.Get(ctx.HttpRequest(), session_name) session_token := session.Values["token"] session.Values["token"] = "" session.Save(ctx.HttpRequest(), ctx.HttpResponseWriter()) if session_token == "" || session_token != ctx.FormValue("token") { return false } return true }
func (r *ThingsController) Create(ctx context.Context) error { data, dataErr := ctx.RequestData() if dataErr != nil { return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, dataErr.Error()) } dataMap := data.(map[string]interface{}) thing := new(Thing) thing.Id = dataMap["Id"].(string) thing.Text = dataMap["Text"].(string) r.Things = append(r.Things, thing) return goweb.Respond.WithStatus(ctx, http.StatusCreated) }
// GET /api/session func (ctrl *SearchController) ReadMany(ctx context.Context) (err error) { query := ctx.FormValue("q") if query == "" { return goweb.API.RespondWithError(ctx, http.StatusUnauthorized, "not enough query") } count, err := strconv.Atoi(ctx.FormValue("count")) if err != nil { count = 10 } statuses, err := SearchStatuses(query, count) if err != nil { logger.Debug(err) return goweb.API.RespondWithError(ctx, http.StatusInternalServerError, "error on db") } return goweb.API.RespondWithData(ctx, statuses) }