Skip to content

Commit

Permalink
fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed May 13, 2015
1 parent 5c009b2 commit 125ad5f
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions apage/apage.go
Expand Up @@ -2,10 +2,11 @@ package apage

import (
"container/list"
"fmt"
"io"
"math/rand"
"net/http"
"path"
"rand"
"strconv"

"github.com/bobappleyard/bwl/actor"
Expand Down Expand Up @@ -45,7 +46,7 @@ func (self *AnonymousPageServer) Handle(h http.Handler) string {
// clear cache
for self.items.Len() >= self.limit {
e := self.items.Back()
self.paths[e.Value.(int64)] = nil, false
delete(self.paths, e.Value.(int64))
self.items.Remove(e)
}
// generate a key
Expand All @@ -61,37 +62,37 @@ func (self *AnonymousPageServer) Handle(h http.Handler) string {
self.items.PushFront(key)
self.paths[key] = h
// and return the path
return self.prefix + strconv.Itoa64(key)
return self.prefix + fmt.Sprint(key)
}).(string)
}

// A shortcut for function pages (which should be most of them)
func (self *AnonymousPageServer) Create(h func(c *http.Conn, r *http.Request)) string {
func (self *AnonymousPageServer) Create(h func(w http.ResponseWriter, r *http.Request)) string {
return self.Handle(http.HandlerFunc(h))
}

func (self *AnonymousPageServer) getPage(id int64) http.Handler {
return self.a.Schedule(func() interface{} {
page, ok := self.paths[id]
if !ok {
return http.HandlerFunc(func(c *http.Conn, r *http.Request) {
c.WriteHeader(http.StatusNotFound)
io.WriteString(c, "page is not in cache")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, "page is not in cache")
})
}
return page
}).(http.Handler)
}

func (self *AnonymousPageServer) Attach(s *http.ServeMux) {
s.Handle(self.prefix, http.HandlerFunc(func(c *http.Conn, r *http.Request) {
s.Handle(self.prefix, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, name := path.Split(r.URL.Path)
id, err := strconv.Atoi64(name)
id, err := strconv.ParseInt(name, 10, 64)
if err != nil {
c.WriteHeader(http.StatusBadRequest)
io.WriteString(c, "invalid page id")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, "invalid page id")
} else {
self.getPage(id).ServeHTTP(c, r)
self.getPage(id).ServeHTTP(w, r)
}
}))
}
Expand All @@ -110,6 +111,6 @@ func Handle(h http.Handler) string {
return server.Handle(h)
}

func Create(h func(c *http.Conn, r *http.Request)) string {
func Create(h func(w http.ResponseWriter, r *http.Request)) string {
return server.Create(h)
}

0 comments on commit 125ad5f

Please sign in to comment.