From 25b185a8db73055bc22bf6e774f85f8f090f396c Mon Sep 17 00:00:00 2001 From: Chetan Sarva Date: Thu, 28 Oct 2021 15:05:47 -0400 Subject: [PATCH] feat: added a simple hello world service for testing --- bin/vproxy/flags.go | 17 +++++++++++++++++ bin/vproxy/main.go | 4 ++++ hello.go | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 hello.go diff --git a/bin/vproxy/flags.go b/bin/vproxy/flags.go index d2f7ba6..e44eb96 100644 --- a/bin/vproxy/flags.go +++ b/bin/vproxy/flags.go @@ -136,6 +136,23 @@ func parseFlags() { }, }, }, + { + Name: "hello", + Usage: "Start a simple Hello World http service", + Action: startHello, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "host", + Value: "127.0.0.1", + Usage: "Host or IP to bind on", + }, + &cli.IntFlag{ + Name: "port", + Value: 8888, + Usage: "Port to listen on", + }, + }, + }, { Name: "version", Usage: "print the version", diff --git a/bin/vproxy/main.go b/bin/vproxy/main.go index 94d56d3..18e6e85 100644 --- a/bin/vproxy/main.go +++ b/bin/vproxy/main.go @@ -192,6 +192,10 @@ func uninstallCAROOT() error { return nil } +func startHello(c *cli.Context) error { + return vproxy.StartHello(c.String("host"), c.Int("port")) +} + func main() { parseFlags() } diff --git a/hello.go b/hello.go new file mode 100644 index 0000000..be9c447 --- /dev/null +++ b/hello.go @@ -0,0 +1,20 @@ +package vproxy + +import ( + "fmt" + "net/http" +) + +// StartHello world service on the given host/port +// +// This is a simple service which always responds with 'Hello World'. +// It's mainly here to serve as a simple demo of vproxy's abilities (see readme). +func StartHello(host string, port int) error { + fmt.Printf("~> starting vproxy hello service at http://%s:%d\n", host, port) + return http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), http.HandlerFunc(helloHandler)) +} + +func helloHandler(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + fmt.Fprintf(w, "

Hello World!

") +}