Skip to content

Commit

Permalink
+cli. +tls.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicerobot committed Nov 27, 2016
1 parent 4c9a73d commit 44b1c52
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 123 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ build $(APP_NAME): $(SOURCE) ## Build opinionated
go build -ldflags "-X main.VERSION=$(COMMIT_TIME)-$(COMMIT_ID)" -o $(APP_NAME)


run: $(APP_NAME) ## Run opinionated
./$(APP_NAME)
run start: $(APP_NAME) ## Run opinionated
./$(APP_NAME) $@

cert: data/server.csr

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ See the [wiki](https://github.com/gomatic/opinionated/wiki) for design ideas and
- [ ] logging
- [ ] caching
- [ ] headers
- [ ] tls
- [x] tls
- [ ] tests
- [ ] auth
- [ ] facebook
Expand Down
146 changes: 129 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,139 @@
package main

import (
"crypto/tls"
stderr "log"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
"github.com/gomatic/opinionated/app"
op "github.com/gomatic/opinionated/app"
"github.com/urfave/cli"
)

func main() {
// Create service
service := goa.New("opinionated")

// Mount middleware
service.Use(middleware.RequestID())
service.Use(middleware.LogRequest(true))
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover())

// Mount "user" controller
c := NewUserController(service)
app.MountUserController(service, c)

// Start service
if err := service.ListenAndServe(":3080"); err != nil {
service.LogError("startup", "err", err)
app := cli.NewApp()
app.Name = "opinionated"
app.Usage = "Opinionated."
app.Version = MAJOR + "." + VERSION
app.EnableBashCompletion = true

Settings.Version = app.Version
Settings.Server = app.Name
Settings.Powered = app.Name + "/" + app.Version

app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, debugger, debugging, d",
Usage: "Enable debugging server.",
Destination: &Settings.Debugger,
},
}

app.Before = func(ctx *cli.Context) error {
if Settings.Debugger {
Debugger()
}
Settings.Mode = strings.ToLower(Settings.Mode)
if Settings.Addr == "" && Settings.Mode == "development" {
Settings.Addr = "localhost"
}
return nil
}

app.Commands = []cli.Command{
cli.Command{
Name: "start",
Aliases: []string{"server", "serve", "run"},
Usage: "Run.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "address, a",
Usage: "Bind to address.",
Value: "localhost",
Destination: &Settings.Addr,
},
cli.IntFlag{
Name: "port, p",
Usage: "Server port.",
Value: 3443,
Destination: &Settings.Port,
},
cli.BoolFlag{
Name: "secure, k",
Usage: "Enable TLS server.",
Destination: &Settings.Secure,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Enable verbose server.",
Destination: &Settings.Verbose,
},
cli.StringFlag{
Name: "mode",
Usage: "Provide a run-mode.",
EnvVar: "DEPLOYMENT",
Destination: &Settings.Mode,
},
},
Before: func(ctx *cli.Context) error {
Settings.Mode = strings.ToLower(Settings.Mode)
if Settings.Addr == "" && Settings.Mode == "development" {
Settings.Addr = "localhost"
}
return nil
},
Action: func(ctx *cli.Context) error {

// Create service
service := goa.New("opinionated")

// Mount middleware
service.Use(middleware.RequestID())
service.Use(middleware.LogRequest(true))
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover())

// Mount "user" controller
c := NewUserController(service)
op.MountUserController(service, c)

privPort := strconv.Itoa(Settings.Port)
pubPort := "80"
if Settings.Port != 443 || Settings.Port >= 3443 {
pubPort = strconv.Itoa(Settings.Port - 363)
}

addr := Settings.Addr + ":" + pubPort

if !Settings.Secure {

stderr.Printf("HTTP %s\n", addr)
return service.ListenAndServe(addr)

} else {
crt := filepath.Join(Settings.Program.Data, "server.crt")
key := filepath.Join(Settings.Program.Data, "server.key")

if _, err := tls.LoadX509KeyPair(crt, key); err != nil {
stderr.Println(err)

stderr.Printf("HTTP %s\n", addr)
return service.ListenAndServe(addr)

} else {

addr := Settings.Addr + ":" + privPort
stderr.Printf("HTTPS %s\n", addr)
return service.ListenAndServeTLS(addr, crt, key)
}
}
},
},
}

app.Run(os.Args)
}
Binary file modified opinionated
Binary file not shown.
6 changes: 3 additions & 3 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var Settings struct {
Verbose bool
Mode string

Addr string
Port int
Insecure bool
Addr string
Port int
Secure bool

Server string
Powered string
Expand Down
92 changes: 0 additions & 92 deletions start.go

This file was deleted.

8 changes: 0 additions & 8 deletions start_test.go

This file was deleted.

0 comments on commit 44b1c52

Please sign in to comment.