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])) } conf, err := store.ReadConfig(confPath) 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 *gc.C) { server, err := store.NewServer(s.store) c.Assert(err, gc.IsNil) req, err := http.NewRequest("GET", "/", nil) c.Assert(err, gc.IsNil) rec := httptest.NewRecorder() server.ServeHTTP(rec, req) c.Assert(rec.Code, gc.Equals, 303) c.Assert(rec.Header().Get("Location"), gc.Equals, "https://juju.ubuntu.com") }
func (s *StoreSuite) prepareServer(c *gc.C) (*store.Server, *charm.URL) { curl := charm.MustParseURL("cs:precise/wordpress") pub, err := s.store.CharmPublisher([]*charm.URL{curl}, "some-digest") c.Assert(err, gc.IsNil) err = pub.Publish(&FakeCharmDir{}) c.Assert(err, gc.IsNil) server, err := store.NewServer(s.store) c.Assert(err, gc.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 *gc.C) { server, err := store.NewServer(s.store) c.Assert(err, gc.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, gc.IsNil) rec := httptest.NewRecorder() server.ServeHTTP(rec, req) c.Assert(rec.Code, gc.Equals, test.code, gc.Commentf("Path: %s", test.path)) } }