cmd/construct: add HTTP test server
This commit is contained in:
parent
5c8881fc18
commit
5e4f1618a4
|
@ -11,7 +11,7 @@
|
|||
branch = "master"
|
||||
name = "github.com/Xe/eclier"
|
||||
packages = ["."]
|
||||
revision = "f56dc59db6b1cf87ae6bbaaf6aa985a285da33a1"
|
||||
revision = "3cde6c5f47044f4875c4b7fe6b12e4e6000608ea"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/kr/pretty"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
hits *atomic.Int64
|
||||
)
|
||||
|
||||
func init() {
|
||||
hits = atomic.NewInt64(0)
|
||||
}
|
||||
|
||||
func demoServerHandler(msg string) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Route is go!")
|
||||
fmt.Fprintf(w, "%s\n", pretty.Sprintf("%s", r.Header))
|
||||
hn, _ := os.Hostname()
|
||||
fmt.Fprintf(w, "message: %s\n", msg)
|
||||
fmt.Fprintf(w, "Served by %s running %s\n", hn, runtime.GOOS)
|
||||
fmt.Fprintf(w, "Hit count: %d", hits.Inc())
|
||||
|
||||
ip := r.Header.Get("X-Remote-Ip")
|
||||
if ip != "" {
|
||||
log.Printf("Hit from %s: %s", ip, r.RequestURI)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func demoServer(ctx context.Context, args []string) error {
|
||||
fs := flag.NewFlagSet("server", flag.ContinueOnError)
|
||||
|
||||
addr := fs.String("addr", ":9090", "http address to listen on")
|
||||
msg := fs.String("msg", "now here's a little lesson in trickery...", "custom message to add to each page render")
|
||||
|
||||
err := fs.Parse(args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hs := &http.Server{
|
||||
Addr: *addr,
|
||||
Handler: demoServerHandler(*msg),
|
||||
}
|
||||
|
||||
go hs.ListenAndServe()
|
||||
|
||||
log.Printf("listening on %s", *addr)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
sctx := context.Background()
|
||||
hs.Shutdown(sctx)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
|
@ -98,6 +98,8 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
r.AddCommand(eclier.NewBuiltinCommand("server", "spawns a http server for testing", "[-addr host:port|-msg \"some message\"]", demoServer))
|
||||
|
||||
r.Run(ctx, flag.Args())
|
||||
}
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ func NewRouter(opts ...RouterOption) (*Router, error) {
|
|||
}
|
||||
}
|
||||
|
||||
var helpCommand Command = NewBuiltinCommand("help", "shows help for subcommands", "[subcommand]", func(ctx context.Context, arg []string) error {
|
||||
var helpCommand Command = NewBuiltinCommand("help", "shows help for subcommands", "help [subcommand]", func(ctx context.Context, arg []string) error {
|
||||
if len(arg) == 0 {
|
||||
table := tablewriter.NewWriter(os.Stdout)
|
||||
table.SetHeader([]string{"Verb", "Author", "Version", "Help"})
|
||||
|
@ -97,6 +97,14 @@ func NewRouter(opts ...RouterOption) (*Router, error) {
|
|||
return r, nil
|
||||
}
|
||||
|
||||
// AddCommand adds a given command instance to the eclier router.
|
||||
func (r *Router) AddCommand(cmd Command) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
r.cmds[cmd.Verb()] = cmd
|
||||
}
|
||||
|
||||
// Run executes a single command given in slot 0 of the argument array.
|
||||
func (r *Router) Run(ctx context.Context, arg []string) error {
|
||||
r.lock.Lock()
|
||||
|
|
Loading…
Reference in New Issue