예제 #1
0
func (f redirectFormatter) serveFromReader(reader io.Reader, writer http.ResponseWriter, request *http.Request) {
	contents, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Warnf("%s %s: could not read from reader: %s", request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}

	var matches = winUrlRegexp.FindSubmatch(contents)
	if matches == nil || len(matches) < 2 {
		matches = firstLineRegexp.FindSubmatch(contents)
	}
	if matches == nil || len(matches) < 2 {
		log.Warnf("%s %s: could not find a URL in file: %v", request.Method, request.URL, matches)
		failer.ServeInternalServerError(writer, request)
		return
	}

	url, err := url.Parse(string(matches[1]))
	if err != nil {
		log.Warnf("%s %s: could not parse file contents: %s", request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}

	http.Redirect(writer, request, url.String(), http.StatusFound)
}
예제 #2
0
func (f markdownFormatter) serveFromReader(reader io.Reader, writer http.ResponseWriter, request *http.Request) {
	writer.Header().Set("Content-Type", markdownFormatterOutputMimeType)

	markdown, err := ioutil.ReadAll(reader)
	if err != nil {
		log.Warnf("%s %s: %s", request.Method, request.URL, err)
		failer.ServeInternalServerError(writer, request)
		return
	}

	html := blackfriday.MarkdownCommon(markdown)
	if err := f.renderer.Render(writer, request.URL, string(html)); err != nil {
		log.Warnf("%s %s: %s", request.Method, request.URL, err)
	}
}
예제 #3
0
파일: file.go 프로젝트: fxnn/gone
func (l *FilesystemLoader) processEvents() {
	for {
		select {
		case event, ok := <-l.watcher.Events:
			if !ok {
				log.Printf("watching filesystem templates stopped")
				return
			}
			var path, name = event.Name, l.templateNames[event.Name]
			var templateChan = l.templateChans[path]
			if event.Op == fsnotify.Write || event.Op == fsnotify.Chmod {
				if template, err := l.LoadHtmlTemplate(name); err != nil {
					log.Warnf("error while reloading template %s from %s: %s", name, path, err)
				} else {
					log.Printf("reloading template %s from %s", name, path)
					templateChan <- template
				}
			}
		case err, ok := <-l.watcher.Errors:
			if !ok {
				log.Printf("watching filesystem templates stopped")
				return
			}
			log.Printf("error while watching filesystem templates: %s", err)
		}
	}
}