Skip to content

Commit 1a740cd

Browse files
committed
Added more method docs.
This is in preparation for the new godoc-driven docs page.
1 parent 25edc21 commit 1a740cd

File tree

1 file changed

+77
-46
lines changed

1 file changed

+77
-46
lines changed

web.go

+77-46
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
// Package web is a lightweight web framework for Go. It's ideal for
3+
// writing simple, performant backend web services.
14
package web
25

36
import (
@@ -31,39 +34,51 @@ type ResponseWriter interface {
3134
Close()
3235
}
3336

37+
// A Context object is created for every incoming HTTP request, and is
38+
// passed to handlers as an optional first argument. It provides information
39+
// about the request, including the http.Request object, the GET and POST params,
40+
// and acts as a Writer for the response.
3441
type Context struct {
3542
Request *http.Request
3643
Params map[string]string
3744
Server *Server
3845
ResponseWriter
3946
}
4047

48+
// WriteString writes string data into the response object.
4149
func (ctx *Context) WriteString(content string) {
4250
ctx.ResponseWriter.Write([]byte(content))
4351
}
4452

53+
// Abort is a helper method that sends an HTTP header and an optional
54+
// body. It is useful for returning 4xx or 5xx errors.
55+
// Once it has been called, any return value from the handler will
56+
// not be written to the response.
4557
func (ctx *Context) Abort(status int, body string) {
4658
ctx.ResponseWriter.WriteHeader(status)
4759
ctx.ResponseWriter.Write([]byte(body))
4860
}
4961

62+
// Redirect is a helper method for 3xx redirects.
5063
func (ctx *Context) Redirect(status int, url_ string) {
5164
ctx.ResponseWriter.Header().Set("Location", url_)
5265
ctx.ResponseWriter.WriteHeader(status)
5366
ctx.ResponseWriter.Write([]byte("Redirecting to: " + url_))
5467
}
5568

69+
// Notmodified writes a 304 HTTP response
5670
func (ctx *Context) NotModified() {
5771
ctx.ResponseWriter.WriteHeader(304)
5872
}
5973

74+
// NotFound writes a 404 HTTP response
6075
func (ctx *Context) NotFound(message string) {
6176
ctx.ResponseWriter.WriteHeader(404)
6277
ctx.ResponseWriter.Write([]byte(message))
6378
}
6479

65-
//Sets the content type by extension, as defined in the mime package.
66-
//For example, ctx.ContentType("json") sets the content-type to "application/json"
80+
// Sets the content type by extension, as defined in the mime package.
81+
// For example, ctx.ContentType("json") sets the content-type to "application/json"
6782
func (ctx *Context) ContentType(ext string) {
6883
if !strings.HasPrefix(ext, ".") {
6984
ext = "." + ext
@@ -74,6 +89,8 @@ func (ctx *Context) ContentType(ext string) {
7489
}
7590
}
7691

92+
// SetHeader sets a response header. If `unique` is true, the current value
93+
// of that header will be overwritten . If false, it will be appended.
7794
func (ctx *Context) SetHeader(hdr string, val string, unique bool) {
7895
if unique {
7996
ctx.Header().Set(hdr, val)
@@ -82,7 +99,8 @@ func (ctx *Context) SetHeader(hdr string, val string, unique bool) {
8299
}
83100
}
84101

85-
//Sets a cookie -- duration is the amount of time in seconds. 0 = forever
102+
// SetCookie sets a cookie header. Duration is specified in seconds. If the duration
103+
// is zero, the cookie is permanent.
86104
func (ctx *Context) SetCookie(name string, value string, age int64) {
87105
var utctime time.Time
88106
if age == 0 {
@@ -204,6 +222,8 @@ type responseWriter struct {
204222
http.ResponseWriter
205223
}
206224

225+
// Close terminates the HTTP connection, and flushes all pending
226+
// response data.
207227
func (c *responseWriter) Close() {
208228
rwc, buf, _ := c.ResponseWriter.(http.Hijacker).Hijack()
209229
if buf != nil {
@@ -220,7 +240,7 @@ func (s *Server) ServeHTTP(c http.ResponseWriter, req *http.Request) {
220240
s.routeHandler(req, &w)
221241
}
222242

223-
//Calls a function with recover block
243+
// safelyCall invokes `function` in recover block
224244
func (s *Server) safelyCall(function reflect.Value, args []reflect.Value) (resp []reflect.Value, e interface{}) {
225245
defer func() {
226246
if err := recover(); err != nil {
@@ -244,7 +264,8 @@ func (s *Server) safelyCall(function reflect.Value, args []reflect.Value) (resp
244264
return function.Call(args), nil
245265
}
246266

247-
//should the context be passed to the handler?
267+
// requiresContext determines whether 'handlerType' contains
268+
// an argument to 'web.Ctx' as its first argument
248269
func requiresContext(handlerType reflect.Type) bool {
249270
//if the method doesn't take arguments, no
250271
if handlerType.NumIn() == 0 {
@@ -264,6 +285,7 @@ func requiresContext(handlerType reflect.Type) bool {
264285
return false
265286
}
266287

288+
// the main route handler in web.go
267289
func (s *Server) routeHandler(req *http.Request, w ResponseWriter) {
268290
requestPath := req.URL.Path
269291
ctx := Context{req, map[string]string{}, s, w}
@@ -365,12 +387,16 @@ func (s *Server) routeHandler(req *http.Request, w ResponseWriter) {
365387
ctx.Abort(404, "Page not found")
366388
}
367389

368-
var Config = &ServerConfig{
369-
RecoverPanic: true,
390+
// ServerConfig is configuration for server objects.
391+
type ServerConfig struct {
392+
StaticDir string
393+
Addr string
394+
Port int
395+
CookieSecret string
396+
RecoverPanic bool
370397
}
371398

372-
var mainServer = NewServer()
373-
399+
// Server represents a web.go server.
374400
type Server struct {
375401
Config *ServerConfig
376402
routes []route
@@ -398,7 +424,7 @@ func (s *Server) initServer() {
398424
}
399425
}
400426

401-
//Runs the web application and serves http requests
427+
// Run starts the web application and serves HTTP requests for s
402428
func (s *Server) Run(addr string) {
403429
s.initServer()
404430

@@ -420,7 +446,12 @@ func (s *Server) Run(addr string) {
420446
s.l.Close()
421447
}
422448

423-
//Runs the web application and serves https requests
449+
// Run starts the web application and serves HTTP requests for the main server.
450+
func Run(addr string) {
451+
mainServer.Run(addr)
452+
}
453+
454+
// RunTLS starts the web application and serves HTTPS requests for s.
424455
func (s *Server) RunTLS(addr string, config *tls.Config) error {
425456
s.initServer()
426457
mux := http.NewServeMux()
@@ -435,107 +466,105 @@ func (s *Server) RunTLS(addr string, config *tls.Config) error {
435466
return http.Serve(s.l, mux)
436467
}
437468

438-
//Runs the web application and serves http requests
439-
func Run(addr string) {
440-
mainServer.Run(addr)
441-
}
442-
443-
//Runs the secure web application and serves https requests
469+
// RunTLS starts the web application and serves HTTPS requests for the main server.
444470
func RunTLS(addr string, config *tls.Config) {
445471
mainServer.RunTLS(addr, config)
446472
}
447473

448-
//Stops the web server
449-
func (s *Server) Close() {
450-
if s.l != nil {
451-
s.l.Close()
452-
}
453-
}
454-
455-
//Stops the web server
456-
func Close() {
457-
mainServer.Close()
458-
}
459-
474+
// RunScgi starts the web application and serves SCGI requests for s.
460475
func (s *Server) RunScgi(addr string) {
461476
s.initServer()
462477
s.Logger.Printf("web.go serving scgi %s\n", addr)
463478
s.listenAndServeScgi(addr)
464479
}
465480

466-
//Runs the web application and serves scgi requests
481+
// RunScgi starts the web application and serves SCGI requests for the main server.
467482
func RunScgi(addr string) {
468483
mainServer.RunScgi(addr)
469484
}
470485

471-
//Runs the web application and serves scgi requests for this Server object.
486+
// RunFcgi starts the web application and serves FastCGI requests for s.
472487
func (s *Server) RunFcgi(addr string) {
473488
s.initServer()
474489
s.Logger.Printf("web.go serving fcgi %s\n", addr)
475490
s.listenAndServeFcgi(addr)
476491
}
477492

478-
//Runs the web application by serving fastcgi requests
493+
// RunFcgi starts the web application and serves FastCGI requests for the main server.
479494
func RunFcgi(addr string) {
480495
mainServer.RunFcgi(addr)
481496
}
482497

483-
//Adds a handler for the 'GET' http method.
498+
// Close stops server s.
499+
func (s *Server) Close() {
500+
if s.l != nil {
501+
s.l.Close()
502+
}
503+
}
504+
505+
// Close stops the main server.
506+
func Close() {
507+
mainServer.Close()
508+
}
509+
510+
// Get adds a handler for the 'GET' http method for server s.
484511
func (s *Server) Get(route string, handler interface{}) {
485512
s.addRoute(route, "GET", handler)
486513
}
487514

488-
//Adds a handler for the 'POST' http method.
515+
// Post adds a handler for the 'POST' http method for server s.
489516
func (s *Server) Post(route string, handler interface{}) {
490517
s.addRoute(route, "POST", handler)
491518
}
492519

493-
//Adds a handler for the 'PUT' http method.
520+
// Put adds a handler for the 'PUT' http method for server s.
494521
func (s *Server) Put(route string, handler interface{}) {
495522
s.addRoute(route, "PUT", handler)
496523
}
497524

498-
//Adds a handler for the 'DELETE' http method.
525+
// Delete adds a handler for the 'DELETE' http method for server s.
499526
func (s *Server) Delete(route string, handler interface{}) {
500527
s.addRoute(route, "DELETE", handler)
501528
}
502529

503-
//Adds a handler for the 'GET' http method.
530+
// Get adds a handler for the 'GET' http method in the main server.
504531
func Get(route string, handler interface{}) {
505532
mainServer.Get(route, handler)
506533
}
507534

508-
//Adds a handler for the 'POST' http method.
535+
// Post adds a handler for the 'POST' http method in the main server.
509536
func Post(route string, handler interface{}) {
510537
mainServer.addRoute(route, "POST", handler)
511538
}
512539

513-
//Adds a handler for the 'PUT' http method.
540+
// Put adds a handler for the 'PUT' http method in the main server.
514541
func Put(route string, handler interface{}) {
515542
mainServer.addRoute(route, "PUT", handler)
516543
}
517544

518-
//Adds a handler for the 'DELETE' http method.
545+
// Delete adds a handler for the 'DELETE' http method in the main server.
519546
func Delete(route string, handler interface{}) {
520547
mainServer.addRoute(route, "DELETE", handler)
521548
}
522549

550+
// SetLogger sets the logger for server s
523551
func (s *Server) SetLogger(logger *log.Logger) {
524552
s.Logger = logger
525553
}
526554

555+
// SetLogger sets the logger for the main server.
527556
func SetLogger(logger *log.Logger) {
528557
mainServer.Logger = logger
529558
}
530559

531-
type ServerConfig struct {
532-
StaticDir string
533-
Addr string
534-
Port int
535-
CookieSecret string
536-
RecoverPanic bool
560+
// Config is the configuration of the main server.
561+
var Config = &ServerConfig{
562+
RecoverPanic: true,
537563
}
538564

565+
var mainServer = NewServer()
566+
567+
// internal utility methods
539568
func webTime(t time.Time) string {
540569
ftime := t.Format(time.RFC1123)
541570
if strings.HasSuffix(ftime, "UTC") {
@@ -565,6 +594,8 @@ func fileExists(dir string) bool {
565594
return !info.IsDir()
566595
}
567596

597+
// Urlencode is a helper method that converts a map into URL-encoded form data.
598+
// It is a useful when constructing HTTP POST requests.
568599
func Urlencode(data map[string]string) string {
569600
var buf bytes.Buffer
570601
for k, v := range data {

0 commit comments

Comments
 (0)