2017-01-29 20:06:56 +01:00
|
|
|
package webserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/NYTimes/gziphandler"
|
2019-01-17 13:26:16 +01:00
|
|
|
"github.com/bdlm/log"
|
2017-01-29 20:06:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// New creates a new webserver and starts it
|
|
|
|
func New(bindAddr, webroot string) *http.Server {
|
2017-10-12 15:25:00 +02:00
|
|
|
return &http.Server{
|
2017-01-29 20:06:56 +01:00
|
|
|
Addr: bindAddr,
|
|
|
|
Handler: gziphandler.GzipHandler(http.FileServer(http.Dir(webroot))),
|
|
|
|
}
|
2017-10-12 15:25:00 +02:00
|
|
|
}
|
2017-01-29 20:06:56 +01:00
|
|
|
|
2017-10-12 15:25:00 +02:00
|
|
|
func Start(srv *http.Server) {
|
|
|
|
// service connections
|
|
|
|
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
2019-01-17 13:26:16 +01:00
|
|
|
log.Panicf("webserver crashed: %s", err)
|
2017-10-12 15:25:00 +02:00
|
|
|
}
|
2017-01-29 20:06:56 +01:00
|
|
|
}
|