"github.com/gorilla/mux" "github.com/gorilla/schema" "sourcegraph.com/sourcegraph/thesrc" "sourcegraph.com/sourcegraph/thesrc/router" ) var ( // ReloadTemplates is whether to reload templates on each request. ReloadTemplates bool // StaticDir is the directory containing static assets. StaticDir = filepath.Join(defaultBase("sourcegraph.com/sourcegraph/thesrc/app"), "static") ) var ( APIClient = thesrc.NewClient(nil) schemaDecoder = schema.NewDecoder() appRouter = router.App() ) func Handler() *mux.Router { m := appRouter m.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(StaticDir)))) // TODO(sqs): add handlers for /favicon.ico and /robots.txt m.Get(router.Post).Handler(handler(servePost)) m.Get(router.Posts).Handler(handler(servePosts)) m.Get(router.SubmitPostForm).Handler(handler(serveSubmitPostForm)) m.Get(router.SubmitPost).Handler(handler(serveSubmitPost)) return m }
package importer import "sourcegraph.com/sourcegraph/thesrc" var Fetchers = []Fetcher{} // A Fetcher fetches posts from other sites. type Fetcher interface { // Fetch posts. Fetch() ([]*thesrc.Post, error) // Site is the name of the site that this Fetcher fetches from. Site() string } var Store = thesrc.NewClient(nil) // Import posts fetched by f. If Imported is non-nil, it is called each time a // post is successfully imported. func Import(f Fetcher) error { posts, err := f.Fetch() if err != nil { return err } for _, post := range posts { created, err := Store.Posts.Submit(post) if err != nil { return err } if Imported != nil {
"io/ioutil" "net/http" "net/http/httptest" "sourcegraph.com/sourcegraph/thesrc" "sourcegraph.com/sourcegraph/thesrc/datastore" ) func init() { serveMux.Handle("/", http.StripPrefix("/api", Handler())) } var ( serveMux = http.NewServeMux() httpClient = http.Client{Transport: (*muxTransport)(serveMux)} apiClient = thesrc.NewClient(&httpClient) ) func setup() { store = datastore.NewMockDatastore() } type muxTransport http.ServeMux // RoundTrip is a custom http.RoundTripper for testing API requests/responses. // It intercepts all HTTP requests during testing to serve up a local/internal // response instead of dialing out to the Host specified in the Client's BaseURL. func (t *muxTransport) RoundTrip(req *http.Request) (*http.Response, error) { rw := httptest.NewRecorder() rw.Body = new(bytes.Buffer) (*http.ServeMux)(t).ServeHTTP(rw, req)
type subcmd struct { name string description string run func(args []string) } var subcmds = []subcmd{ {"post", "submit a post", postCmd}, {"import", "import posts from other sites", importCmd}, {"classify", "classify posts", classifyCmd}, {"serve", "start web server", serveCmd}, {"createdb", "create the database schema", createDBCmd}, } var apiclient = thesrc.NewClient(nil) func postCmd(args []string) { fs := flag.NewFlagSet("post", flag.ExitOnError) title := fs.String("title", "", "title of post") linkURL := fs.String("link", "", "link URL") body := fs.String("body", "", "body of post") fs.Usage = func() { fmt.Fprintln(os.Stderr, `usage: thesrc post [options] Submits a post. The options are: `) fs.PrintDefaults() os.Exit(1)