Add NewServer and NewPackage functions

git-svn-id: file:///srv/svn/repo/toyohime/trunk@18 922d331f-388e-da47-97a9-ad700dc0b8b9
This commit is contained in:
kare.nuorteva 2016-10-24 02:01:49 +00:00
parent 269673402c
commit 77428251fe
4 changed files with 37 additions and 22 deletions

View File

@ -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())
}

View File

@ -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 {

View File

@ -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" {

View File

@ -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 {