func serve() error { var confPath string if len(os.Args) == 2 { if _, err := os.Stat(os.Args[1]); err == nil { confPath = os.Args[1] } } if confPath == "" { return fmt.Errorf("usage: %s <config path>", filepath.Base(os.Args[0])) } var conf config err := readConfig(confPath, &conf) if err != nil { return err } if conf.MongoURL == "" || conf.APIAddr == "" { return fmt.Errorf("missing mongo-url or api-addr in config file") } s, err := store.Open(conf.MongoURL) if err != nil { return err } defer s.Close() server, err := store.NewServer(s) if err != nil { return err } return http.ListenAndServe(conf.APIAddr, server) }
func (s *StoreSuite) TestRootRedirect(c *C) { server, err := store.NewServer(s.store) c.Assert(err, IsNil) req, err := http.NewRequest("GET", "/", nil) c.Assert(err, IsNil) rec := httptest.NewRecorder() server.ServeHTTP(rec, req) c.Assert(rec.Code, Equals, 303) c.Assert(rec.Header().Get("Location"), Equals, "https://juju.ubuntu.com") }
func (s *StoreSuite) prepareServer(c *C) (*store.Server, *charm.URL) { curl := charm.MustParseURL("cs:oneiric/wordpress") pub, err := s.store.CharmPublisher([]*charm.URL{curl}, "some-digest") c.Assert(err, IsNil) err = pub.Publish(&FakeCharmDir{}) c.Assert(err, IsNil) server, err := store.NewServer(s.store) c.Assert(err, IsNil) return server, curl }
func (s *StoreSuite) TestSeriesNotFound(c *gc.C) { server, err := store.NewServer(s.store) req, err := http.NewRequest("GET", "/charm-info?charms=cs:not-found", nil) c.Assert(err, gc.IsNil) rec := httptest.NewRecorder() server.ServeHTTP(rec, req) c.Assert(rec.Code, gc.Equals, http.StatusOK) expected := map[string]interface{}{"cs:not-found": map[string]interface{}{ "revision": float64(0), "errors": []interface{}{"entry not found"}}} obtained := map[string]interface{}{} err = json.NewDecoder(rec.Body).Decode(&obtained) c.Assert(err, gc.IsNil) c.Assert(obtained, gc.DeepEquals, expected) }
func (s *StoreSuite) TestServerStatus(c *C) { server, err := store.NewServer(s.store) c.Assert(err, IsNil) tests := []struct { path string code int }{ {"/charm-info/any", 404}, {"/charm/bad-url", 404}, {"/charm/bad-series/wordpress", 404}, {"/stats/counter/", 403}, {"/stats/counter/*", 403}, {"/stats/counter/any/", 404}, {"/stats/", 404}, {"/stats/any", 404}, } for _, test := range tests { req, err := http.NewRequest("GET", test.path, nil) c.Assert(err, IsNil) rec := httptest.NewRecorder() server.ServeHTTP(rec, req) c.Assert(rec.Code, Equals, test.code, Commentf("Path: %s", test.path)) } }