Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
msoap committed Oct 23, 2021
1 parent a87cbea commit 5546048
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func getConfig() (*Config, error) {

flag.StringVar(&logFilename, "log", "", "log `filename`, default - STDOUT")
flag.BoolVar(&noLogTimestamp, "no-log-timestamp", false, "log output without timestamps")
flag.IntVar(&cfg.port, "port", PORT, "`port` for http server")
flag.IntVar(&cfg.port, "port", defaultPort, "`port` for http server")
flag.StringVar(&cfg.host, "host", "", "`host` for http server")
flag.BoolVar(&cfg.setCGI, "cgi", false, "run scripts in CGI-mode")
flag.StringVar(&cfg.exportVars, "export-vars", "", "export environment vars (\"VAR1,VAR2,...\")")
Expand Down
30 changes: 15 additions & 15 deletions shell2http.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
var version = "dev"

const (
// PORT - default port for http-server
PORT = 8080
// defaultPort - default port for http-server
defaultPort = 8080

// shBasicAuthVar - name of env var for basic auth credentials
shBasicAuthVar = "SH_BASIC_AUTH"
Expand All @@ -46,8 +46,8 @@ const (
maxMemoryForUploadFile = 65536
)

// INDEXHTML - Template for index page
const INDEXHTML = `<!DOCTYPE html>
// indexTmpl - template for index page
const indexTmpl = `<!DOCTYPE html>
<!-- Served by shell2http/%s -->
<html>
<head>
Expand Down Expand Up @@ -75,17 +75,17 @@ const INDEXHTML = `<!DOCTYPE html>
</html>
`

// Command - one command type
type Command struct {
// command - one command
type command struct {
path string
cmd string
httpMethod string
handler http.HandlerFunc
}

// parsePathAndCommands - get all commands with pathes
func parsePathAndCommands(args []string) ([]Command, error) {
var cmdHandlers []Command
func parsePathAndCommands(args []string) ([]command, error) {
var cmdHandlers []command

if len(args) < 2 || len(args)%2 == 1 {
return cmdHandlers, fmt.Errorf("requires a pair of path and shell command")
Expand All @@ -104,7 +104,7 @@ func parsePathAndCommands(args []string) ([]Command, error) {
if len(pathParts) != 3 {
return nil, fmt.Errorf("the path %q must begin with the prefix /, and with optional METHOD: prefix", path)
}
cmdHandlers = append(cmdHandlers, Command{path: pathParts[2], cmd: cmd, httpMethod: pathParts[1]})
cmdHandlers = append(cmdHandlers, command{path: pathParts[2], cmd: cmd, httpMethod: pathParts[1]})

uniqPaths[path] = true
}
Expand Down Expand Up @@ -264,8 +264,8 @@ func execShellCommand(appConfig Config, shell string, params []string, req *http
}

// setupHandlers - setup http handlers
func setupHandlers(cmdHandlers []Command, appConfig Config, cacheTTL raphanus.DB) ([]Command, error) {
resultHandlers := []Command{}
func setupHandlers(cmdHandlers []command, appConfig Config, cacheTTL raphanus.DB) ([]command, error) {
resultHandlers := []command{}
indexLiHTML := ""
existsRootPath := false

Expand Down Expand Up @@ -301,7 +301,7 @@ func setupHandlers(cmdHandlers []Command, appConfig Config, cacheTTL raphanus.DB
if err != nil {
return nil, err
}
resultHandlers = append(resultHandlers, Command{
resultHandlers = append(resultHandlers, command{
path: path,
handler: handler,
cmd: strings.Join(cmdsForLog[path], "; "),
Expand All @@ -310,7 +310,7 @@ func setupHandlers(cmdHandlers []Command, appConfig Config, cacheTTL raphanus.DB

// --------------
if appConfig.addExit {
resultHandlers = append(resultHandlers, Command{
resultHandlers = append(resultHandlers, command{
path: "/exit",
cmd: "/exit",
handler: func(rw http.ResponseWriter, _ *http.Request) {
Expand All @@ -324,8 +324,8 @@ func setupHandlers(cmdHandlers []Command, appConfig Config, cacheTTL raphanus.DB

// --------------
if !appConfig.noIndex && !existsRootPath {
indexHTML := fmt.Sprintf(INDEXHTML, version, indexLiHTML)
resultHandlers = append(resultHandlers, Command{
indexHTML := fmt.Sprintf(indexTmpl, version, indexLiHTML)
resultHandlers = append(resultHandlers, command{
path: "/",
cmd: "index page",
handler: func(rw http.ResponseWriter, req *http.Request) {
Expand Down
8 changes: 4 additions & 4 deletions shell2http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func Test_parsePathAndCommands(t *testing.T) {
tests := []struct {
name string
args []string
want []Command
want []command
wantErr bool
}{
{
Expand Down Expand Up @@ -355,19 +355,19 @@ func Test_parsePathAndCommands(t *testing.T) {
{
name: "two arg",
args: []string{"/date", "date"},
want: []Command{{path: "/date", cmd: "date"}},
want: []command{{path: "/date", cmd: "date"}},
wantErr: false,
},
{
name: "four arg",
args: []string{"/date", "date", "/", "echo index"},
want: []Command{{path: "/date", cmd: "date"}, {path: "/", cmd: "echo index"}},
want: []command{{path: "/date", cmd: "date"}, {path: "/", cmd: "echo index"}},
wantErr: false,
},
{
name: "with http method",
args: []string{"POST:/date", "date", "GET:/", "echo index"},
want: []Command{{path: "/date", cmd: "date", httpMethod: "POST"}, {path: "/", cmd: "echo index", httpMethod: "GET"}},
want: []command{{path: "/date", cmd: "date", httpMethod: "POST"}, {path: "/", cmd: "echo index", httpMethod: "GET"}},
wantErr: false,
},
{
Expand Down

0 comments on commit 5546048

Please sign in to comment.