Commits

Miki Tebeka committed 07f0606

Working

  • Participants
  • Parent commits 831e34a
  • Branches zip

Comments (0)

Files changed (2)

 	Version = "0.2.0"
 )
 
-var ResourceMap map[string]Resource
+var ResourceMap map[string]Resource = nil
 
 func loadMap() (map[string]Resource, error) {
 	this := os.Args[0]
 }
 
 func Initialize() error {
+	if ResourceMap != nil {
+		return nil
+	}
 	var err error
 	ResourceMap, err = loadMap()
 	return err
 }
 
 // Handle register HTTP handler under prefix
-func Handle(prefix string) {
+func Handle(prefix string) error {
+	Initialize()
 	if !strings.HasSuffix(prefix, "/") {
 		prefix += "/"
 	}
 	var h handler
 	http.Handle(prefix, http.StripPrefix(prefix, h))
+	return nil
 }
 
 // LoadTemplates loads named templates from resources.
 	return createMain()
 }
 
-func build() {
-	cmd := exec.Command("go", "build")
-	if err := cmd.Run(); err != nil {
-		panic(err)
-	}
-}
-
 func get(path string) (*http.Response, error) {
 	url := fmt.Sprintf("http://localhost:%d/static/%s", port, path)
 	return http.Get(url)
 	return nil
 }
 
+func fixGOPATH(cwd string) {
+	path := os.Getenv("GOPATH")
+	if len(path) == 0 {
+		os.Setenv("GOPATH", fmt.Sprintf("%s/../..", cwd))
+	}
+}
+
 func init() {
-	build()
-
 	if err := initDir(); err != nil {
 		panic(err)
 	}
 	path := func(name string) string {
 		return fmt.Sprintf("%s/%s", cwd, name)
 	}
+	fixGOPATH(cwd)
+
 	os.Chdir(root)
 	defer os.Chdir(cwd)
 
-	cmd := exec.Command(path("nrsc"), path("test-resources"))
+	cmd := exec.Command("go", "install")
 	if err := cmd.Run(); err != nil {
+		fmt.Printf("error building: %s\n", err)
 		panic(err)
 	}
-	build()
+
+	cmd = exec.Command(path("pack.sh"), "nrsc-test", path("test-resources"))
+	if err := cmd.Run(); err != nil {
+		fmt.Printf("error packing: %s\n", err)
+		panic(err)
+	}
 }
 
 func checkHeaders(t *testing.T, expected map[string]string, headers http.Header) {
 	"net/http"
 	"os"
 
-	"./nrsc"
+	"nrsc"
 )
 
 type params struct {
 	nrsc.Handle("/static/")
 	http.HandleFunc("/", indexHandler)
 	if err := http.ListenAndServe(":9888", nil); err != nil {
-		fmt.Fprintf(os.Stderr, "error: %s\n", err)
+		fmt.Fprintf(os.Stderr, "error: %d\n", err)
 		os.Exit(1)
 	}
 }