Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
haoqu.ma
/
gotty
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ce96943a
authored
9 years ago
by
Iwasaki Yudai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create App instance with struct of options
1 parent
e09d6e04
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
16 deletions
app/app.go
main.go
app/app.go
View file @
ce96943
...
...
@@ -20,22 +20,21 @@ import (
)
type
App
struct
{
options
Options
}
type
Options
struct
{
Address
string
Port
string
PermitWrite
bool
RandomUrl
bool
Credential
string
RandomUrl
bool
Command
[]
string
}
func
New
(
address
string
,
port
string
,
permitWrite
bool
,
cred
string
,
randomUrl
bool
,
command
[]
string
)
*
App
{
func
New
(
options
Options
)
*
App
{
return
&
App
{
Address
:
address
,
Port
:
port
,
PermitWrite
:
permitWrite
,
Credential
:
cred
,
RandomUrl
:
randomUrl
,
Command
:
command
,
options
:
options
,
}
}
...
...
@@ -71,7 +70,7 @@ func basicAuthHandler(h http.Handler, cred string) http.Handler {
func
(
app
*
App
)
Run
()
error
{
path
:=
"/"
if
app
.
RandomUrl
{
if
app
.
options
.
RandomUrl
{
randomPath
:=
generateRandomString
(
8
)
path
=
"/"
+
randomPath
+
"/"
}
...
...
@@ -80,12 +79,12 @@ func (app *App) Run() error {
http
.
Handle
(
path
,
fs
)
http
.
HandleFunc
(
path
+
"ws"
,
app
.
generateHandler
())
endpoint
:=
app
.
Address
+
":"
+
app
.
Port
log
.
Printf
(
"Server is running at %s, command: %s"
,
endpoint
+
path
,
strings
.
Join
(
app
.
Command
,
" "
))
endpoint
:=
app
.
options
.
Address
+
":"
+
app
.
options
.
Port
log
.
Printf
(
"Server is running at %s, command: %s"
,
endpoint
+
path
,
strings
.
Join
(
app
.
options
.
Command
,
" "
))
handler
:=
http
.
Handler
(
http
.
DefaultServeMux
)
handler
=
loggerHandler
(
handler
)
if
app
.
Credential
!=
""
{
handler
=
basicAuthHandler
(
handler
,
app
.
Credential
)
if
app
.
options
.
Credential
!=
""
{
handler
=
basicAuthHandler
(
handler
,
app
.
options
.
Credential
)
}
err
:=
http
.
ListenAndServe
(
endpoint
,
handler
)
if
err
!=
nil
{
...
...
@@ -116,7 +115,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
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
)
log
.
Printf
(
"Command is running for client %s with PID %d"
,
r
.
RemoteAddr
,
cmd
.
Process
.
Pid
)
if
err
!=
nil
{
...
...
@@ -160,7 +159,7 @@ func (app *App) generateHandler() func(w http.ResponseWriter, r *http.Request) {
switch
data
[
0
]
{
case
'0'
:
if
!
app
.
PermitWrite
{
if
!
app
.
options
.
PermitWrite
{
break
}
...
...
This diff is collapsed.
Click to expand it.
main.go
View file @
ce96943
...
...
@@ -49,7 +49,16 @@ func main() {
cli
.
ShowAppHelp
(
c
)
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
()
if
err
!=
nil
{
fmt
.
Println
(
err
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment