Documentation ¶
Index ¶
- func WriteJSON(w http.ResponseWriter, v interface{})
- func WriteString(w http.ResponseWriter, format string, args ...interface{})
- type Context
- type Handle
- type ResponseWriter
- type Server
- func (s *Server) Delete(path string, handle Handle)
- func (s *Server) Get(path string, handle Handle)
- func (s *Server) Handle(method, path string, handle Handle)
- func (s *Server) Head(path string, handle Handle)
- func (s *Server) Post(path string, handle Handle)
- func (s *Server) Put(path string, handle Handle)
- func (s *Server) ServeHTTP(hw http.ResponseWriter, r *http.Request)
- func (s *Server) Use(middleware http.Handler)
- func (s *Server) UseAfter(middleware http.Handler)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, v interface{})
WriteJSON is a helper function that will write the JSON encoded version of v to the ResponseWriter and set the application/json Content-Type header.
func WriteString ¶
func WriteString(w http.ResponseWriter, format string, args ...interface{})
WriteString is a Printf-like helper function that will write a text/plain header and string to the ResponseWriter.
Types ¶
type Context ¶
The Context interface allows you to implement a shared context that is needed among a group of request types. NewContext should process the Request for any information that is necessary for a particular request type. For example, an endpoint that requires authentication could parse the authentication header, or a session cookie, and set User in the context. The idea is that Context is not generated by stom because it may not be needed in every request. In your request handler simply call NewContext(r) when you need it.
A simple example:
type User struct { ID int Name string } type Context struct { User *User } func NewContext(r *http.Request) *Context { ctx := new(Context) // Error handling omitted for simplicity sessionID, _ := r.Cookie("session_id") user, _ := FetchSessionUser(sessionID) ctx.User = user return ctx } func HelloName(w http.ResponseWriter, r *http.Request) { ctx := NewContext(r) if ctx.User != nil { fmt.Fprintf(w, "Hello, %s", ctx.User.Name) } else { fmt.Fprintf(w, "Hello, I don't know who you are!") } }
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter Status int StartTime time.Time // contains filtered or unexported fields }
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(status int)