Create App instance with struct of options

This commit is contained in:
Iwasaki Yudai 2015-08-21 12:48:07 +09:00
parent e09d6e0486
commit ce96943af2
2 changed files with 24 additions and 16 deletions

View File

@ -20,22 +20,21 @@ import (
) )
type App struct { type App struct {
options Options
}
type Options struct {
Address string Address string
Port string Port string
PermitWrite bool PermitWrite bool
RandomUrl bool
Credential string Credential string
RandomUrl bool
Command []string Command []string
} }
func New(address string, port string, permitWrite bool, cred string, randomUrl bool, command []string) *App { func New(options Options) *App {
return &App{ return &App{
Address: address, options: options,
Port: port,
PermitWrite: permitWrite,
Credential: cred,
RandomUrl: randomUrl,
Command: command,
} }
} }
@ -71,7 +70,7 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
func (app *App) Run() error { func (app *App) Run() error {
path := "/" path := "/"
if app.RandomUrl { if app.options.RandomUrl {
randomPath := generateRandomString(8) randomPath := generateRandomString(8)
path = "/" + randomPath + "/" path = "/" + randomPath + "/"
} }
@ -80,12 +79,12 @@ func (app *App) Run() error {
http.Handle(path, fs) http.Handle(path, fs)
http.HandleFunc(path+"ws", app.generateHandler()) http.HandleFunc(path+"ws", app.generateHandler())
endpoint := app.Address + ":" + app.Port endpoint := app.options.Address + ":" + app.options.Port
log.Printf("Server is running at %s, command: %s", endpoint+path, strings.Join(app.Command, " ")) log.Printf("Server is running at %s, command: %s", endpoint+path, strings.Join(app.options.Command, " "))
handler := http.Handler(http.DefaultServeMux) handler := http.Handler(http.DefaultServeMux)
handler = loggerHandler(handler) handler = loggerHandler(handler)
if app.Credential != "" { if app.options.Credential != "" {
handler = basicAuthHandler(handler, app.Credential) handler = basicAuthHandler(handler, app.options.Credential)
} }
err := http.ListenAndServe(endpoint, handler) err := http.ListenAndServe(endpoint, handler)
if err != nil { if err != nil {
@ -116,7 +115,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
return return
} }
cmd := exec.Command(app.Command[0], app.Command[1:]...) cmd := exec.Command(app.options.Command[0], app.options.Command[1:]...)
fio, err := pty.Start(cmd) fio, err := pty.Start(cmd)
log.Printf("Command is running for client %s with PID %d", r.RemoteAddr, cmd.Process.Pid) log.Printf("Command is running for client %s with PID %d", r.RemoteAddr, cmd.Process.Pid)
if err != nil { if err != nil {
@ -160,7 +159,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
switch data[0] { switch data[0] {
case '0': case '0':
if !app.PermitWrite { if !app.options.PermitWrite {
break break
} }

11
main.go
View File

@ -49,7 +49,16 @@ func main() {
cli.ShowAppHelp(c) cli.ShowAppHelp(c)
os.Exit(1) os.Exit(1)
} }
app := app.New(c.String("addr"), c.String("port"), c.Bool("permit-write"), c.String("credential"), c.Bool("random-url"), c.Args()) app := app.New(
app.Options{
c.String("addr"),
c.String("port"),
c.Bool("permit-write"),
c.String("credential"),
c.Bool("random-url"),
c.Args(),
},
)
err := app.Run() err := app.Run()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)