| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | package lib | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/genofire/logmania/log" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | // little httpserver to handle reconnect and http.Handlers
 | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | type HTTPServer struct { | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | 	srv               *http.Server | 
					
						
							|  |  |  | 	Addr              string | 
					
						
							|  |  |  | 	Handler           http.Handler | 
					
						
							|  |  |  | 	errorNoPanic      bool | 
					
						
							|  |  |  | 	errorNoPanicAsync sync.Mutex | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | // start httpserver
 | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | func (hs *HTTPServer) Start() { | 
					
						
							|  |  |  | 	hs.srv = &http.Server{ | 
					
						
							|  |  |  | 		Addr:    hs.Addr, | 
					
						
							|  |  |  | 		Handler: hs.Handler, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		log.Debug("startup of http listener") | 
					
						
							|  |  |  | 		if err := hs.srv.ListenAndServe(); err != nil { | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | 			if hs.errorNoPanic { | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | 				log.Debug("httpserver shutdown without panic") | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			log.Panic(err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-13 00:21:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // rebind httpserver to a new address (e.g. new configuration)
 | 
					
						
							|  |  |  | func (hs *HTTPServer) Rebind(addr string) bool { | 
					
						
							|  |  |  | 	if addr == hs.Addr { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	hs.errorNoPanicAsync.Lock() | 
					
						
							|  |  |  | 	hs.errorNoPanic = true | 
					
						
							|  |  |  | 	hs.Close() | 
					
						
							|  |  |  | 	hs.Addr = addr | 
					
						
							|  |  |  | 	hs.Start() | 
					
						
							|  |  |  | 	hs.errorNoPanic = false | 
					
						
							|  |  |  | 	hs.errorNoPanicAsync.Unlock() | 
					
						
							|  |  |  | 	return true | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // close/stop current httpserver
 | 
					
						
							| 
									
										
										
										
											2017-06-12 22:32:27 +02:00
										 |  |  | func (hs *HTTPServer) Close() { | 
					
						
							|  |  |  | 	log.Debug("startup of http listener") | 
					
						
							|  |  |  | 	hs.srv.Close() | 
					
						
							|  |  |  | } |