2009-02-06 16:26:30 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// GDS: Go Documentation Server
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio";
|
|
|
|
"flag";
|
|
|
|
"fmt";
|
|
|
|
"http";
|
|
|
|
"io";
|
|
|
|
"net";
|
|
|
|
"os";
|
2009-02-09 22:05:14 -07:00
|
|
|
"sort";
|
|
|
|
"log";
|
2009-03-13 17:59:51 -06:00
|
|
|
"template";
|
2009-02-06 16:26:30 -07:00
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
"utils";
|
|
|
|
"platform";
|
|
|
|
"compilation";
|
|
|
|
"printer";
|
2009-02-06 16:26:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
verbose = flag.Bool("v", false, "verbose mode");
|
|
|
|
port = flag.String("port", "6060", "server port");
|
2009-02-09 22:05:14 -07:00
|
|
|
root = flag.String("root", Platform.GOROOT, "go root directory");
|
2009-02-06 16:26:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
// Support for directory sorting.
|
|
|
|
type DirArray []os.Dir
|
|
|
|
func (p DirArray) Len() int { return len(p); }
|
|
|
|
func (p DirArray) Less(i, j int) bool { return p[i].Name < p[j].Name; }
|
|
|
|
func (p DirArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
|
|
|
|
|
|
|
|
|
|
|
|
func isGoFile(dir *os.Dir) bool {
|
2009-03-13 17:59:51 -06:00
|
|
|
const ext = ".go";
|
2009-02-09 22:05:14 -07:00
|
|
|
return dir.IsRegular() && Utils.Contains(dir.Name, ext, len(dir.Name) - len(ext));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func printLink(c *http.Conn, path, name string) {
|
2009-03-13 17:59:51 -06:00
|
|
|
fmt.Fprintf(c, "<a href=\"%s\">%s</a><br />\n", path + name, name);
|
2009-02-09 22:05:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
var dir_template = template.NewTemplateOrDie("dir_template.html");
|
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
func serveDir(c *http.Conn, dirname string) {
|
|
|
|
fd, err1 := os.Open(*root + dirname, os.O_RDONLY, 0);
|
|
|
|
if err1 != nil {
|
|
|
|
c.WriteHeader(http.StatusNotFound);
|
|
|
|
fmt.Fprintf(c, "Error: %v (%s)\n", err1, dirname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
list, err2 := fd.Readdir(-1);
|
2009-02-09 22:05:14 -07:00
|
|
|
if err2 != nil {
|
|
|
|
c.WriteHeader(http.StatusNotFound);
|
|
|
|
fmt.Fprintf(c, "Error: %v (%s)\n", err2, dirname);
|
|
|
|
return;
|
2009-02-06 16:26:30 -07:00
|
|
|
}
|
2009-03-11 13:51:10 -06:00
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
sort.Sort(DirArray(list));
|
2009-02-06 16:26:30 -07:00
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
c.SetHeader("content-type", "text/html; charset=utf-8");
|
|
|
|
path := dirname + "/";
|
2009-02-06 16:26:30 -07:00
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
// Print contents in 3 sections: directories, go files, everything else
|
2009-03-11 13:51:10 -06:00
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
// TODO handle Apply errors
|
|
|
|
dir_template.Apply(c, "<!--", template.Substitution {
|
|
|
|
"PATH-->" : func() {
|
|
|
|
fmt.Fprintf(c, "%s", path);
|
|
|
|
},
|
|
|
|
|
|
|
|
"DIRECTORIES-->" : func() {
|
|
|
|
for i, entry := range list {
|
|
|
|
if entry.IsDirectory() {
|
|
|
|
printLink(c, path, entry.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"GO FILES-->" : func() {
|
|
|
|
for i, entry := range list {
|
|
|
|
if isGoFile(&entry) {
|
|
|
|
printLink(c, path, entry.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
"OTHER FILES-->" : func() {
|
|
|
|
for i, entry := range list {
|
|
|
|
if !entry.IsDirectory() && !isGoFile(&entry) {
|
|
|
|
fmt.Fprintf(c, "%s<br />\n", entry.Name);
|
|
|
|
}
|
|
|
|
}
|
2009-02-09 22:05:14 -07:00
|
|
|
}
|
2009-03-13 17:59:51 -06:00
|
|
|
});
|
|
|
|
}
|
2009-02-09 22:05:14 -07:00
|
|
|
|
|
|
|
|
2009-03-13 17:59:51 -06:00
|
|
|
var error_template = template.NewTemplateOrDie("error_template.html");
|
|
|
|
|
|
|
|
func printErrors(c *http.Conn, filename string, errors Compilation.ErrorList) {
|
|
|
|
// TODO factor code - shouldn't do this here and in Compilation
|
|
|
|
src, ok := Platform.ReadSourceFile(*root + filename);
|
|
|
|
|
|
|
|
// TODO handle Apply errors
|
|
|
|
error_template.Apply(c, "<!--", template.Substitution {
|
|
|
|
"FILE_NAME-->" : func() {
|
|
|
|
fmt.Fprintf(c, "%s", filename);
|
|
|
|
},
|
|
|
|
|
|
|
|
"ERRORS-->" : func () {
|
|
|
|
if ok == false /* 6g bug139 */ {
|
|
|
|
fmt.Fprintf(c, "could not read file %s\n", *root + filename);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-26 17:10:07 -06:00
|
|
|
offs := 0;
|
2009-03-13 17:59:51 -06:00
|
|
|
for i, e := range errors {
|
2009-03-26 17:10:07 -06:00
|
|
|
if 0 <= e.Pos.Offset && e.Pos.Offset <= len(src) {
|
2009-03-13 17:59:51 -06:00
|
|
|
// TODO handle Write errors
|
2009-03-26 17:10:07 -06:00
|
|
|
c.Write(src[offs : e.Pos.Offset]);
|
2009-03-13 17:59:51 -06:00
|
|
|
// TODO this should be done using a .css file
|
|
|
|
fmt.Fprintf(c, "<b><font color=red>%s >>></font></b>", e.Msg);
|
2009-03-26 17:10:07 -06:00
|
|
|
offs = e.Pos.Offset;
|
2009-03-13 17:59:51 -06:00
|
|
|
} else {
|
2009-03-26 17:10:07 -06:00
|
|
|
log.Stdoutf("error position %d out of bounds (len = %d)", e.Pos.Offset, len(src));
|
2009-03-13 17:59:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO handle Write errors
|
2009-03-26 17:10:07 -06:00
|
|
|
c.Write(src[offs : len(src)]);
|
2009-02-09 22:05:14 -07:00
|
|
|
}
|
2009-03-13 17:59:51 -06:00
|
|
|
});
|
2009-02-09 22:05:14 -07:00
|
|
|
}
|
|
|
|
|
2009-02-06 16:26:30 -07:00
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
func serveFile(c *http.Conn, filename string) {
|
2009-02-06 16:26:30 -07:00
|
|
|
var flags Compilation.Flags;
|
2009-03-13 17:59:51 -06:00
|
|
|
prog, errors := Compilation.Compile(*root + filename, &flags);
|
|
|
|
if errors == nil {
|
2009-02-06 16:26:30 -07:00
|
|
|
c.WriteHeader(http.StatusNotFound);
|
2009-03-13 17:59:51 -06:00
|
|
|
fmt.Fprintf(c, "Error: could not read file (%s)\n", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errors) > 0 {
|
|
|
|
c.SetHeader("content-type", "text/html; charset=utf-8");
|
|
|
|
printErrors(c, filename, errors);
|
2009-02-06 16:26:30 -07:00
|
|
|
return;
|
|
|
|
}
|
2009-03-11 13:51:10 -06:00
|
|
|
|
2009-02-06 16:26:30 -07:00
|
|
|
c.SetHeader("content-type", "text/html; charset=utf-8");
|
2009-03-12 18:24:03 -06:00
|
|
|
Printer.Print(c, prog, true);
|
2009-02-06 16:26:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
func serve(c *http.Conn, req *http.Request) {
|
|
|
|
if *verbose {
|
|
|
|
log.Stdoutf("URL = %s\n", req.RawUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
path := Utils.SanitizePath(req.Url.Path);
|
|
|
|
dir, err := os.Stat(*root + path);
|
|
|
|
if err != nil {
|
|
|
|
c.WriteHeader(http.StatusNotFound);
|
|
|
|
fmt.Fprintf(c, "Error: %v (%s)\n", err, path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case dir.IsDirectory():
|
|
|
|
serveDir(c, path);
|
|
|
|
case isGoFile(dir):
|
|
|
|
serveFile(c, path);
|
|
|
|
default:
|
|
|
|
c.WriteHeader(http.StatusNotFound);
|
|
|
|
fmt.Fprintf(c, "Error: Not a directory or .go file (%s)\n", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-06 16:26:30 -07:00
|
|
|
func main() {
|
|
|
|
flag.Parse();
|
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
*root = Utils.SanitizePath(*root);
|
|
|
|
dir, err1 := os.Stat(*root);
|
|
|
|
if err1 != nil || !dir.IsDirectory() {
|
2009-03-13 17:59:51 -06:00
|
|
|
log.Exitf("root not found or not a directory: %s", *root);
|
2009-02-09 22:05:14 -07:00
|
|
|
}
|
|
|
|
|
2009-02-06 16:26:30 -07:00
|
|
|
if *verbose {
|
2009-02-09 22:05:14 -07:00
|
|
|
log.Stdoutf("Go Documentation Server\n");
|
|
|
|
log.Stdoutf("port = %s\n", *port);
|
|
|
|
log.Stdoutf("root = %s\n", *root);
|
2009-02-06 16:26:30 -07:00
|
|
|
}
|
|
|
|
|
2009-02-09 22:05:14 -07:00
|
|
|
http.Handle("/", http.HandlerFunc(serve));
|
|
|
|
err2 := http.ListenAndServe(":" + *port, nil);
|
|
|
|
if err2 != nil {
|
2009-02-12 17:06:21 -07:00
|
|
|
log.Exitf("ListenAndServe: %s", err2.String())
|
2009-02-06 16:26:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|