1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/server.go

126 lines
2.6 KiB
Go
Raw Normal View History

2017-06-27 11:00:33 +02:00
package main
import (
2018-09-10 08:17:00 +02:00
"context"
"crypto/subtle"
2017-06-27 11:00:33 +02:00
"fmt"
"net"
2017-06-27 11:00:33 +02:00
"net/http"
2017-07-03 06:08:47 +02:00
"time"
2018-03-15 18:58:11 +02:00
"golang.org/x/net/netutil"
2017-06-27 11:00:33 +02:00
)
2018-10-05 17:17:36 +02:00
var (
imgproxyIsRunningMsg = []byte("imgproxy is running")
2018-10-05 22:29:55 +02:00
errInvalidSecret = newError(403, "Invalid secret", "Forbidden")
)
func buildRouter() *router {
r := newRouter()
2018-09-10 08:17:00 +02:00
r.PanicHandler = handlePanic
r.GET("/health", handleHealth)
r.GET("/", withCORS(withSecret(handleProcessing)))
r.OPTIONS("/", withCORS(handleOptions))
return r
}
func startServer() *http.Server {
l, err := net.Listen("tcp", conf.Bind)
if err != nil {
logFatal(err.Error())
}
l = netutil.LimitListener(l, conf.MaxClients)
s := &http.Server{
Handler: buildRouter(),
ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
MaxHeaderBytes: 1 << 20,
2018-09-10 08:17:00 +02:00
}
initProcessingHandler()
2018-09-10 08:17:00 +02:00
go func() {
2019-01-11 17:01:48 +02:00
logNotice("Starting server at %s", conf.Bind)
if err := s.Serve(l); err != nil && err != http.ErrServerClosed {
2019-01-11 17:01:48 +02:00
logFatal(err.Error())
2018-10-05 17:17:36 +02:00
}
2018-09-10 08:17:00 +02:00
}()
return s
2018-09-10 08:17:00 +02:00
}
func shutdownServer(s *http.Server) {
2019-01-11 17:01:48 +02:00
logNotice("Shutting down the server...")
ctx, close := context.WithTimeout(context.Background(), 5*time.Second)
defer close()
s.Shutdown(ctx)
2018-09-10 08:17:00 +02:00
}
func withCORS(h routeHandler) routeHandler {
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
if len(conf.AllowOrigin) > 0 {
rw.Header().Set("Access-Control-Allow-Origin", conf.AllowOrigin)
rw.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
}
h(reqID, rw, r)
}
2018-11-20 14:53:44 +02:00
}
func withSecret(h routeHandler) routeHandler {
if len(conf.Secret) == 0 {
return h
2019-05-06 10:42:30 +02:00
}
authHeader := []byte(fmt.Sprintf("Bearer %s", conf.Secret))
2019-05-06 10:42:30 +02:00
return func(reqID string, rw http.ResponseWriter, r *http.Request) {
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), authHeader) == 1 {
h(reqID, rw, r)
} else {
panic(errInvalidSecret)
}
2017-07-03 11:36:37 +02:00
}
2017-07-04 16:05:53 +02:00
}
func handlePanic(reqID string, rw http.ResponseWriter, r *http.Request, err error) {
reportError(err, r)
2018-10-05 17:17:36 +02:00
var (
ierr *imgproxyError
ok bool
)
if ierr, ok = err.(*imgproxyError); !ok {
ierr = newUnexpectedError(err.Error(), 3)
}
logResponse(reqID, ierr.StatusCode, ierr.Message)
rw.WriteHeader(ierr.StatusCode)
if conf.DevelopmentErrorsMode {
rw.Write([]byte(ierr.Message))
} else {
rw.Write([]byte(ierr.PublicMessage))
}
2017-07-04 16:05:53 +02:00
}
func handleHealth(reqID string, rw http.ResponseWriter, r *http.Request) {
logResponse(reqID, 200, string(imgproxyIsRunningMsg))
rw.WriteHeader(200)
rw.Write(imgproxyIsRunningMsg)
}
func handleOptions(reqID string, rw http.ResponseWriter, r *http.Request) {
logResponse(reqID, 200, "Respond with options")
rw.WriteHeader(200)
}