Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Go Httpbin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
24f38176
Commit
24f38176
authored
6 years ago
by
saurabh hirani
Committed by
Will McCutchen
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
adding support for -host, -https-cert-file, -https-key-file (#14)
parent
c5cb2f48
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
README.md
+25
-2
25 additions, 2 deletions
README.md
cmd/go-httpbin/main.go
+42
-6
42 additions, 6 deletions
cmd/go-httpbin/main.go
with
67 additions
and
8 deletions
README.md
+
25
−
2
View file @
24f38176
...
...
@@ -16,18 +16,41 @@ variables:
```
$ go-httpbin -help
Usage of go-httpbin:
-host string
Host to listen on (default "0.0.0.0")
-port int
Port to listen on (default 8080)
-https-cert-file string
HTTPS certificate file
-https-key-file string
HTTPS private key file
-max-body-size int
Maximum size of request or response, in bytes (default 1048576)
-max-duration duration
Maximum duration a response may take (default 10s)
-port int
Port to listen on (default 8080)
Examples:
# Run http server
$ go-httpbin -host 127.0.0.1 -port 8081
# Run https server
# Generate .crt and .key files
$ openssl genrsa -out server.key 2048
$ openssl ecparam -genkey -name secp384r1 -out server.key
$ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
$ go-httpbin -host 127.0.0.1 -port 8081 -https-cert-file ./server.crt -https-key-file ./server.key
```
Docker images are published to
[
Docker Hub
][
docker-hub
]
:
```
# Run http server
$ docker run -P mccutchen/go-httpbin
# Run https server
$ docker run -e HTTPS_CERT_FILE='/tmp/server.crt' -e HTTPS_KEY_FILE='/tmp/server.key' -p 8080:8080 -v /tmp:/tmp mccutchen/go-httpbin
```
The
`github.com/mccutchen/go-httpbin/httpbin`
package can also be used as a
...
...
This diff is collapsed.
Click to expand it.
cmd/go-httpbin/main.go
+
42
−
6
View file @
24f38176
package
main
import
(
"crypto/tls"
"flag"
"fmt"
"log"
...
...
@@ -13,16 +14,23 @@ import (
"github.com/mccutchen/go-httpbin/httpbin"
)
const
defaultHost
=
"0.0.0.0"
const
defaultPort
=
8080
var
(
port
int
maxBodySize
int64
maxDuration
time
.
Duration
host
string
port
int
maxBodySize
int64
maxDuration
time
.
Duration
httpsCertFile
string
httpsKeyFile
string
)
func
main
()
{
flag
.
StringVar
(
&
host
,
"host"
,
defaultHost
,
"Host to listen on"
)
flag
.
IntVar
(
&
port
,
"port"
,
defaultPort
,
"Port to listen on"
)
flag
.
StringVar
(
&
httpsCertFile
,
"https-cert-file"
,
""
,
"HTTPS Server certificate file"
)
flag
.
StringVar
(
&
httpsKeyFile
,
"https-key-file"
,
""
,
"HTTPS Server private key file"
)
flag
.
Int64Var
(
&
maxBodySize
,
"max-body-size"
,
httpbin
.
DefaultMaxBodySize
,
"Maximum size of request or response, in bytes"
)
flag
.
DurationVar
(
&
maxDuration
,
"max-duration"
,
httpbin
.
DefaultMaxDuration
,
"Maximum duration a response may take"
)
flag
.
Parse
()
...
...
@@ -47,6 +55,9 @@ func main() {
os
.
Exit
(
1
)
}
}
if
host
==
defaultHost
&&
os
.
Getenv
(
"HOST"
)
!=
""
{
host
=
os
.
Getenv
(
"HOST"
)
}
if
port
==
defaultPort
&&
os
.
Getenv
(
"PORT"
)
!=
""
{
port
,
err
=
strconv
.
Atoi
(
os
.
Getenv
(
"PORT"
))
if
err
!=
nil
{
...
...
@@ -56,6 +67,13 @@ func main() {
}
}
if
httpsCertFile
==
""
&&
os
.
Getenv
(
"HTTPS_CERT_FILE"
)
!=
""
{
httpsCertFile
=
os
.
Getenv
(
"HTTPS_CERT_FILE"
)
}
if
httpsKeyFile
==
""
&&
os
.
Getenv
(
"HTTPS_KEY_FILE"
)
!=
""
{
httpsKeyFile
=
os
.
Getenv
(
"HTTPS_KEY_FILE"
)
}
logger
:=
log
.
New
(
os
.
Stderr
,
""
,
0
)
h
:=
httpbin
.
New
(
...
...
@@ -64,7 +82,25 @@ func main() {
httpbin
.
WithObserver
(
httpbin
.
StdLogObserver
(
logger
)),
)
listenAddr
:=
net
.
JoinHostPort
(
"0.0.0.0"
,
strconv
.
Itoa
(
port
))
logger
.
Printf
(
"go-httpbin listening on %s"
,
listenAddr
)
http
.
ListenAndServe
(
listenAddr
,
h
.
Handler
())
listenAddr
:=
net
.
JoinHostPort
(
host
,
strconv
.
Itoa
(
port
))
server
:=
&
http
.
Server
{
Addr
:
listenAddr
,
Handler
:
h
.
Handler
(),
}
if
httpsCertFile
!=
""
&&
httpsKeyFile
!=
""
{
cert
,
err
:=
tls
.
LoadX509KeyPair
(
httpsCertFile
,
httpsKeyFile
)
if
err
!=
nil
{
log
.
Fatal
(
"Failed to generate https key pair: "
,
err
)
}
server
.
TLSConfig
=
&
tls
.
Config
{
Certificates
:
[]
tls
.
Certificate
{
cert
},
}
logger
.
Printf
(
"go-httpbin listening on https://%s"
,
listenAddr
)
server
.
ListenAndServeTLS
(
""
,
""
)
}
else
{
logger
.
Printf
(
"go-httpbin listening on http://%s"
,
listenAddr
)
server
.
ListenAndServe
()
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment