Show command and hostname in windows title

This commit is contained in:
Iwasaki Yudai 2015-08-23 14:04:31 +09:00
parent 5ee6356242
commit 67b54b7f20
6 changed files with 113 additions and 53 deletions

View file

@ -1,19 +1,23 @@
package app
import (
"bytes"
"crypto/rand"
"encoding/base64"
htemplate "html/template"
"log"
"math/big"
"net"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
ttemplate "text/template"
"github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket"
"github.com/kr/pty"
"net"
)
type App struct {
@ -28,6 +32,7 @@ type Options struct {
PermitWrite bool
Credential string
RandomUrl bool
TitleFormat string
Command []string
}
@ -44,21 +49,23 @@ func New(options Options) *App {
}
func (app *App) Run() error {
path := "/"
path := ""
if app.options.RandomUrl {
path += generateRandomString(8)
path += "/" + generateRandomString(8)
}
endpoint := app.options.Address + ":" + app.options.Port
staticHandler := http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "bindata"},
)
indexHandler := http.HandlerFunc(app.handleIndex)
wsHandler := http.HandlerFunc(app.handleWS)
staticHandler := http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"},
)
var siteMux = http.NewServeMux()
siteMux.Handle(path, staticHandler)
siteMux.Handle(path+"ws", wsHandler)
siteMux.Handle(path+"/", indexHandler)
siteMux.Handle(path+"/static/", http.StripPrefix(path+"/static/", staticHandler))
siteMux.Handle(path+"/ws", wsHandler)
siteHandler := http.Handler(siteMux)
@ -74,10 +81,10 @@ func (app *App) Run() error {
strings.Join(app.options.Command, " "),
)
if app.options.Address != "" {
log.Printf("URL: %s", "http://"+endpoint+path)
log.Printf("URL: %s", "http://"+endpoint+path+"/")
} else {
for _, address := range listAddresses() {
log.Printf("URL: %s", "http://"+address+":"+app.options.Port+path)
log.Printf("URL: %s", "http://"+address+":"+app.options.Port+path+"/")
}
}
if err := http.ListenAndServe(endpoint, siteHandler); err != nil {
@ -87,6 +94,36 @@ func (app *App) Run() error {
return nil
}
type TitleVars struct {
Command string
Hostname string
}
type IndexVars struct {
Title string
}
func (app *App) handleIndex(w http.ResponseWriter, r *http.Request) {
title := make([]byte, 0)
titleBuf := bytes.NewBuffer(title)
hostname, _ := os.Hostname()
titleVars := TitleVars{
Command: strings.Join(app.options.Command, " "),
Hostname: hostname,
}
titleTmpl, _ := ttemplate.New("title").Parse(app.options.TitleFormat)
titleTmpl.Execute(titleBuf, titleVars)
data, _ := Asset("templates/index.html")
tmpl, _ := htemplate.New("index").Parse(string(data))
vars := IndexVars{
Title: titleBuf.String(),
}
tmpl.Execute(w, vars)
}
func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
log.Printf("New client connected: %s", r.RemoteAddr)

File diff suppressed because one or more lines are too long