mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-02-14 09:59:50 +00:00
* added env variables for haulerDir/tempDir
* updated hauler directory for the install script
* cleanup/fixes for install script
* updated variables based on feedback
* revert "updated variables based on feedback"
* reverts commit 54f7a4d695
* minor restructure to root flags
* updated logic to include haulerdir flag
* cleaned up/formatted new logic
* more cleanup and formatting
42 lines
901 B
Go
42 lines
901 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gorilla/handlers"
|
|
"github.com/gorilla/mux"
|
|
"hauler.dev/go/hauler/internal/flags"
|
|
"hauler.dev/go/hauler/pkg/consts"
|
|
)
|
|
|
|
// NewFile returns a fileserver
|
|
// TODO: Better configs
|
|
func NewFile(ctx context.Context, cfg flags.ServeFilesOpts) (Server, error) {
|
|
r := mux.NewRouter()
|
|
r.PathPrefix("/").Handler(handlers.LoggingHandler(os.Stdout, http.StripPrefix("/", http.FileServer(http.Dir(cfg.RootDir)))))
|
|
if cfg.RootDir == "" {
|
|
cfg.RootDir = "."
|
|
}
|
|
|
|
if cfg.Port == 0 {
|
|
cfg.Port = consts.DefaultFileserverPort
|
|
}
|
|
|
|
if cfg.Timeout == 0 {
|
|
cfg.Timeout = consts.DefaultFileserverTimeout
|
|
}
|
|
|
|
srv := &http.Server{
|
|
Handler: r,
|
|
Addr: fmt.Sprintf(":%d", cfg.Port),
|
|
WriteTimeout: time.Duration(cfg.Timeout) * time.Second,
|
|
ReadTimeout: time.Duration(cfg.Timeout) * time.Second,
|
|
}
|
|
|
|
return srv, nil
|
|
}
|