diff options
author | bnewbold <bnewbold@robocracy.org> | 2012-04-22 22:44:08 -0400 |
---|---|---|
committer | bnewbold <bnewbold@robocracy.org> | 2012-04-22 22:44:08 -0400 |
commit | f9ef9d242c4d1de392269379598d2a05109d44ad (patch) | |
tree | bdd1ae26da09594fe73baf71ea69df3a40a86982 | |
parent | b4f1701943d9a8628d9b30ecf34a7d4687b60b00 (diff) | |
download | bommom-f9ef9d242c4d1de392269379598d2a05109d44ad.tar.gz bommom-f9ef9d242c4d1de392269379598d2a05109d44ad.zip |
http serving first steps
-rw-r--r-- | bommom.go | 7 | ||||
-rw-r--r-- | serve.go | 68 | ||||
-rw-r--r-- | templates/home.html | 9 | ||||
-rw-r--r-- | templates/static/default.css | 1 |
4 files changed, 84 insertions, 1 deletions
@@ -25,6 +25,8 @@ var ( helpFlag = flag.Bool("help", false, "print full help info") outFormat = flag.String("format", "", "command output format (for 'dump' etc)") inFormat = flag.String("informat", "", "command output format (for 'load' etc)") + listenPort = flag.Uint("port", 7070, "port to listen on (HTTP serve)") + listenHost = flag.String("host", "", "hostname to listen on (HTTP serve)") ) func main() { @@ -52,7 +54,7 @@ func main() { switch flag.Arg(0) { default: log.Fatal("Error: unknown command: ", flag.Arg(0)) - case "serve": + case "magic", "love": log.Fatal("Error: Unimplemented, sorry") case "init": log.Println("Initializing...") @@ -65,6 +67,9 @@ func main() { convertCmd() case "list": listCmd() + case "serve": + // defined in serve.go + serveCmd() } } diff --git a/serve.go b/serve.go new file mode 100644 index 0000000..dff2006 --- /dev/null +++ b/serve.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "html/template" + "log" + "net/http" + "regexp" +) + +var ( + tmplHome, tmplView *template.Template +) + +func baseHandler(w http.ResponseWriter, r *http.Request) { + var err error + log.Printf("serving %s\n", r.URL.Path) + + bomUrlPattern := regexp.MustCompile("^/([a-zA-Z][a-zA-Z0-9_]*)/([a-zA-Z][a-zA-Z0-9_]*)/(.*)$") + userUrlPattern := regexp.MustCompile("^/([a-zA-Z][a-zA-Z0-9_]*)/$") + + switch { + case r.URL.Path == "/": + err = tmplHome.Execute(w, nil) + case bomUrlPattern.MatchString(r.URL.Path): + match := bomUrlPattern.FindStringSubmatch(r.URL.Path) + bomController(w, r, match[1], match[2], match[3]) + case userUrlPattern.MatchString(r.URL.Path): + match := userUrlPattern.FindStringSubmatch(r.URL.Path) + fmt.Fprintf(w, "will show BOM list here for user %s", match[1]) + default: + // 404 + log.Println("warning: 404") + http.NotFound(w, r) + return + } + if err != nil { + // this could cause multiple responses? + http.Error(w, "Internal error (check logs)", 500) + log.Println("error, 500: " + err.Error()) + } +} + +func bomController(w http.ResponseWriter, r *http.Request, user, name, extra string) { + fmt.Fprintf(w, "will show BOM %s/%s here?", user, name) +} + +func serveCmd() { + var err error + + // load and parse templates + tmplHome = template.Must(template.ParseFiles(*templatePath + "/home.html")) + tmplView = template.Must(template.ParseFiles(*templatePath + "/view.html")) + if err != nil { + log.Fatal(err) + } + + // serve template static assets (images, CSS, JS) + http.Handle("/static/", http.FileServer(http.Dir(*templatePath+"/"))) + http.Handle("/favicon.ico", http.FileServer(http.Dir(*templatePath+"/static/"))) + + // fall through to default handler + http.HandleFunc("/", baseHandler) + + listenString := fmt.Sprintf("%s:%d", *listenHost, *listenPort) + http.ListenAndServe(listenString, nil) + fmt.Println("Serving at " + listenString) +} diff --git a/templates/home.html b/templates/home.html new file mode 100644 index 0000000..0cc420e --- /dev/null +++ b/templates/home.html @@ -0,0 +1,9 @@ +<html> +<head> +<title>bombom</title> +<link rel="stylesheet" type="text/css" href="/static/default.css"></link> +</head> +<body> +<h1>Home Page</h1> +</body> +</html> diff --git a/templates/static/default.css b/templates/static/default.css new file mode 100644 index 0000000..40ff439 --- /dev/null +++ b/templates/static/default.css @@ -0,0 +1 @@ +/* this will be the default CSS file */ |