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

145 lines
3.2 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/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 {
2020-04-07 13:54:00 +02:00
r := newRouter(conf.PathPrefix)
2018-09-10 08:17:00 +02:00
r.PanicHandler = handlePanic
r.GET("/", handleLanding, true)
r.GET("/health", handleHealth, true)
r.GET("/favicon.ico", handleFavicon, true)
r.GET("/", withCORS(withSecret(handleProcessing)), false)
2020-01-30 12:07:43 +02:00
r.HEAD("/", withCORS(handleHead), false)
r.OPTIONS("/", withCORS(handleHead), false)
return r
}
2020-02-27 17:44:59 +02:00
func startServer(cancel context.CancelFunc) (*http.Server, error) {
2020-02-03 14:03:18 +02:00
l, err := listenReuseport(conf.Network, conf.Bind)
if err != nil {
2020-02-27 17:44:59 +02:00
return nil, fmt.Errorf("Can't start server: %s", err)
}
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
}
2019-06-21 12:32:37 +02:00
if conf.KeepAliveTimeout > 0 {
s.IdleTimeout = time.Duration(conf.KeepAliveTimeout) * time.Second
} else {
s.SetKeepAlivesEnabled(false)
}
2020-02-27 17:44:59 +02:00
if err := initProcessingHandler(); err != nil {
return nil, err
}
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 {
2020-02-27 17:44:59 +02:00
logError(err.Error())
2018-10-05 17:17:36 +02:00
}
2020-02-27 17:44:59 +02:00
cancel()
2018-09-10 08:17:00 +02:00
}()
2020-02-27 17:44:59 +02:00
return s, nil
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) {
var (
ierr *imgproxyError
ok bool
)
if ierr, ok = err.(*imgproxyError); !ok {
ierr = newUnexpectedError(err.Error(), 3)
}
2019-08-15 15:15:37 +02:00
if ierr.Unexpected {
reportError(err, r)
}
logResponse(reqID, r, ierr.StatusCode, ierr, nil, nil)
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, r, 200, nil, nil, nil)
rw.WriteHeader(200)
rw.Write(imgproxyIsRunningMsg)
}
2020-01-30 12:07:43 +02:00
func handleHead(reqID string, rw http.ResponseWriter, r *http.Request) {
logResponse(reqID, r, 200, nil, nil, nil)
rw.WriteHeader(200)
}
func handleFavicon(reqID string, rw http.ResponseWriter, r *http.Request) {
logResponse(reqID, r, 200, nil, nil, nil)
// TODO: Add a real favicon maybe?
rw.WriteHeader(200)
}