gosora/router.go
Azareal 91f70d2a4a Add a per-user theme switcher. The CSS might be slightly broken in the themes, that'll be fixed in a follow-up commit.
Added basic support for server sync.

Re-added a few missing defers.
Renamed TO-DO to TODO across the entire codebase.
Renamed StaticForumStore to MemoryForumStore.
The ForumStore is now built on a sync.Map with a view slice for generating /forums rather than a slice.
Renamed many more functions and variables to satisfy the linter.
increase_post_user_stats() and decrease_post_user_stats() are now methods on the User struct. We also fix a bug where they take the moderator's score rather than the target user's into account when recalculating their level after a post / topic is deleted.
Transitioned the topic list to CSS Grid for Tempra Simple, with a float fallback.
Cosmo and Cosmo Conflux are now hidden from the theme list.
Fixed more data races.
Added more debug data to the template compiler logs.
2017-09-10 17:57:22 +01:00

64 lines
1.6 KiB
Go

/* Obsoleted by gen_router.go :( */
package main
//import "fmt"
import "strings"
import "sync"
import "net/http"
// TODO: Support the new handler signatures created by our efforts to move the PreRoute middleware into the generated router
// nolint Stop linting the uselessness of this file, we never know when we might need this file again
type Router struct {
sync.RWMutex
routes map[string]func(http.ResponseWriter, *http.Request)
}
// nolint
func NewRouter() *Router {
return &Router{
routes: make(map[string]func(http.ResponseWriter, *http.Request)),
}
}
// nolint
func (router *Router) Handle(pattern string, handle http.Handler) {
router.Lock()
router.routes[pattern] = handle.ServeHTTP
router.Unlock()
}
// nolint
func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
router.Lock()
router.routes[pattern] = handle
router.Unlock()
}
// nolint
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' {
w.WriteHeader(405)
w.Write([]byte(""))
return
}
var /*extraData, */ prefix string
if req.URL.Path[len(req.URL.Path)-1] != '/' {
//extraData = req.URL.Path[strings.LastIndexByte(req.URL.Path,'/') + 1:]
prefix = req.URL.Path[:strings.LastIndexByte(req.URL.Path, '/')+1]
} else {
prefix = req.URL.Path
}
router.RLock()
handle, ok := router.routes[prefix]
router.RUnlock()
if ok {
handle(w, req)
return
}
//log.Print("req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]",req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')])
NotFound(w, req)
}