thesrc

package module
v0.0.0-...-ed5b6d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2015 License: MIT Imports: 13 Imported by: 0

README

thesrc Build Status docs examples status views

thesrc is a news site for programmers that's intended to be an example of how to structure a large Go web app. While this app is not large itself, it demonstrates the same patterns as in the web app that powers Sourcegraph.com.

The web application architecture and patterns demonstrated here were presented in a talk at Google I/O 2014 entitled Building Sourcegraph, a large-scale code search engine in Go. See that talk for more details.

thesrc has a few special features of interest to programmers:

  • just the good stuff: an automated classifier rejects links that don't contain code or involve programming;
  • not a popularity contest: you can only see a link's score by mousing over it for a couple of seconds, and (TODO) freshly posted links are randomly rotated into the homepage;

Browse the code on Sourcegraph.

Installation

Use the thesrc command to interact with the app.

You can either run it directly:

go get -u sourcegraph.com/sourcegraph/thesrc/...
thesrc

Or inside Docker:

docker build -t thesrc && docker run thesrc

If you want to run it in Docker, substitute docker run thesrc for every instance of thesrc. (Also note that you'll have to pass Docker the necessary PG* environment variables to connect to the PostgreSQL database.)

Running

First, set the PG* environment variables so that psql works.

Then run these commands to create the DB, import posts from other sites, and classify their links:

# start the server:
thesrc -url=http://localhost:5000 serve

# then, in a separate terminal window, run:
thesrc -url=http://localhost:5000 createdb
thesrc -url=http://localhost:5000 import
thesrc -url=http://localhost:5000 classify

# now open your browser to localhost:5000

Documentation

Index

Constants

View Source
const DefaultPerPage = 10

DefaultPerPage is the default number of items to return per page in a paginated result set.

Variables

View Source
var (
	ErrPostNotFound = errors.New("post not found")
)

Functions

func CheckResponse

func CheckResponse(r *http.Response) error

CheckResponse checks the API response for errors, and returns them if present. A response is considered an error if it has a status code outside the 200 range. API error responses are expected to have either no response body, or a JSON response body that maps to ErrorResponse. Any other response body will be silently ignored.

func IsHTTPErrorCode

func IsHTTPErrorCode(err error, statusCode int) bool

Types

type Client

type Client struct {
	Posts PostsService

	// BaseURL for HTTP requests to thesrc's API.
	BaseURL *url.URL

	//UserAgent used for HTTP requests to thesrc's API.
	UserAgent string
	// contains filtered or unexported fields
}

A Client communicates with thesrc's HTTP API.

func NewClient

func NewClient(httpClient *http.Client) *Client

NewClient creates a new HTTP API client for thesrc. If httpClient == nil, then http.DefaultClient is used.

func (*Client) Do

func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The API response is JSON-decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.

func (*Client) NewRequest

func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error)

NewRequest creates an API request. A relative URL can be provided in urlStr, in which case it is resolved relative to the BaseURL of the Client. Relative URLs should always be specified without a preceding slash. If specified, the value pointed to by body is JSON encoded and included as the request body.

type ErrorResponse

type ErrorResponse struct {
	Response *http.Response `json:",omitempty"`
	Message  string
}

An ErrorResponse reports errors caused by an API request.

func (*ErrorResponse) Error

func (r *ErrorResponse) Error() string

func (*ErrorResponse) HTTPStatusCode

func (r *ErrorResponse) HTTPStatusCode() int

type ListOptions

type ListOptions struct {
	PerPage int `url:",omitempty" json:",omitempty"`
	Page    int `url:",omitempty" json:",omitempty"`
}

ListOptions specifies general pagination options for fetching a list of results.

func (ListOptions) Offset

func (o ListOptions) Offset() int

func (ListOptions) PageOrDefault

func (o ListOptions) PageOrDefault() int

func (ListOptions) PerPageOrDefault

func (o ListOptions) PerPageOrDefault() int

type MockPostsService

type MockPostsService struct {
	Get_    func(id int) (*Post, error)
	List_   func(opt *PostListOptions) ([]*Post, error)
	Submit_ func(post *Post) (bool, error)
}

func (*MockPostsService) Get

func (s *MockPostsService) Get(id int) (*Post, error)

func (*MockPostsService) List

func (s *MockPostsService) List(opt *PostListOptions) ([]*Post, error)

func (*MockPostsService) Submit

func (s *MockPostsService) Submit(post *Post) (bool, error)

type Post

type Post struct {
	// ID a unique identifier for this post.
	ID int `json:",omitempty"`

	// Title of the post.
	Title string

	// LinkURL is the URL to a link that this post is about.
	LinkURL string

	// Body of the post.
	Body string

	// SubmittedAt is when the post was submitted.
	SubmittedAt time.Time

	// AuthorUserID is the user ID of this post's author.
	AuthorUserID int

	// Score in points.
	Score int

	// Classification is the output of the classifier on this post.
	Classification string
}

A Post is a link and short body submitted to and displayed on thesrc.

type PostListOptions

type PostListOptions struct {
	// CodeOnly filters the result set to only those posts whose links contain code.
	CodeOnly bool

	ListOptions
}

type PostsService

type PostsService interface {
	// Get a post.
	Get(id int) (*Post, error)

	// List posts.
	List(opt *PostListOptions) ([]*Post, error)

	// Submit a post. If this post's link URL has never been submitted, post.ID
	// will be a new ID, and created will be true. If it has been submitted
	// before, post.ID will be the ID of the previous post, and created will be
	// false.
	Submit(post *Post) (created bool, err error)
}

PostsService interacts with the post-related endpoints in thesrc's API.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL