Exemplo n.º 1
0
func handleError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	if err != nil {
		var buf bytes.Buffer
		fmt.Fprintf(&buf, "Error serving %s: %v\n", req.URL, err)
		if r != nil {
			fmt.Fprintln(&buf, r)
			buf.Write(debug.Stack())
		}
		log.Print(buf.String())
	}
	switch status {
	case 0:
		// nothing to do
	case web.StatusNotFound:
		executeTemplate(resp, "notfound"+templateExt(req), status, nil)
	default:
		s := web.StatusText(status)
		if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(doc.GetError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(web.StatusInternalServerError, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
Exemplo n.º 2
0
func executeTemplate(resp web.Response, name string, status int, data interface{}) error {
	contentType, ok := contentTypes[path.Ext(name)]
	if !ok {
		contentType = "text/plain; charset=utf-8"
	}

	w := resp.Start(status, web.Header{web.HeaderContentType: {contentType}})
	return templateSet.ExecuteTemplate(w, name, data)
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: AlekSi/gddo
func serveCompile(resp web.Response, req *web.Request) error {
	r, err := http.PostForm("http://golang.org/compile", req.Form)
	if err != nil {
		return err
	}
	defer r.Body.Close()
	_, err = io.Copy(
		resp.Start(web.StatusOK, web.Header{web.HeaderContentType: r.Header[web.HeaderContentType]}),
		r.Body)
	return err
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: AlekSi/gddo
func serveAPIPackages(resp web.Response, req *web.Request) error {
	pkgs, err := db.AllPackages()
	if err != nil {
		return err
	}
	var data struct {
		Results []database.Package `json:"results"`
	}
	data.Results = pkgs
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(&data)
}
Exemplo n.º 5
0
func executeTemplate(resp web.Response, name string, status int, data interface{}) error {
	contentType, ok := contentTypes[path.Ext(name)]
	if !ok {
		contentType = "text/plain; charset=utf-8"
	}
	t := templates[name]
	if t == nil {
		return fmt.Errorf("Template %s not found", name)
	}
	w := resp.Start(status, web.Header{web.HeaderContentType: {contentType}})
	return t.Execute(w, data)
}
Exemplo n.º 6
0
Arquivo: main.go Projeto: AlekSi/gddo
func serveTypeahead(resp web.Response, req *web.Request) error {
	pkgs, err := db.Popular(1000)
	if err != nil {
		return err
	}
	items := make([]string, len(pkgs))
	for i, pkg := range pkgs {
		items[i] = pkg.Path
	}
	data := map[string]interface{}{"items": items}
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(data)
}
Exemplo n.º 7
0
Arquivo: main.go Projeto: AlekSi/gddo
func serveAPISearch(resp web.Response, req *web.Request) error {
	q := strings.TrimSpace(req.Form.Get("q"))
	pkgs, err := db.Query(q)
	if err != nil {
		return err
	}

	var data struct {
		Results []database.Package `json:"results"`
	}
	data.Results = pkgs
	w := resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
	return json.NewEncoder(w).Encode(&data)
}
Exemplo n.º 8
0
Arquivo: main.go Projeto: AlekSi/gddo
func handleAPIError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	default:
		var data struct {
			Error struct {
				Message string `json:"message"`
			} `json:"error"`
		}
		data.Error.Message = web.StatusText(status)
		w := resp.Start(status, web.Header{web.HeaderContentType: {"application/json; charset=uft-8"}})
		json.NewEncoder(w).Encode(&data)
	}
}
Exemplo n.º 9
0
Arquivo: main.go Projeto: AlekSi/gddo
func renderPresentation(resp web.Response, fname string, doc *present.Doc) error {
	t := presentTemplates[path.Ext(fname)]
	data := struct {
		*present.Doc
		Template    *template.Template
		PlayEnabled bool
	}{
		doc,
		t,
		true,
	}

	return t.Execute(
		resp.Start(web.StatusOK, web.Header{web.HeaderContentType: {"text/html; charset=utf8"}}),
		&data)
}
Exemplo n.º 10
0
Arquivo: main.go Projeto: AlekSi/gddo
func handleError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	case web.StatusNotFound:
		executeTemplate(resp, "notfound"+templateExt(req), status, nil, nil)
	default:
		s := web.StatusText(status)
		if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(*doc.RemoteError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(web.StatusInternalServerError, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
Exemplo n.º 11
0
Arquivo: main.go Projeto: AlekSi/gddo
func handlePresentError(resp web.Response, req *web.Request, status int, err error, r interface{}) {
	logError(req, err, r)
	switch status {
	case 0:
		// nothing to do
	default:
		s := web.StatusText(status)
		if doc.IsNotFound(err) {
			s = web.StatusText(web.StatusNotFound)
			status = web.StatusNotFound
		} else if err == errUpdateTimeout {
			s = "Timeout getting package files from the version control system."
		} else if e, ok := err.(*doc.RemoteError); ok {
			s = "Error getting package files from " + e.Host + "."
		}
		w := resp.Start(status, web.Header{web.HeaderContentType: {"text/plan; charset=uft-8"}})
		io.WriteString(w, s)
	}
}
Exemplo n.º 12
0
func rootHandler(resp web.Response, req *web.Request) error {

	key := req.URL.Path[1:]
	if key == "" {
		key = config.Index
	}

	var status = web.StatusOK
	b, mimeType, err := readKey(key)
	if err != nil && config.Error != "" {
		status = web.StatusNotFound
		b, mimeType, err = readKey(config.Error)
	}
	if err != nil {
		return &web.Error{Status: web.StatusNotFound, Reason: err}
	}

	w := resp.Start(status, web.Header{
		web.HeaderContentLength: {strconv.Itoa(len(b))},
		web.HeaderContentType:   {mimeType},
	})
	_, err = w.Write(b)
	return err
}