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 (
|
2009-12-15 16:35:38 -07:00
|
|
|
"bytes"
|
|
|
|
"expvar"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2010-06-08 18:51:57 -06:00
|
|
|
"http"
|
2009-12-15 16:35:38 -07:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2008-09-24 16:26:55 -06:00
|
|
|
)
|
|
|
|
|
2009-02-02 19:01:32 -07:00
|
|
|
|
|
|
|
// hello world, the web server
|
2009-10-06 20:41:51 -06:00
|
|
|
var helloRequests = expvar.NewInt("hello-requests")
|
|
|
|
|
2010-09-29 21:22:28 -06:00
|
|
|
func HelloServer(w http.ResponseWriter, req *http.Request) {
|
2009-12-15 16:35:38 -07:00
|
|
|
helloRequests.Add(1)
|
2010-09-29 21:22:28 -06:00
|
|
|
io.WriteString(w, "hello, world!\n")
|
2009-02-02 19:01:32 -07:00
|
|
|
}
|
|
|
|
|
2009-06-06 18:30:17 -06:00
|
|
|
// Simple counter server. POSTing to it will set the value.
|
2009-02-02 19:01:32 -07:00
|
|
|
type Counter struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
n int
|
2009-02-02 19:01:32 -07:00
|
|
|
}
|
|
|
|
|
2009-09-30 14:11:33 -06:00
|
|
|
// This makes Counter satisfy the expvar.Var interface, so we can export
|
2009-05-04 16:14:22 -06:00
|
|
|
// it directly.
|
2009-12-15 16:35:38 -07:00
|
|
|
func (ctr *Counter) String() string { return fmt.Sprintf("%d", ctr.n) }
|
2009-05-04 16:14:22 -06:00
|
|
|
|
2010-09-29 21:22:28 -06:00
|
|
|
func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2009-06-06 18:30:17 -06:00
|
|
|
switch req.Method {
|
|
|
|
case "GET":
|
2009-11-09 13:07:39 -07:00
|
|
|
ctr.n++
|
2009-06-06 18:30:17 -06:00
|
|
|
case "POST":
|
2009-12-15 16:35:38 -07:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
io.Copy(buf, req.Body)
|
|
|
|
body := buf.String()
|
2009-06-06 18:30:17 -06:00
|
|
|
if n, err := strconv.Atoi(body); err != nil {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(w, "bad POST: %v\nbody: [%v]\n", err, body)
|
2009-06-06 18:30:17 -06:00
|
|
|
} else {
|
2009-12-15 16:35:38 -07:00
|
|
|
ctr.n = n
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprint(w, "counter reset\n")
|
2009-06-06 18:30:17 -06:00
|
|
|
}
|
|
|
|
}
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(w, "counter = %d\n", ctr.n)
|
2009-02-02 19:01:32 -07:00
|
|
|
}
|
|
|
|
|
2009-02-17 20:35:01 -07:00
|
|
|
// simple flag server
|
|
|
|
var booleanflag = flag.Bool("boolean", true, "another flag for testing")
|
2009-10-06 20:41:51 -06:00
|
|
|
|
2010-09-29 21:22:28 -06:00
|
|
|
func FlagServer(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.SetHeader("content-type", "text/plain; charset=utf-8")
|
|
|
|
fmt.Fprint(w, "Flags:\n")
|
2009-10-06 20:41:51 -06:00
|
|
|
flag.VisitAll(func(f *flag.Flag) {
|
2009-02-17 20:35:01 -07:00
|
|
|
if f.Value.String() != f.DefValue {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue)
|
2009-02-17 20:35:01 -07:00
|
|
|
} else {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String())
|
2009-02-17 20:35:01 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
})
|
2009-02-17 20:35:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// simple argument server
|
2010-09-29 21:22:28 -06:00
|
|
|
func ArgServer(w http.ResponseWriter, req *http.Request) {
|
2010-06-08 18:51:57 -06:00
|
|
|
for _, s := range os.Args {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprint(w, s, " ")
|
2009-02-17 20:35:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-05 16:56:31 -07:00
|
|
|
// a channel (just for the fun of it)
|
|
|
|
type Chan chan int
|
|
|
|
|
|
|
|
func ChanCreate() Chan {
|
2009-12-15 16:35:38 -07:00
|
|
|
c := make(Chan)
|
2009-02-05 16:56:31 -07:00
|
|
|
go func(c Chan) {
|
2009-10-06 20:41:51 -06:00
|
|
|
for x := 0; ; x++ {
|
2009-11-09 13:07:39 -07:00
|
|
|
c <- x
|
2009-02-05 16:56:31 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
}(c)
|
|
|
|
return c
|
2009-02-05 16:56:31 -07:00
|
|
|
}
|
|
|
|
|
2010-09-29 21:22:28 -06:00
|
|
|
func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
io.WriteString(w, fmt.Sprintf("channel send #%d\n", <-ch))
|
2009-02-05 16:56:31 -07:00
|
|
|
}
|
|
|
|
|
2009-06-08 15:03:21 -06:00
|
|
|
// exec a program, redirecting output
|
2010-09-29 21:22:28 -06:00
|
|
|
func DateServer(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.SetHeader("content-type", "text/plain; charset=utf-8")
|
2009-12-15 16:35:38 -07:00
|
|
|
r, w, err := os.Pipe()
|
2009-06-08 15:03:21 -06:00
|
|
|
if err != nil {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(rw, "pipe: %s\n", err)
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-06-08 15:03:21 -06:00
|
|
|
}
|
2011-02-03 20:41:26 -07:00
|
|
|
p, err := os.StartProcess("/bin/date", []string{"date"}, os.Environ(), "", []*os.File{nil, w, w})
|
2009-12-15 16:35:38 -07:00
|
|
|
defer r.Close()
|
|
|
|
w.Close()
|
2009-06-08 15:03:21 -06:00
|
|
|
if err != nil {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(rw, "fork/exec: %s\n", err)
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-06-08 15:03:21 -06:00
|
|
|
}
|
2011-02-03 20:41:26 -07:00
|
|
|
defer p.Release()
|
2010-09-29 21:22:28 -06:00
|
|
|
io.Copy(rw, r)
|
2011-02-03 20:41:26 -07:00
|
|
|
wait, err := p.Wait(0)
|
2009-06-08 15:03:21 -06:00
|
|
|
if err != nil {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(rw, "wait: %s\n", err)
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-06-08 15:03:21 -06:00
|
|
|
}
|
|
|
|
if !wait.Exited() || wait.ExitStatus() != 0 {
|
2010-09-29 21:22:28 -06:00
|
|
|
fmt.Fprintf(rw, "date: %v\n", wait)
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-06-08 15:03:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 21:22:28 -06:00
|
|
|
func Logger(w http.ResponseWriter, req *http.Request) {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print(req.URL.Raw)
|
2010-09-29 21:22:28 -06:00
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("oops"))
|
2010-06-08 18:51:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-02 12:21:11 -06:00
|
|
|
var webroot = flag.String("root", "/home/rsc", "web root directory")
|
|
|
|
|
2008-09-24 16:26:55 -06:00
|
|
|
func main() {
|
2009-12-15 16:35:38 -07:00
|
|
|
flag.Parse()
|
2009-05-04 16:14:22 -06:00
|
|
|
|
|
|
|
// The counter is published as a variable directly.
|
2009-12-15 16:35:38 -07:00
|
|
|
ctr := new(Counter)
|
|
|
|
http.Handle("/counter", ctr)
|
|
|
|
expvar.Publish("counter", ctr)
|
|
|
|
|
2010-06-08 18:51:57 -06:00
|
|
|
http.Handle("/", http.HandlerFunc(Logger))
|
2010-09-02 12:21:11 -06:00
|
|
|
http.Handle("/go/", http.FileServer(*webroot, "/go/"))
|
2009-12-15 16:35:38 -07:00
|
|
|
http.Handle("/flags", http.HandlerFunc(FlagServer))
|
|
|
|
http.Handle("/args", http.HandlerFunc(ArgServer))
|
|
|
|
http.Handle("/go/hello", http.HandlerFunc(HelloServer))
|
|
|
|
http.Handle("/chan", ChanCreate())
|
|
|
|
http.Handle("/date", http.HandlerFunc(DateServer))
|
|
|
|
err := http.ListenAndServe(":12345", nil)
|
2008-09-24 16:26:55 -06:00
|
|
|
if err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Panicln("ListenAndServe:", err)
|
2008-09-24 16:26:55 -06:00
|
|
|
}
|
|
|
|
}
|