1
0
forked from sm/vain

Don't require user to set YSV_HOST

The original reason for having this required configuration parameter was that
I didn't think to read the hostname off the request. Doing it this way has the
additional benefit of a single server being able to serve a multitude of
hostnames.

Derek helped me realize this by repeating "there should be sane defaults" like
9000 times (maybe over 9000) till I got so tired of trying to make sure that he
understood why I did it that way to begin with that I looked at the problem
from a different perspective and like in a dream the solution came to me.

fixes #10.
This commit is contained in:
Stephen McQuay 2016-02-16 23:18:54 -08:00
parent bb48f82f9e
commit 501631a45a
2 changed files with 5 additions and 12 deletions

View File

@ -62,7 +62,6 @@ YSV_DB: path to json database
type config struct { type config struct {
Port int Port int
Host string
DB string DB string
} }
@ -81,10 +80,6 @@ func main() {
os.Exit(0) os.Exit(0)
} }
} }
if c.Host == "" {
log.Printf("must set YSV_HOST; please run $(ysvd env) for more information")
os.Exit(1)
}
if c.DB == "" { if c.DB == "" {
log.Printf("warning: in-memory db mode; if you do not want this set YSV_DB") log.Printf("warning: in-memory db mode; if you do not want this set YSV_DB")
} }
@ -100,7 +95,7 @@ func main() {
if err := ms.Load(); err != nil { if err := ms.Load(); err != nil {
log.Printf("unable to load db: %v; creating fresh database", err) log.Printf("unable to load db: %v; creating fresh database", err)
} }
vain.NewServer(sm, ms, c.Host) vain.NewServer(sm, ms)
addr := fmt.Sprintf(":%d", c.Port) addr := fmt.Sprintf(":%d", c.Port)
if err := http.ListenAndServe(addr, sm); err != nil { if err := http.ListenAndServe(addr, sm); err != nil {
log.Printf("problem with http server: %v", err) log.Printf("problem with http server: %v", err)

View File

@ -8,10 +8,9 @@ import (
) )
// NewServer populates a server, adds the routes, and returns it for use. // NewServer populates a server, adds the routes, and returns it for use.
func NewServer(sm *http.ServeMux, store Storage, hostname string) *Server { func NewServer(sm *http.ServeMux, store Storage) *Server {
s := &Server{ s := &Server{
storage: store, storage: store,
hostname: hostname,
} }
sm.Handle("/", s) sm.Handle("/", s)
return s return s
@ -19,7 +18,6 @@ func NewServer(sm *http.ServeMux, store Storage, hostname string) *Server {
// Server serves up the http. // Server serves up the http.
type Server struct { type Server struct {
hostname string
storage Storage storage Storage
} }
@ -45,7 +43,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.Error(w, fmt.Sprintf("invalid repository %q", req.URL.Path), http.StatusBadRequest) http.Error(w, fmt.Sprintf("invalid repository %q", req.URL.Path), http.StatusBadRequest)
return return
} }
p.path = fmt.Sprintf("%s/%s", s.hostname, strings.Trim(req.URL.Path, "/")) p.path = fmt.Sprintf("%s/%s", req.Host, strings.Trim(req.URL.Path, "/"))
if !Valid(p.path, s.storage.All()) { if !Valid(p.path, s.storage.All()) {
http.Error(w, fmt.Sprintf("invalid path; prefix already taken %q", req.URL.Path), http.StatusConflict) http.Error(w, fmt.Sprintf("invalid path; prefix already taken %q", req.URL.Path), http.StatusConflict)
return return