basic webserver
This commit is contained in:
parent
48ccd03be5
commit
992ccd98b3
@ -19,7 +19,7 @@ var rootCmd = &cobra.Command{
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
viper.SetDefault("port", 8000)
|
viper.SetDefault("addr", ":8000")
|
||||||
viper.SetDefault("env", "production")
|
viper.SetDefault("env", "production")
|
||||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c",
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c",
|
||||||
"", "Full path to config file")
|
"", "Full path to config file")
|
||||||
|
|||||||
20
cmd/run.go
Normal file
20
cmd/run.go
Normal file
@ -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()
|
||||||
|
},
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.12
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/mux v1.7.3
|
github.com/gorilla/mux v1.7.3
|
||||||
|
github.com/joho/godotenv v1.3.0
|
||||||
github.com/kyoh86/xdg v1.0.0
|
github.com/kyoh86/xdg v1.0.0
|
||||||
github.com/spf13/cobra v0.0.5
|
github.com/spf13/cobra v0.0.5
|
||||||
github.com/spf13/viper v1.3.2
|
github.com/spf13/viper v1.3.2
|
||||||
|
|||||||
2
go.sum
2
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/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 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
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 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
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=
|
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||||
|
|||||||
31
web/router.go
Normal file
31
web/router.go
Normal file
@ -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))
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user