pidgin/carrier
Clone
Summary
Browse
Changes
Graph
UptimeRobot keeps sending a GET so just accept that
draft
2019-05-14, Gary Kramlich
937f50c92885
UptimeRobot keeps sending a GET so just accept that
package
server
import
(
"net/http"
log
"github.com/sirupsen/logrus"
"bitbucket.org/pidgin/carrier/uptimerobot"
)
type
Server
struct
{
server
*
http
.
Server
errChan
chan
error
}
func
healthzHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
status
:=
http
.
StatusMethodNotAllowed
if
r
.
Method
==
http
.
MethodGet
{
status
=
http
.
StatusOK
}
w
.
WriteHeader
(
status
)
}
func
httpLogger
(
handler
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Infof
(
"%s %s %s\n"
,
r
.
RemoteAddr
,
r
.
Method
,
r
.
URL
)
handler
.
ServeHTTP
(
w
,
r
)
})
}
func
NewServer
(
addr
string
,
errChan
chan
error
)
*
Server
{
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/healthz"
,
healthzHandler
)
mux
.
HandleFunc
(
"/uptime-robot"
,
uptimerobot
.
Handler
)
return
&
Server
{
errChan
:
errChan
,
server
:
&
http
.
Server
{
Addr
:
addr
,
Handler
:
httpLogger
(
mux
),
},
}
}
func
(
s
*
Server
)
Start
()
{
log
.
Infof
(
"http server started"
)
s
.
errChan
<-
s
.
server
.
ListenAndServe
()
}
func
(
s
*
Server
)
Stop
()
{
s
.
server
.
Close
()
}