Skip to content

Commit

Permalink
huge breaking change - break everything up into subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
jtolio committed Dec 27, 2016
1 parent 39bdf72 commit 0770d15
Show file tree
Hide file tree
Showing 34 changed files with 1,055 additions and 763 deletions.
39 changes: 0 additions & 39 deletions close17.go

This file was deleted.

20 changes: 0 additions & 20 deletions close18.go

This file was deleted.

35 changes: 0 additions & 35 deletions compat17.go

This file was deleted.

101 changes: 0 additions & 101 deletions errors.go

This file was deleted.

33 changes: 20 additions & 13 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ package webhelp_test
import (
"fmt"
"net/http"
"testing"

"github.com/jtolds/webhelp"
"github.com/jtolds/webhelp/whcompat"
"github.com/jtolds/webhelp/whlog"
"github.com/jtolds/webhelp/whmux"
)

func Example(t *testing.T) {
pageName := webhelp.NewStringArgMux()
handler := webhelp.DirMux{
"wiki": pageName.Shift(webhelp.Exact(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
name := pageName.Get(r.Context())
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Welcome to %s", name)
})))}

webhelp.ListenAndServe(":0", handler)
var (
pageName = whmux.NewStringArg()
)

func page(w http.ResponseWriter, r *http.Request) {
name := pageName.Get(whcompat.Context(r))

w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Welcome to %s", name)
}

func Example() {
pageHandler := pageName.Shift(whmux.Exact(http.HandlerFunc(page)))

whlog.ListenAndServe(":0", whmux.Dir{
"wiki": pageHandler,
})
}
81 changes: 0 additions & 81 deletions fatal.go

This file was deleted.

53 changes: 20 additions & 33 deletions gensym.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,25 @@
package webhelp

import (
"github.com/spacemonkeygo/errors"
"sync"
)

// GenSym generates a brand new, never-before-seen symbol for use as a
// Context.WithValue key.
//
// Example usage:
//
// var UserKey = webhelp.GenSym()
//
// func myWrapper(h http.Handler) http.Handler {
// return webhelp.RouteHandlerFunc(h,
// func(w http.ResponseWriter, r *http.Request) {
// user, err := loadUser(r)
// if err != nil {
// webhelp.HandleError(w, r, err)
// return
// }
// h.ServeHTTP(w, webhelp.WithContext(r,
// context.WithValue(webhelp.Context(r), UserKey, user)))
// })
// }
//
// func myHandler(w http.ResponseWriter, r *http.Request) {
// ctx := webhelp.Context(r)
// if user, ok := ctx.Value(UserKey).(*User); ok {
// // do something with the user
// }
// }
//
// func Routes() http.Handler {
// return myWrapper(http.HandlerFunc(myHandler))
// }
//
func GenSym() interface{} { return errors.GenSym() }
var (
keyMtx sync.Mutex
keyCounter uint64
)

// ContextKey is only useful via the GenSym() constructor. See GenSym() for
// more documentation
type ContextKey struct {
id uint64
}

// GenSym generates a brand new, never-before-seen ContextKey for use as a
// Context.WithValue key. Please see the example.
func GenSym() ContextKey {
keyMtx.Lock()
defer keyMtx.Unlock()
keyCounter += 1
return ContextKey{id: keyCounter}
}
Loading

0 comments on commit 0770d15

Please sign in to comment.