func (c *ContentController) handleDirectory(path string, e *httpapp.Env) *httpapp.Response {
	d, err := model.NewDirectoryModel(path)
	if err != nil {
		panic(err)
	}

	if e.Request.Method == "GET" {
		resp := httpapp.NewResponse(http.StatusOK)
		resp.Headers.Add("Content-Type", util.MediaType("application/json", "charset", "utf-8"))
		resp.Body.Write(view.NewJSONView().Render(d.List()))
		return resp
	} else {
		resp := httpapp.NewResponse(http.StatusMethodNotAllowed)
		resp.Body.WriteString(fmt.Sprintf("The %s method is not supported on directories", e.Request.Method))
		return resp
	}
}
Ejemplo n.º 2
0
func (c *prefixStripper) Call(e *httpapp.Env) *httpapp.Response {
	if p := strings.TrimPrefix(e.Request.URL.Path, c.prefix); len(p) < len(e.Request.URL.Path) {
		e.Request.URL.Path = p
		return c.app.Call(e)
	} else {
		resp := httpapp.NewResponse(http.StatusNotFound)
		resp.Body.WriteString(fmt.Sprintf("Page not found: %s", e.Request.URL.Path))
		return resp
	}
}
func (c *ContentController) handleFilePOST(path string, e *httpapp.Env) *httpapp.Response {
	f, err := model.CreateVersionedFileModel(path, util.SlurpRequestBody(e))
	if err != nil {
		panic(err)
	}
	c.commitChanges(f, e)
	resp := httpapp.NewResponse(http.StatusNoContent)
	resp.Headers.Add("Location", e.Request.URL.Path)
	resp.Headers.Add("Content-Type", util.GuessMediaType(f.Name()))
	return resp
}
Ejemplo n.º 4
0
func (c *FileServerComponent) Call(e *httpapp.Env) *httpapp.Response {
	upath := e.Request.URL.Path
	if !strings.HasPrefix(upath, "/") {
		upath = "/" + upath
		e.Request.URL.Path = upath
	}

	resp := httpapp.NewResponse(200)
	http.ServeFile(resp, e.Request, path.Join(c.Root, path.Clean(upath)))
	return resp
}
Ejemplo n.º 5
0
func (a *GoogleOAuthHandler) checkSession(e *httpapp.Env) *httpapp.Response {
	session := e.Get("session").(*sessions.Session)
	if _, ok := session.Data["user"]; !ok {
		session.Data["return_to"] = e.Request.URL.Path
		url := a.OauthConfig.AuthCodeURL("")
		resp := httpapp.NewResponse(http.StatusFound)
		resp.Headers.Add("Location", url)
		return resp
	} else {
		return a.App.Call(e)
	}
}
Ejemplo n.º 6
0
func (c *URLMapComponent) Call(e *httpapp.Env) *httpapp.Response {
	handler, _ := c.Mux.Handler(e.Request)
	if app, ok := handler.(httpapp.App); ok {
		return app.Call(e)
	} else {
		resp := httpapp.NewResponse(http.StatusNotFound)
		resp.Body.WriteString(
			fmt.Sprintf("Unable to get handler for %s, got %v instead", e.Request.URL.Path, handler),
		)
		return resp
	}
}
Ejemplo n.º 7
0
func (m *ErrorHandler) Call(e *httpapp.Env) (resp *httpapp.Response) {
	defer func() {
		if err := recover(); err != nil {
			var status int
			if os.IsNotExist(err.(error)) {
				status = http.StatusNotFound
			} else {
				status = http.StatusInternalServerError
			}
			resp = httpapp.NewResponse(status)
			resp.Body.WriteString(err.(error).Error())
		}
	}()
	return m.App.Call(e)
}
func (c *ContentController) handleFile(path string, e *httpapp.Env) *httpapp.Response {
	f, err := model.NewVersionedFileModel(path)
	if err != nil {
		panic(err)
	}

	if e.Request.Method == "GET" {
		return c.handleFileGET(f, e)
	} else if e.Request.Method == "PUT" {
		return c.handleFilePUT(f, e)
	} else if e.Request.Method == "DELETE" {
		return c.handleFileDELETE(f, e)
	} else {
		resp := httpapp.NewResponse(http.StatusMethodNotAllowed)
		resp.Body.WriteString(fmt.Sprintf("The %s method is not supported on existing files", e.Request.Method))
		return resp
	}
}
Ejemplo n.º 9
0
func (a *GoogleOAuthHandler) handleOAuth2Callback(e *httpapp.Env) *httpapp.Response {

	t := &oauth.Transport{Config: a.OauthConfig}
	t.Exchange(e.Request.FormValue("code"))

	base_user_info := a.getBaseUserInfo(t)
	ext_user_info := a.getExtendedUserInfo(t, base_user_info)
	user_info := auth.NewGoogleUser(
		base_user_info.Id,
		base_user_info.Email,
		ext_user_info.FullName,
	)

	session := e.Get("session").(*sessions.Session)
	session.Data["user"] = user_info

	resp := httpapp.NewResponse(http.StatusFound)
	resp.Headers.Add("Location", session.Data["return_to"].(string))
	return resp
}
Ejemplo n.º 10
0
func (c *ContentController) handleFileDELETE(f *model.VersionedFile, e *httpapp.Env) *httpapp.Response {
	f.Remove()
	c.commitChanges(f, e)
	return httpapp.NewResponse(http.StatusNoContent)
}
Ejemplo n.º 11
0
func (c *ContentController) handleFilePUT(f *model.VersionedFile, e *httpapp.Env) *httpapp.Response {
	f.Write(util.SlurpRequestBody(e))
	c.commitChanges(f, e)
	return httpapp.NewResponse(http.StatusNoContent)
}
Ejemplo n.º 12
0
func (c *ContentController) handleFileGET(f *model.VersionedFile, e *httpapp.Env) *httpapp.Response {
	resp := httpapp.NewResponse(http.StatusOK)
	resp.Headers.Add("Content-Type", util.GuessMediaType(f.Name()))
	resp.Body.WriteString(f.Read())
	return resp
}
Ejemplo n.º 13
0
func (c *ContentController) handleDirectoryPOST(path string, e *httpapp.Env) *httpapp.Response {
	resp := httpapp.NewResponse(http.StatusMethodNotAllowed)
	resp.Body.WriteString("The POST method is not supported on directories")
	return resp
}
Ejemplo n.º 14
0
func (c *RedirectComponent) Call(e *httpapp.Env) *httpapp.Response {
	resp := httpapp.NewResponse(http.StatusMovedPermanently)
	resp.Headers.Add("Location", c.Location)
	return resp
}