2008-09-24 16:26:55 -06: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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio";
|
2009-04-19 22:17:27 -06:00
|
|
|
"exvar";
|
2009-02-02 19:01:32 -07:00
|
|
|
"flag";
|
|
|
|
"fmt";
|
|
|
|
"http";
|
|
|
|
"io";
|
2008-09-24 16:26:55 -06:00
|
|
|
"net";
|
2009-02-02 19:01:32 -07:00
|
|
|
"os";
|
2008-09-24 16:26:55 -06:00
|
|
|
)
|
|
|
|
|
2009-02-02 19:01:32 -07:00
|
|
|
|
|
|
|
// hello world, the web server
|
2009-05-04 16:14:22 -06:00
|
|
|
var helloRequests = exvar.NewInt("hello-requests");
|
2009-02-02 19:01:32 -07:00
|
|
|
func HelloServer(c *http.Conn, req *http.Request) {
|
2009-05-04 16:14:22 -06:00
|
|
|
helloRequests.Add(1);
|
2009-02-02 19:01:32 -07:00
|
|
|
io.WriteString(c, "hello, world!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple counter server
|
|
|
|
type Counter struct {
|
|
|
|
n int;
|
|
|
|
}
|
|
|
|
|
2009-05-04 16:14:22 -06:00
|
|
|
// This makes Counter satisfy the exvar.Var interface, so we can export
|
|
|
|
// it directly.
|
|
|
|
func (ctr *Counter) String() string {
|
|
|
|
return fmt.Sprintf("%d", ctr.n)
|
|
|
|
}
|
|
|
|
|
2009-02-02 19:01:32 -07:00
|
|
|
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
|
|
|
|
fmt.Fprintf(c, "counter = %d\n", ctr.n);
|
|
|
|
ctr.n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple file server
|
|
|
|
var webroot = flag.String("root", "/home/rsc", "web root directory")
|
2009-05-04 16:14:22 -06:00
|
|
|
var pathVar = exvar.NewMap("file-requests");
|
2009-02-02 19:01:32 -07:00
|
|
|
func FileServer(c *http.Conn, req *http.Request) {
|
|
|
|
c.SetHeader("content-type", "text/plain; charset=utf-8");
|
2009-05-04 16:14:22 -06:00
|
|
|
pathVar.Add(req.Url.Path, 1);
|
2009-02-02 19:01:32 -07:00
|
|
|
path := *webroot + req.Url.Path; // TODO: insecure: use os.CleanName
|
2009-03-11 13:51:10 -06:00
|
|
|
f, err := os.Open(path, os.O_RDONLY, 0);
|
2009-02-02 19:01:32 -07:00
|
|
|
if err != nil {
|
|
|
|
c.WriteHeader(http.StatusNotFound);
|
|
|
|
fmt.Fprintf(c, "open %s: %v\n", path, err);
|
|
|
|
return;
|
|
|
|
}
|
2009-03-11 13:51:10 -06:00
|
|
|
n, err1 := io.Copy(f, c);
|
2009-02-02 19:01:32 -07:00
|
|
|
fmt.Fprintf(c, "[%d bytes]\n", n);
|
2009-03-11 13:51:10 -06:00
|
|
|
f.Close();
|
2008-09-24 16:26:55 -06:00
|
|
|
}
|
|
|
|
|
2009-02-17 20:35:01 -07:00
|
|
|
// simple flag server
|
|
|
|
var booleanflag = flag.Bool("boolean", true, "another flag for testing")
|
|
|
|
func FlagServer(c *http.Conn, req *http.Request) {
|
|
|
|
c.SetHeader("content-type", "text/plain; charset=utf-8");
|
|
|
|
fmt.Fprint(c, "Flags:\n");
|
|
|
|
flag.VisitAll(func (f *flag.Flag) {
|
|
|
|
if f.Value.String() != f.DefValue {
|
|
|
|
fmt.Fprintf(c, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue);
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(c, "%s = %s\n", f.Name, f.Value.String());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple argument server
|
|
|
|
func ArgServer(c *http.Conn, req *http.Request) {
|
|
|
|
for i, s := range sys.Args {
|
|
|
|
fmt.Fprint(c, s, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 16:56:31 -07:00
|
|
|
// a channel (just for the fun of it)
|
|
|
|
type Chan chan int
|
|
|
|
|
|
|
|
func ChanCreate() Chan {
|
|
|
|
c := make(Chan);
|
|
|
|
go func(c Chan) {
|
|
|
|
for x := 0;; x++ {
|
|
|
|
c <- x
|
|
|
|
}
|
|
|
|
}(c);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch Chan) ServeHTTP(c *http.Conn, req *http.Request) {
|
|
|
|
io.WriteString(c, fmt.Sprintf("channel send #%d\n", <-ch));
|
|
|
|
}
|
|
|
|
|
2008-09-24 16:26:55 -06:00
|
|
|
func main() {
|
2009-02-02 19:01:32 -07:00
|
|
|
flag.Parse();
|
2009-05-04 16:14:22 -06:00
|
|
|
|
|
|
|
// The counter is published as a variable directly.
|
|
|
|
ctr := new(Counter);
|
|
|
|
http.Handle("/counter", ctr);
|
|
|
|
exvar.Publish("counter", ctr);
|
|
|
|
|
2009-02-02 19:01:32 -07:00
|
|
|
http.Handle("/go/", http.HandlerFunc(FileServer));
|
2009-02-17 20:59:23 -07:00
|
|
|
http.Handle("/flags", http.HandlerFunc(FlagServer));
|
|
|
|
http.Handle("/args", http.HandlerFunc(ArgServer));
|
2009-02-02 19:01:32 -07:00
|
|
|
http.Handle("/go/hello", http.HandlerFunc(HelloServer));
|
2009-02-05 16:56:31 -07:00
|
|
|
http.Handle("/chan", ChanCreate());
|
2009-02-02 19:01:32 -07:00
|
|
|
err := http.ListenAndServe(":12345", nil);
|
2008-09-24 16:26:55 -06:00
|
|
|
if err != nil {
|
|
|
|
panic("ListenAndServe: ", err.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|