Ejemplo n.º 1
0
func (e *TemplateDeliverer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
	if request.Method != "GET" {
		log.Printf("%s %s: wrong method for template handling", request.Method, request.URL)
		failer.ServeMethodNotAllowed(writer, request)
		return
	}

	readCloser, err := e.loader.LoadResource(request.URL.Path)
	if err != nil {
		log.Printf("%s %s: error while opening template resource: %s",
			request.Method, request.URL, err)
		failer.ServeNotFound(writer, request)
		return
	}
	defer readCloser.Close()

	writer.Header().Set("Content-Type", e.detectMimeType(request))

	_, err = io.Copy(writer, readCloser)
	if err != nil {
		log.Printf("%s %s: error while writing template resource to output: %s",
			request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}
}
Ejemplo n.º 2
0
Archivo: Viewer.go Proyecto: fxnn/gone
func (v *Viewer) serveError(writer http.ResponseWriter, request *http.Request, err error) {
	v.log(request, err)

	if store.IsPathNotFoundError(err) {
		failer.ServeNotFound(writer, request)
		return
	}

	failer.ServeInternalServerError(writer, request)
}
Ejemplo n.º 3
0
Archivo: editor.go Proyecto: fxnn/gone
func (e *Editor) serveEditUI(writer http.ResponseWriter, request *http.Request) {
	if !e.store.HasWriteAccessForRequest(request) {
		log.Printf("%s %s: no write permissions", request.Method, request.URL)
		failer.ServeUnauthorized(writer, request)
		return
	}
	if !router.Is(router.ModeCreate, request) && !e.store.HasReadAccessForRequest(request) {
		log.Printf("%s %s: no read permissions", request.Method, request.URL)
		failer.ServeUnauthorized(writer, request)
		return
	}

	mimeType := e.store.MimeTypeForRequest(request)
	if err := e.assertEditableTextFile(request, mimeType); err != nil {
		log.Printf("%s %s: no editable text file: %s", request.Method, request.URL, err)
		failer.ServeUnsupportedMediaType(writer, request)
		return
	}

	var content = e.store.ReadString(request)
	if err := e.store.Err(); err != nil {
		if !store.IsPathNotFoundError(err) {
			log.Printf("%s %s: %s", request.Method, request.URL, err)
			failer.ServeInternalServerError(writer, request)
			return
		} else if router.Is(router.ModeEdit, request) {
			log.Printf("%s %s: file to be edited does not exist: %s", request.Method, request.URL, err)
			failer.ServeNotFound(writer, request)
			return
		}
	} else if router.Is(router.ModeCreate, request) {
		log.Printf("%s %s: file to be created already exists: %s", request.Method, request.URL, err)
		failer.ServeConflict(writer, request)
		return
	}

	err := e.renderer.Render(writer, request.URL, content, mimeType,
		router.Is(router.ModeEdit, request))
	if err != nil {
		log.Printf("%s %s: %s", request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}

	log.Printf("%s %s: served from template", request.Method, request.URL)
}