gosora/old_router.go

68 lines
1.8 KiB
Go
Raw Normal View History

2021-03-23 08:17:07 +00:00
/* Obsoleted by gen_router.go :( */
package main
//import "fmt"
import (
2022-02-21 03:32:53 +00:00
"net/http"
"strings"
"sync"
2021-03-23 08:17:07 +00:00
2022-02-21 03:32:53 +00:00
"github.com/Azareal/Gosora/common"
2021-03-23 08:17:07 +00:00
)
// 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 {
2022-02-21 03:32:53 +00:00
sync.RWMutex
routes map[string]func(http.ResponseWriter, *http.Request)
2021-03-23 08:17:07 +00:00
}
// nolint
func NewRouter() *Router {
2022-02-21 03:32:53 +00:00
return &Router{
routes: make(map[string]func(http.ResponseWriter, *http.Request)),
}
2021-03-23 08:17:07 +00:00
}
// nolint
func (router *Router) Handle(pattern string, handle http.Handler) {
2022-02-21 03:32:53 +00:00
router.Lock()
router.routes[pattern] = handle.ServeHTTP
router.Unlock()
2021-03-23 08:17:07 +00:00
}
// nolint
func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) {
2022-02-21 03:32:53 +00:00
router.Lock()
router.routes[pattern] = handle
router.Unlock()
2021-03-23 08:17:07 +00:00
}
// nolint
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
2022-02-21 03:32:53 +00:00
if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' {
w.WriteHeader(405)
w.Write([]byte(""))
return
}
2021-03-23 08:17:07 +00:00
2022-02-21 03:32:53 +00:00
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
}
2021-03-23 08:17:07 +00:00
2022-02-21 03:32:53 +00:00
router.RLock()
handle, ok := router.routes[prefix]
router.RUnlock()
2021-03-23 08:17:07 +00:00
2022-02-21 03:32:53 +00:00
if ok {
handle(w, req)
return
}
//log.Print("req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]",req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')])
common.NotFound(w, req,nil)
2021-03-23 08:17:07 +00:00
}