mirror of
				https://github.com/imgproxy/imgproxy.git
				synced 2025-10-30 23:08:02 +02:00 
			
		
		
		
	Favicon dummy handler & hello message at the root
This commit is contained in:
		
							
								
								
									
										20
									
								
								landing.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								landing.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,20 @@ | ||||
| package main | ||||
|  | ||||
| import "net/http" | ||||
|  | ||||
| var landingTmpl = []byte(` | ||||
| <!doctype html> | ||||
| <html> | ||||
| 	<head><title>Hey, I'm imgproxy!</title></head> | ||||
| 	<body> | ||||
| 		<h1>Hey, I'm imgproxy!</h1> | ||||
| 		<p style="font-size:1.2em">You can get me here: <a href="https://github.com/imgproxy/imgproxy" target="_blank">https://github.com/imgproxy/imgproxy</a></p> | ||||
| 	</body> | ||||
| </html> | ||||
| `) | ||||
|  | ||||
| func handleLanding(reqID string, rw http.ResponseWriter, r *http.Request) { | ||||
| 	rw.Header().Set("Content-Tyle", "text/html") | ||||
| 	rw.WriteHeader(200) | ||||
| 	rw.Write(landingTmpl) | ||||
| } | ||||
							
								
								
									
										27
									
								
								router.go
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								router.go
									
									
									
									
									
								
							| @@ -23,6 +23,7 @@ type route struct { | ||||
| 	Method  string | ||||
| 	Prefix  string | ||||
| 	Handler routeHandler | ||||
| 	Exact   bool | ||||
| } | ||||
|  | ||||
| type router struct { | ||||
| @@ -30,25 +31,37 @@ type router struct { | ||||
| 	PanicHandler panicHandler | ||||
| } | ||||
|  | ||||
| func (r *route) IsMatch(req *http.Request) bool { | ||||
| 	if r.Method != req.Method { | ||||
| 		return false | ||||
| 	} | ||||
|  | ||||
| 	if r.Exact { | ||||
| 		return req.URL.Path == r.Prefix | ||||
| 	} | ||||
|  | ||||
| 	return strings.HasPrefix(req.URL.Path, r.Prefix) | ||||
| } | ||||
|  | ||||
| func newRouter() *router { | ||||
| 	return &router{ | ||||
| 		Routes: make([]*route, 0), | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (r *router) Add(method, prefix string, handler routeHandler) { | ||||
| func (r *router) Add(method, prefix string, handler routeHandler, exact bool) { | ||||
| 	r.Routes = append( | ||||
| 		r.Routes, | ||||
| 		&route{Method: method, Prefix: prefix, Handler: handler}, | ||||
| 		&route{Method: method, Prefix: prefix, Handler: handler, Exact: exact}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (r *router) GET(prefix string, handler routeHandler) { | ||||
| 	r.Add(http.MethodGet, prefix, handler) | ||||
| func (r *router) GET(prefix string, handler routeHandler, exact bool) { | ||||
| 	r.Add(http.MethodGet, prefix, handler, exact) | ||||
| } | ||||
|  | ||||
| func (r *router) OPTIONS(prefix string, handler routeHandler) { | ||||
| 	r.Add(http.MethodOptions, prefix, handler) | ||||
| func (r *router) OPTIONS(prefix string, handler routeHandler, exact bool) { | ||||
| 	r.Add(http.MethodOptions, prefix, handler, exact) | ||||
| } | ||||
|  | ||||
| func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||||
| @@ -74,7 +87,7 @@ func (r *router) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||||
| 	logRequest(reqID, req) | ||||
|  | ||||
| 	for _, rr := range r.Routes { | ||||
| 		if rr.Method == req.Method && strings.HasPrefix(req.URL.Path, rr.Prefix) { | ||||
| 		if rr.IsMatch(req) { | ||||
| 			rr.Handler(reqID, rw, req) | ||||
| 			return | ||||
| 		} | ||||
|   | ||||
							
								
								
									
										13
									
								
								server.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								server.go
									
									
									
									
									
								
							| @@ -21,9 +21,11 @@ func buildRouter() *router { | ||||
|  | ||||
| 	r.PanicHandler = handlePanic | ||||
|  | ||||
| 	r.GET("/health", handleHealth) | ||||
| 	r.GET("/", withCORS(withSecret(handleProcessing))) | ||||
| 	r.OPTIONS("/", withCORS(handleOptions)) | ||||
| 	r.GET("/", handleLanding, true) | ||||
| 	r.GET("/health", handleHealth, true) | ||||
| 	r.GET("/favicon.ico", handleFavicon, true) | ||||
| 	r.GET("/", withCORS(withSecret(handleProcessing)), false) | ||||
| 	r.OPTIONS("/", withCORS(handleOptions), false) | ||||
|  | ||||
| 	return r | ||||
| } | ||||
| @@ -130,3 +132,8 @@ func handleOptions(reqID string, rw http.ResponseWriter, r *http.Request) { | ||||
| 	logResponse(reqID, 200, "Respond with options") | ||||
| 	rw.WriteHeader(200) | ||||
| } | ||||
|  | ||||
| func handleFavicon(reqID string, rw http.ResponseWriter, r *http.Request) { | ||||
| 	// TODO: Add a real favicon maybe? | ||||
| 	rw.WriteHeader(200) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user