Skip to content

Commit

Permalink
+development localhost. +cli flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicerobot committed Jul 25, 2016
1 parent 961355c commit c6d21d5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,7 @@ See the [wiki](https://github.com/gomatic/opinionated/wiki) for design ideas and
### Client

- [ ] landing page
- [ ] tests
- [ ] auth
- [ ] facebook
- [ ] google
Expand Down
44 changes: 37 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
_ "expvar"
"log"
"os"
"strings"

"application"

Expand Down Expand Up @@ -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
}

Expand All @@ -49,35 +53,61 @@ 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{
Name: "manager",
Aliases: []string{"manage", "administer", "admin"},
Usage: "Administer.",
Action: func(ctx *cli.Context) error {
log.Println(application.Manage())
return nil
return application.Manage()
},
},
cli.Command{
Name: "start",
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()
},
},
}

Expand Down
30 changes: 16 additions & 14 deletions vendor/application/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -55,47 +55,49 @@ 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,
logged(testService.Post()),
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()),
Expand Down
5 changes: 4 additions & 1 deletion vendor/application/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions vendor/application/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c6d21d5

Please sign in to comment.