2019-10-29 14:38:29 -07:00
|
|
|
package main
|
2019-10-29 13:30:51 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
http.Server
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:57:10 +02:00
|
|
|
func NewServer(cfg *Config, storage http.FileSystem) *Server {
|
2019-10-29 13:30:51 -07:00
|
|
|
port := cfg.Server.Port
|
|
|
|
if port == 0 {
|
|
|
|
port = 8080
|
|
|
|
}
|
2022-01-02 13:17:28 +02:00
|
|
|
|
2020-10-27 13:29:42 +08:00
|
|
|
bindAddress := cfg.Server.BindAddress
|
|
|
|
if bindAddress == "*" {
|
|
|
|
bindAddress = ""
|
|
|
|
}
|
2022-01-02 13:17:28 +02:00
|
|
|
|
2019-10-29 13:30:51 -07:00
|
|
|
srv := Server{}
|
|
|
|
|
2020-10-27 13:29:42 +08:00
|
|
|
srv.Addr = fmt.Sprintf("%s:%d", bindAddress, port)
|
|
|
|
log.Debugf("using address: %s:%s", bindAddress, srv.Addr)
|
2019-10-29 13:30:51 -07:00
|
|
|
|
2022-01-02 13:17:28 +02:00
|
|
|
fileServer := http.FileServer(storage)
|
|
|
|
|
|
|
|
log.Debugf("handle path: /%s", cfg.Server.Path)
|
|
|
|
http.Handle(fmt.Sprintf("/%s", cfg.Server.Path), fileServer)
|
2019-10-29 13:30:51 -07:00
|
|
|
|
|
|
|
return &srv
|
|
|
|
}
|