diff --git a/cmd/vanity/main.go b/cmd/vanity/main.go index 903d287..6fddc40 100644 --- a/cmd/vanity/main.go +++ b/cmd/vanity/main.go @@ -44,7 +44,7 @@ func main() { if err != nil { log.Fatal(err) } - server := vanity.Server{domainFlag, conf} + server := vanity.NewServer(*domainFlag, conf) port := fmt.Sprintf(":%v", *portFlag) log.Fatal(http.ListenAndServe(port, server)) } @@ -58,12 +58,8 @@ func readConfig(r io.Reader) (map[vanity.Path]vanity.Package, error) { case 0: continue case 3: - pack := vanity.Package{ - Path: parsePath(fields[0]), - VCSSystem: fields[1], - VCSURL: fields[2], - } - conf[vanity.Path(fields[0])] = pack + pack := vanity.NewPackage(parsePath(fields[0]), fields[1], fields[2]) + conf[vanity.Path(fields[0])] = *pack default: return conf, errors.New("configuration error: " + scanner.Text()) } diff --git a/cmd/vanity/main_test.go b/cmd/vanity/main_test.go index a6f095c..a7f309d 100644 --- a/cmd/vanity/main_test.go +++ b/cmd/vanity/main_test.go @@ -15,10 +15,10 @@ func TestParseConfig(t *testing.T) { /vanity/cmd/vanity git https://github.com/kare/vanity` expected := map[vanity.Path]vanity.Package{ - "/gist": {"/gist", "git", "https://github.com/kare/gist"}, - "/vanity": {"/vanity", "git", "https://github.com/kare/vanity"}, - "/vanity/cmd": {"/vanity", "git", "https://github.com/kare/vanity"}, - "/vanity/cmd/vanity": {"/vanity", "git", "https://github.com/kare/vanity"}, + "/gist": *vanity.NewPackage("/gist", "git", "https://github.com/kare/gist"), + "/vanity": *vanity.NewPackage("/vanity", "git", "https://github.com/kare/vanity"), + "/vanity/cmd": *vanity.NewPackage("/vanity", "git", "https://github.com/kare/vanity"), + "/vanity/cmd/vanity": *vanity.NewPackage("/vanity", "git", "https://github.com/kare/vanity"), } conf, err := readConfig(strings.NewReader(config)) if err != nil { diff --git a/vanity.go b/vanity.go index 2fd1a39..7fcfd9f 100644 --- a/vanity.go +++ b/vanity.go @@ -27,6 +27,25 @@ type ( } ) +// NewPackage returns a new Package given a path, VCS system and VCS URL. +func NewPackage(path, vcssystem, vcsurl string) *Package { + p := &Package{ + Path: path, + VCSSystem: vcssystem, + VCSURL: vcsurl, + } + return p +} + +// NewServer returns a new Vanity Server given domain name and vanity package configuration. +func NewServer(domain string, config map[Path]Package) *Server { + s := &Server{ + Domain: &domain, + Config: config, + } + return s +} + func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/html; charset=utf-8") if r.Method != "GET" { diff --git a/vanity_test.go b/vanity_test.go index ae0fdb2..858a514 100644 --- a/vanity_test.go +++ b/vanity_test.go @@ -12,18 +12,18 @@ import ( var ( hostname = "kkn.fi" config = map[Path]Package{ - "/gist": {"/gist", "git", "https://github.com/kare/gist"}, - "/vanity": {"/vanity", "git", "https://github.com/kare/vanity"}, - "/vanity/cmd": {"/vanity", "git", "https://github.com/kare/vanity"}, - "/vanity/cmd/vanity": {"/vanity", "git", "https://github.com/kare/vanity"}, - "/foo/bar": {"/foo", "git", "https://github.com/kare/foo"}, - "/foo/bar/baz": {"/foo", "git", "https://github.com/kare/foo"}, - "/": {"/", "git", "https://github.com/project"}, + "/gist": *NewPackage("/gist", "git", "https://github.com/kare/gist"), + "/vanity": *NewPackage("/vanity", "git", "https://github.com/kare/vanity"), + "/vanity/cmd": *NewPackage("/vanity", "git", "https://github.com/kare/vanity"), + "/vanity/cmd/vanity": *NewPackage("/vanity", "git", "https://github.com/kare/vanity"), + "/foo/bar": *NewPackage("/foo", "git", "https://github.com/kare/foo"), + "/foo/bar/baz": *NewPackage("/foo", "git", "https://github.com/kare/foo"), + "/": *NewPackage("/", "git", "https://github.com/project"), } ) func TestHTTPMethodsSupport(t *testing.T) { - server := Server{&hostname, config} + server := NewServer(hostname, config) tests := []struct { method string status int @@ -50,7 +50,7 @@ func TestHTTPMethodsSupport(t *testing.T) { } func TestGoTool(t *testing.T) { - server := httptest.NewServer(Server{&hostname, config}) + server := httptest.NewServer(NewServer(hostname, config)) defer server.Close() tests := []struct { @@ -95,7 +95,7 @@ func TestGoTool(t *testing.T) { } func TestGoToolPackageNotFound(t *testing.T) { - server := httptest.NewServer(Server{&hostname, config}) + server := httptest.NewServer(NewServer(hostname, config)) defer server.Close() url := server.URL + "/package-not-found?go-get=1" @@ -123,7 +123,7 @@ func TestGoToolPackageNotFound(t *testing.T) { } func TestBrowserGoDoc(t *testing.T) { - server := httptest.NewServer(Server{&hostname, config}) + server := httptest.NewServer(NewServer(hostname, config)) defer server.Close() tests := []struct {