6fdf615cf5
The tests and benchmarks now run again. gloinit() is always called prior to the tests and benchmarks now. The tests and benchmarks no longer use hard-coded session strings for admin route tests. Added some new route tests. We now pull the database version into a variable. Fixed an issue with guest perms not applying properly. Tweaked the router to make it a little more efficient. Moved more topic / user parsing logic into CascadeGet Profiles now use the user cache. Added the Set method to the caches. Set is used when you don't know if an item exists in a cache or not. Added the Load method to the caches. Load is used to forcefully reload an item in a cache from the database.
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package main
|
|
//import "fmt"
|
|
import "strings"
|
|
import "sync"
|
|
import "net/http"
|
|
|
|
type Router struct {
|
|
mu sync.RWMutex
|
|
routes map[string]func(http.ResponseWriter, *http.Request)
|
|
}
|
|
|
|
func NewRouter() *Router {
|
|
return &Router{
|
|
routes: make(map[string]func(http.ResponseWriter, *http.Request)),
|
|
}
|
|
}
|
|
|
|
func (router *Router) Handle(pattern string, handle http.Handler) {
|
|
router.mu.Lock()
|
|
router.routes[pattern] = handle.ServeHTTP
|
|
router.mu.Unlock()
|
|
}
|
|
|
|
func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
|
|
router.mu.Lock()
|
|
router.routes[pattern] = handle
|
|
router.mu.Unlock()
|
|
}
|
|
|
|
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
if req.URL.Path[0] != '/' {
|
|
w.WriteHeader(405)
|
|
w.Write([]byte(""))
|
|
return
|
|
}
|
|
|
|
router.mu.RLock()
|
|
handle, ok := router.routes[req.URL.Path]
|
|
if ok {
|
|
router.mu.RUnlock()
|
|
handle(w,req)
|
|
return
|
|
}
|
|
|
|
if req.URL.Path[len(req.URL.Path) - 1] == '/' {
|
|
router.mu.RUnlock()
|
|
NotFound(w,req)
|
|
return
|
|
}
|
|
|
|
handle, ok = router.routes[req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/') + 1]]
|
|
if ok {
|
|
router.mu.RUnlock()
|
|
handle(w,req)
|
|
return
|
|
}
|
|
//fmt.Println(req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')])
|
|
|
|
handle, ok = router.routes[req.URL.Path + "/"]
|
|
if ok {
|
|
router.mu.RUnlock()
|
|
handle(w,req)
|
|
return
|
|
}
|
|
|
|
router.mu.RUnlock()
|
|
NotFound(w,req)
|
|
return
|
|
} |