From 992ccd98b30bde1a6a6802b489ef306664adc315 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Sat, 7 Sep 2019 09:48:31 -0500 Subject: [PATCH] basic webserver --- cmd/root.go | 2 +- cmd/run.go | 20 ++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ web/router.go | 31 +++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 cmd/run.go create mode 100644 web/router.go diff --git a/cmd/root.go b/cmd/root.go index 8dee4c8..297ccfb 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,7 +19,7 @@ var rootCmd = &cobra.Command{ func init() { cobra.OnInitialize(initConfig) - viper.SetDefault("port", 8000) + viper.SetDefault("addr", ":8000") viper.SetDefault("env", "production") rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Full path to config file") diff --git a/cmd/run.go b/cmd/run.go new file mode 100644 index 0000000..a5086bf --- /dev/null +++ b/cmd/run.go @@ -0,0 +1,20 @@ +package cmd + +import ( + "git.bloy.org/mike/gotest/web" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(runCmd) + runCmd.Flags().String("addr", ":8000", "Port to serve http") +} + +var runCmd = &cobra.Command{ + Use: "run", + Short: "Run the webserver", + Long: "Listen to the specified port and serve http until interrupted", + Run: func(cmd *cobra.Command, args []string) { + web.RunServer() + }, +} diff --git a/go.mod b/go.mod index 72a23ef..f355500 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.12 require ( github.com/gorilla/mux v1.7.3 + github.com/joho/godotenv v1.3.0 github.com/kyoh86/xdg v1.0.0 github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.3.2 diff --git a/go.sum b/go.sum index dd88a19..5653de9 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= diff --git a/web/router.go b/web/router.go new file mode 100644 index 0000000..e0353c5 --- /dev/null +++ b/web/router.go @@ -0,0 +1,31 @@ +package web + +import ( + "fmt" + "log" + "net/http" + + "github.com/gorilla/mux" + "github.com/spf13/viper" +) + +// Router creates the default http router +func Router() *mux.Router { + r := mux.NewRouter() + r.HandleFunc("/", root) + return r +} + +func root(resp http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/" { + http.NotFound(resp, req) + return + } + fmt.Fprintf(resp, "Hello World!") +} + +// RunServer runs the webserver with the router and middleware +func RunServer() { + router := Router() + log.Fatal(http.ListenAndServe(viper.GetString("addr"), router)) +}