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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Nix
Go Httpbin
Commits
40ab5eb0
Unverified
Commit
40ab5eb0
authored
5 years ago
by
Will McCutchen
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #26 from mccutchen/graceful-shutdown
Shut down gracefully
parents
ee985a5e
5b482296
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.travis.yml
+1
-1
1 addition, 1 deletion
.travis.yml
Dockerfile
+1
-1
1 addition, 1 deletion
Dockerfile
Makefile
+4
-3
4 additions, 3 deletions
Makefile
cmd/maincmd/main.go
+59
-17
59 additions, 17 deletions
cmd/maincmd/main.go
with
65 additions
and
22 deletions
.travis.yml
+
1
−
1
View file @
40ab5eb0
---
language
:
go
go
:
-
'
1.10'
-
'
1.11'
-
'
1.12'
-
'
1.13'
script
:
-
make lint
...
...
This diff is collapsed.
Click to expand it.
Dockerfile
+
1
−
1
View file @
40ab5eb0
FROM
golang:1.1
1
FROM
golang:1.1
3
WORKDIR
/go/src/github.com/mccutchen/go-httpbin
...
...
This diff is collapsed.
Click to expand it.
Makefile
+
4
−
3
View file @
40ab5eb0
...
...
@@ -4,8 +4,9 @@
# go-httpbin:latest image use `make imagepush VERSION=latest)`
VERSION
?=
$(
shell git rev-parse
--short
HEAD
)
# Override th
i
s to deploy to a different App Engine project
# Override th
ese value
s to deploy to a different App Engine project
GCLOUD_PROJECT
?=
httpbingo
GCLOUD_ACCOUNT
?=
mccutchen@gmail.com
# Built binaries will be placed here
DIST_PATH
?=
dist
...
...
@@ -72,10 +73,10 @@ lint: $(GOLINT)
# deploy & run locally
# =============================================================================
deploy
:
build
gcloud app deploy
--quiet
--project
=
$(
GCLOUD_PROJECT
)
--version
=
$(
VERSION
)
--promote
gcloud
--account
=
$(
GCLOUD_ACCOUNT
)
app deploy
--quiet
--project
=
$(
GCLOUD_PROJECT
)
--version
=
$(
VERSION
)
--promote
stagedeploy
:
build
gcloud app deploy
--quiet
--project
=
$(
GCLOUD_PROJECT
)
--version
=
$(
VERSION
)
--no-promote
gcloud
--account
=
$(
GCLOUD_ACCOUNT
)
app deploy
--quiet
--project
=
$(
GCLOUD_PROJECT
)
--version
=
$(
VERSION
)
--no-promote
run
:
build
$(
DIST_PATH
)
/go-httpbin
...
...
This diff is collapsed.
Click to expand it.
cmd/maincmd/main.go
+
59
−
17
View file @
40ab5eb0
package
maincmd
import
(
"c
rypto/tls
"
"c
ontext
"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/mccutchen/go-httpbin/httpbin"
...
...
@@ -43,7 +45,7 @@ func Main() {
if
maxBodySize
==
httpbin
.
DefaultMaxBodySize
&&
os
.
Getenv
(
"MAX_BODY_SIZE"
)
!=
""
{
maxBodySize
,
err
=
strconv
.
ParseInt
(
os
.
Getenv
(
"MAX_BODY_SIZE"
),
10
,
64
)
if
err
!=
nil
{
fmt
.
P
rintf
(
"
invalid value %#v for env var MAX_BODY_SIZE: %s
\n
"
,
os
.
Getenv
(
"MAX_BODY_SIZE"
),
err
)
fmt
.
Fp
rintf
(
os
.
Stderr
,
"Error:
invalid value %#v for env var MAX_BODY_SIZE: %s
\n
\n
"
,
os
.
Getenv
(
"MAX_BODY_SIZE"
),
err
)
flag
.
Usage
()
os
.
Exit
(
1
)
}
...
...
@@ -51,7 +53,7 @@ func Main() {
if
maxDuration
==
httpbin
.
DefaultMaxDuration
&&
os
.
Getenv
(
"MAX_DURATION"
)
!=
""
{
maxDuration
,
err
=
time
.
ParseDuration
(
os
.
Getenv
(
"MAX_DURATION"
))
if
err
!=
nil
{
fmt
.
P
rintf
(
"
invalid value %#v for env var MAX_DURATION: %s
\n
"
,
os
.
Getenv
(
"MAX_DURATION"
),
err
)
fmt
.
Fp
rintf
(
os
.
Stderr
,
"Error:
invalid value %#v for env var MAX_DURATION: %s
\n
\n
"
,
os
.
Getenv
(
"MAX_DURATION"
),
err
)
flag
.
Usage
()
os
.
Exit
(
1
)
}
...
...
@@ -62,7 +64,7 @@ func Main() {
if
port
==
defaultPort
&&
os
.
Getenv
(
"PORT"
)
!=
""
{
port
,
err
=
strconv
.
Atoi
(
os
.
Getenv
(
"PORT"
))
if
err
!=
nil
{
fmt
.
P
rintf
(
"
invalid value %#v for env var PORT: %s
\n
"
,
os
.
Getenv
(
"PORT"
),
err
)
fmt
.
Fp
rintf
(
os
.
Stderr
,
"Error:
invalid value %#v for env var PORT: %s
\n
\n
"
,
os
.
Getenv
(
"PORT"
),
err
)
flag
.
Usage
()
os
.
Exit
(
1
)
}
...
...
@@ -75,8 +77,29 @@ func Main() {
httpsKeyFile
=
os
.
Getenv
(
"HTTPS_KEY_FILE"
)
}
var
serveTLS
bool
if
httpsCertFile
!=
""
||
httpsKeyFile
!=
""
{
serveTLS
=
true
if
httpsCertFile
==
""
||
httpsKeyFile
==
""
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Error: https cert and key must both be provided
\n\n
"
)
flag
.
Usage
()
os
.
Exit
(
1
)
}
}
logger
:=
log
.
New
(
os
.
Stderr
,
""
,
0
)
// A hacky log helper function to ensure that shutdown messages are
// formatted the same as other messages. See StdLogObserver in
// httpbin/middleware.go for the format we're matching here.
serverLog
:=
func
(
msg
string
,
args
...
interface
{})
{
const
(
logFmt
=
"time=%q msg=%q"
dateFmt
=
"2006-01-02T15:04:05.9999"
)
logger
.
Printf
(
logFmt
,
time
.
Now
()
.
Format
(
dateFmt
),
fmt
.
Sprintf
(
msg
,
args
...
))
}
h
:=
httpbin
.
New
(
httpbin
.
WithMaxBodySize
(
maxBodySize
),
httpbin
.
WithMaxDuration
(
maxDuration
),
...
...
@@ -90,22 +113,41 @@ func Main() {
Handler
:
h
.
Handler
(),
}
var
listenErr
error
if
httpsCertFile
!=
""
&&
httpsKeyFile
!=
""
{
cert
,
err
:=
tls
.
LoadX509KeyPair
(
httpsCertFile
,
httpsKeyFile
)
if
err
!=
nil
{
logger
.
Fatal
(
"Failed to generate https key pair: "
,
err
)
}
server
.
TLSConfig
=
&
tls
.
Config
{
Certificates
:
[]
tls
.
Certificate
{
cert
},
// shutdownCh triggers graceful shutdown on SIGINT or SIGTERM
shutdownCh
:=
make
(
chan
os
.
Signal
,
1
)
signal
.
Notify
(
shutdownCh
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
// exitCh will be closed when it is safe to exit, after graceful shutdown
exitCh
:=
make
(
chan
struct
{})
go
func
()
{
sig
:=
<-
shutdownCh
serverLog
(
"shutdown started by signal: %s"
,
sig
)
shutdownTimeout
:=
maxDuration
+
1
*
time
.
Second
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
shutdownTimeout
)
defer
cancel
()
server
.
SetKeepAlivesEnabled
(
false
)
if
err
:=
server
.
Shutdown
(
ctx
);
err
!=
nil
{
serverLog
(
"shutdown error: %s"
,
err
)
}
logger
.
Printf
(
"go-httpbin listening on https://%s"
,
listenAddr
)
listenErr
=
server
.
ListenAndServeTLS
(
""
,
""
)
close
(
exitCh
)
}()
var
listenErr
error
if
serveTLS
{
serverLog
(
"go-httpbin listening on https://%s"
,
listenAddr
)
listenErr
=
server
.
ListenAndServeTLS
(
httpsCertFile
,
httpsKeyFile
)
}
else
{
logger
.
Printf
(
"go-httpbin listening on http://%s"
,
listenAddr
)
serverLog
(
"go-httpbin listening on http://%s"
,
listenAddr
)
listenErr
=
server
.
ListenAndServe
()
}
if
listenErr
!=
nil
{
logger
.
Fatalf
(
"
F
ailed to listen: %s"
,
listenErr
)
if
listenErr
!=
nil
&&
listenErr
!=
http
.
ErrServerClosed
{
logger
.
Fatalf
(
"
f
ailed to listen: %s"
,
listenErr
)
}
<-
exitCh
serverLog
(
"shutdown finished"
)
}
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