Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "net/url" "github.com/n0trace/photon" ) func main() { p := photon.New() p.Get(newTestServer().URL+"/users?id=hello", func(ctx photon.Context) { text, _ := ctx.Text() fmt.Println(text) }) p.Wait() } func newTestServer() *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { u, err := url.Parse(r.RequestURI) if err != nil { panic(err) } w.Write([]byte(u.Query().Get("id"))) }) mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{Name: "username", Value: r.FormValue("username"), Path: "/", MaxAge: 86400} http.SetCookie(w, &cookie) w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) }) mux.HandleFunc("/must-login", func(w http.ResponseWriter, r *http.Request) { cookie, _ := r.Cookie("username") w.Write([]byte("hello " + cookie.Value)) }) mux.HandleFunc("/user-agent", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.Header.Get("User-Agent"))) }) return httptest.NewServer(mux) }
Output: hello
Example (KeepAuth) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "net/url" "strings" "github.com/n0trace/photon" "github.com/n0trace/photon/middleware" ) func main() { p := photon.New() reader := strings.NewReader("username=foo&password=bar") lastCtx := p.Post(newTestServer().URL+"/login", "application/x-www-form-urlencoded", reader, func(ctx photon.Context) { text, _ := ctx.Text() fmt.Println(text) }) p.Get(newTestServer().URL+"/must-login", func(ctx photon.Context) { text, _ := ctx.Text() fmt.Println(text) }, middleware.FromContext(lastCtx)) p.Wait() } func newTestServer() *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { u, err := url.Parse(r.RequestURI) if err != nil { panic(err) } w.Write([]byte(u.Query().Get("id"))) }) mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{Name: "username", Value: r.FormValue("username"), Path: "/", MaxAge: 86400} http.SetCookie(w, &cookie) w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) }) mux.HandleFunc("/must-login", func(w http.ResponseWriter, r *http.Request) { cookie, _ := r.Cookie("username") w.Write([]byte("hello " + cookie.Value)) }) mux.HandleFunc("/user-agent", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.Header.Get("User-Agent"))) }) return httptest.NewServer(mux) }
Output: ok hello foo
Example (UseMiddleware) ¶
package main import ( "fmt" "net/http" "net/http/httptest" "net/url" "time" "github.com/n0trace/photon" "github.com/n0trace/photon/middleware" ) func main() { rootURL := newTestServer().URL p := photon.New() p.Use(middleware.Limit(200*time.Millisecond), middleware.UserAgent("diy-agent")) for i := 0; i != 3; i++ { url := fmt.Sprintf("%s/user-agent", rootURL) p.Get(url, func(ctx photon.Context) { text, _ := ctx.Text() fmt.Println(text) }) } //or: //p.Get(url,callback,middleware...) p.Wait() } func newTestServer() *httptest.Server { mux := http.NewServeMux() mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) { u, err := url.Parse(r.RequestURI) if err != nil { panic(err) } w.Write([]byte(u.Query().Get("id"))) }) mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{Name: "username", Value: r.FormValue("username"), Path: "/", MaxAge: 86400} http.SetCookie(w, &cookie) w.WriteHeader(http.StatusOK) w.Write([]byte("ok")) }) mux.HandleFunc("/must-login", func(w http.ResponseWriter, r *http.Request) { cookie, _ := r.Cookie("username") w.Write([]byte("hello " + cookie.Value)) }) mux.HandleFunc("/user-agent", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(r.Header.Get("User-Agent"))) }) return httptest.NewServer(mux) }
Output: diy-agent diy-agent diy-agent
Index ¶
- type Context
- type HandlerFunc
- type Jar
- type MiddlewareFunc
- type Photon
- func (p *Photon) Get(url string, handle HandlerFunc, middlewares ...MiddlewareFunc) Context
- func (p *Photon) NewContext() *context
- func (p *Photon) NewStdClient() *http.Client
- func (p *Photon) Post(url string, contentType string, body io.Reader, handle HandlerFunc, ...) Context
- func (p *Photon) Request(req *http.Request, cb HandlerFunc, middleware ...MiddlewareFunc) Context
- func (p *Photon) SetParallel(parallel int)
- func (p *Photon) Use(middleware ...MiddlewareFunc)
- func (p *Photon) Wait()
- type Response
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface { Request() *http.Request SetRequest(*http.Request) Client() *http.Client SetClient(*http.Client) Reset() Error() error SetError(error) SetStdResponse(*http.Response) StdResponse() *http.Response Text() (string, error) Bytes() ([]byte, error) JSON(interface{}) error XML(interface{}) error Document() (*goquery.Document, error) Photon() *Photon Set(string, interface{}) Get(string) (interface{}, bool) FromContext(interface{}) Downloaded() bool SetDownload(bool) Finish() WaitFinish() }
type HandlerFunc ¶
type HandlerFunc func(Context)
type MiddlewareFunc ¶
type MiddlewareFunc func(HandlerFunc) HandlerFunc
type Photon ¶
type Photon struct {
// contains filtered or unexported fields
}
func (*Photon) Get ¶
func (p *Photon) Get(url string, handle HandlerFunc, middlewares ...MiddlewareFunc) Context
Get Get
func (*Photon) NewContext ¶
func (p *Photon) NewContext() *context
func (*Photon) NewStdClient ¶
func (*Photon) Post ¶
func (p *Photon) Post(url string, contentType string, body io.Reader, handle HandlerFunc, middlewares ...MiddlewareFunc) Context
Post Post
func (*Photon) Request ¶
func (p *Photon) Request(req *http.Request, cb HandlerFunc, middleware ...MiddlewareFunc) Context
Request Request
func (*Photon) Use ¶
func (p *Photon) Use(middleware ...MiddlewareFunc)
type Response ¶
Response wraps an http.ResponseWriter and implements its interface to be used
Click to show internal directories.
Click to hide internal directories.