mirror of
https://github.com/hauler-dev/hauler.git
synced 2026-08-24 11:47:20 +00:00
* fix: move constant and flags to prevent loop * feat: add tls cert to serve * fix: add tls cli description * fix: remove unnecessary code * small updates/fixed unit test errors * fix: migrate all flags, use exported vars * fix: standardize to AddFlags --------- Signed-off-by: will <30413278+wcrum@users.noreply.github.com> Co-authored-by: Zack Brady <zackbrady123@gmail.com>
41 lines
825 B
Go
41 lines
825 B
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gorilla/handlers"
|
|
"github.com/gorilla/mux"
|
|
"github.com/rancherfederal/hauler/internal/flags"
|
|
)
|
|
|
|
// 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 = 8080
|
|
}
|
|
|
|
if cfg.Timeout == 0 {
|
|
cfg.Timeout = 60
|
|
}
|
|
|
|
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
|
|
}
|