func main() { e := echo.New() // the file server for rice. "app" is the folder where the files come from. assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox()) // serves the index.html from rice e.GET("/", echo.WrapHandler(assetHandler)) // servers other static files e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler))) e.Logger.Fatal(e.Start(":1323")) }
func TestServe(t *testing.T) { registerHandlers() ts := httptest.NewServer(http.DefaultServeMux) defer ts.Close() expected := make(map[string]int) for endpoint := range v1Endpoints { expected[v1APIPath(endpoint)] = http.StatusOK } staticDir := "static" err := rice.MustFindBox(staticDir).Walk("", func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { expected["/"+path] = http.StatusOK } return nil }) if err != nil { t.Error(err) } // Disabled endpoints should return '404 Not Found' expected[v1APIPath("sign")] = http.StatusNotFound expected[v1APIPath("authsign")] = http.StatusNotFound expected[v1APIPath("newcert")] = http.StatusNotFound expected[v1APIPath("info")] = http.StatusNotFound expected[v1APIPath("bundle")] = http.StatusNotFound // Enabled endpoints should return '405 Method Not Allowed' expected[v1APIPath("init_ca")] = http.StatusMethodNotAllowed expected[v1APIPath("newkey")] = http.StatusMethodNotAllowed // POST-only endpoints should return '400 Bad Request' expected[v1APIPath("scan")] = http.StatusBadRequest // Non-existent endpoints should return '404 Not Found' expected["/bad_endpoint"] = http.StatusNotFound for endpoint, status := range expected { resp, err := http.Get(ts.URL + endpoint) if err != nil { t.Error(err) } if resp.StatusCode != status { t.Fatalf("%s: '%s' (expected '%s')", endpoint, resp.Status, http.StatusText(status)) } } }
}, "init_ca": func() (http.Handler, error) { return initca.NewHandler(), nil }, "scan": func() (http.Handler, error) { return scan.NewHandler(), nil }, "scaninfo": func() (http.Handler, error) { return scan.NewInfoHandler(), nil }, "/": func() (http.Handler, error) { return http.FileServer(rice.MustFindBox("static").HTTPBox()), nil }, } // v1APIPath prepends the V1 API prefix to endpoints not func v1APIPath(endpoint string) string { prefix := "/api/v1/cfssl/" if strings.HasPrefix(endpoint, "/") { prefix = "" } return (&url.URL{Path: prefix + endpoint}).String() } // registerHandlers instantiates various handlers and associate them to corresponding endpoints. func registerHandlers() { for path, getHandler := range v1Endpoints {