From c6d21d54d293ad3ebebd6d8e097b966f58387f3c Mon Sep 17 00:00:00 2001 From: nicerobot Date: Sun, 24 Jul 2016 18:10:46 -0700 Subject: [PATCH] +development localhost. +cli flags. --- README.md | 2 ++ app.go | 44 ++++++++++++++++++++++++++++------ vendor/application/routes.go | 30 ++++++++++++----------- vendor/application/settings.go | 5 +++- vendor/application/start.go | 11 +++++---- 5 files changed, 66 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e269a0b..09267f0 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ See the [wiki](https://github.com/gomatic/opinionated/wiki) for design ideas and - [x] logging - [x] caching - [x] headers +- [ ] tests - [ ] auth - [ ] facebook - [ ] google @@ -28,6 +29,7 @@ See the [wiki](https://github.com/gomatic/opinionated/wiki) for design ideas and ### Client - [ ] landing page +- [ ] tests - [ ] auth - [ ] facebook - [ ] google diff --git a/app.go b/app.go index 5fc2caa..761903d 100644 --- a/app.go +++ b/app.go @@ -2,8 +2,8 @@ package main import ( _ "expvar" - "log" "os" + "strings" "application" @@ -40,6 +40,10 @@ func main() { if application.Settings.Debugger { application.Debugger() } + application.Settings.Mode = strings.ToLower(application.Settings.Mode) + if application.Settings.Addr == "" && application.Settings.Mode == "development" { + application.Settings.Addr = "localhost" + } return nil } @@ -49,8 +53,7 @@ func main() { Aliases: []string{"configure", "config"}, Usage: "Configure.", Action: func(ctx *cli.Context) error { - log.Println(application.Configure()) - return nil + return application.Configure() }, }, cli.Command{ @@ -58,8 +61,7 @@ func main() { Aliases: []string{"manage", "administer", "admin"}, Usage: "Administer.", Action: func(ctx *cli.Context) error { - log.Println(application.Manage()) - return nil + return application.Manage() }, }, cli.Command{ @@ -67,17 +69,45 @@ func main() { Aliases: []string{"server", "serve", "run"}, Usage: "Run.", Flags: []cli.Flag{ + cli.StringFlag{ + Name: "address, a", + Usage: "Bind to address.", + Value: "", + Destination: &application.Settings.Addr, + }, cli.IntFlag{ Name: "port, p", Usage: "Server port.", Value: 3000, Destination: &application.Settings.Port, }, + cli.BoolFlag{ + Name: "insecure, k", + Usage: "Disable TLS server.", + Destination: &application.Settings.Insecure, + }, + cli.BoolFlag{ + Name: "verbose, v", + Usage: "Enable verbose server.", + Destination: &application.Settings.Verbose, + }, + cli.StringFlag{ + Name: "mode", + Usage: "Provide a run-mode.", + EnvVar: "DEPLOYMENT", + Destination: &application.Settings.Mode, + }, }, - Action: func(ctx *cli.Context) error { - log.Println(application.Start()) + Before: func(ctx *cli.Context) error { + application.Settings.Mode = strings.ToLower(application.Settings.Mode) + if application.Settings.Addr == "" && application.Settings.Mode == "development" { + application.Settings.Addr = "localhost" + } return nil }, + Action: func(ctx *cli.Context) error { + return application.Start() + }, }, } diff --git a/vendor/application/routes.go b/vendor/application/routes.go index 51a9931..0f970ef 100644 --- a/vendor/application/routes.go +++ b/vendor/application/routes.go @@ -35,7 +35,7 @@ func routeServices() *mux.Router { func routeLoginService(ctx context.Context, r *mux.Router) { logged := logging.New("login").Middleware() - servered := server.New("opinionated/auth", "gomatic/opinionated/auth") + servered := server.New(Settings.Server+"/auth", Settings.Powered+"/auth") json := content.New("application/json") loginService := login.New() @@ -55,16 +55,12 @@ func routeLoginService(ctx context.Context, r *mux.Router) { func routeTestService(ctx context.Context, r *mux.Router) { logged := logging.New("test").Middleware() - servered := server.New("opinionated", "gomatic/opinionated") + servered := server.New(Settings.Server, Settings.Powered) json := content.New("application/json") testService := testing.New() // POST / - // GET /:id - // PUT /:id - // PATCH /:id - // DELETE /:id r.Methods("POST").Path("/").Handler(httptransport.NewServer( ctx, @@ -72,30 +68,36 @@ func routeTestService(ctx context.Context, r *mux.Router) { testService.DecodePost, servered(json(testService.EncodePost)), )).Name("POST") - r.Methods("POST").Path("/{id}").Handler(httptransport.NewServer( - ctx, - logged(testService.Get()), - testService.DecodeGet, - servered(json(testService.EncodeGet)), - )).Name("POST Id") - r.Methods("GET").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer( + + // GET /:id + + r.Methods("POST", "GET").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer( ctx, logged(testService.Get()), testService.DecodeGet, servered(json(testService.EncodeGet)), - ))).Name("GET Id") + ))).Name("GET/POST Id") + + // PUT /:id + r.Methods("PUT").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer( ctx, logged(testService.Put()), testService.DecodePut, servered(json(testService.EncodePut)), ))).Name("PUT Id") + + // PATCH /:id + r.Methods("PATCH").Path("/{id}").Handler(httptransport.NewServer( ctx, logged(testService.Patch()), testService.DecodePatch, servered(json(testService.EncodePatch)), )).Name("PATCH Id") + + // DELETE /:id + r.Methods("DELETE").Path("/{id}").Handler(caching.New(-1, httptransport.NewServer( ctx, logged(testService.Delete()), diff --git a/vendor/application/settings.go b/vendor/application/settings.go index 61a349f..677a36b 100644 --- a/vendor/application/settings.go +++ b/vendor/application/settings.go @@ -5,9 +5,12 @@ import "net/url" // var Settings struct { Debugger bool + Verbose bool Mode string - Port int + Addr string + Port int + Insecure bool Server string Powered string diff --git a/vendor/application/start.go b/vendor/application/start.go index 29e1952..1b9d86c 100644 --- a/vendor/application/start.go +++ b/vendor/application/start.go @@ -22,17 +22,20 @@ func Start() error { // Start the server - port := strconv.Itoa(Settings.Port) srv := &http.Server{ - Addr: "localhost:" + port, + Addr: Settings.Addr + ":" + strconv.Itoa(Settings.Port), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, Handler: mux, } - fmt.Println("listening on " + port) + fmt.Println("listening on " + srv.Addr) - if cert, err := tls.LoadX509KeyPair("server.crt", "server.key"); err != nil { + if Settings.Insecure { + + return srv.ListenAndServe() + + } else if cert, err := tls.LoadX509KeyPair("server.crt", "server.key"); err != nil { stderr.Println(err) return srv.ListenAndServe()