-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support serving directory in local webserver (#44)
- Loading branch information
Showing
3 changed files
with
101 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package scraper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"mime" | ||
"net/http" | ||
|
||
"github.com/cornelk/gotokit/log" | ||
) | ||
|
||
// set more mime types in the browser, this for example fixes .asp files not being | ||
// downloaded but handled as html. | ||
var mimeTypes = map[string]string{ | ||
".asp": "text/html; charset=utf-8", | ||
} | ||
|
||
func ServeDirectory(ctx context.Context, path string, port int16, logger *log.Logger) error { | ||
fs := http.FileServer(http.Dir(path)) | ||
mux := http.NewServeMux() | ||
mux.Handle("/", fs) // server root by file system | ||
|
||
// update mime types | ||
for ext, mt := range mimeTypes { | ||
if err := mime.AddExtensionType(ext, mt); err != nil { | ||
return fmt.Errorf("adding mime type '%s': %w", ext, err) | ||
} | ||
} | ||
|
||
fullAddr := fmt.Sprintf("http://127.0.0.1:%d", port) | ||
logger.Info("Serving directory...", | ||
log.String("path", path), | ||
log.String("address", fullAddr)) | ||
|
||
server := &http.Server{ | ||
Addr: fmt.Sprintf(":%d", port), | ||
Handler: mux, | ||
} | ||
|
||
serverErr := make(chan error, 1) | ||
go func() { | ||
serverErr <- server.ListenAndServe() | ||
}() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
//nolint: contextcheck | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
return fmt.Errorf("shutting down webserver: %w", err) | ||
} | ||
return nil | ||
|
||
case err := <-serverErr: | ||
return fmt.Errorf("starting webserver: %w", err) | ||
} | ||
} |