github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/old_router.go (about) 1 /* Obsoleted by gen_router.go :( */ 2 package main 3 4 //import "fmt" 5 import ( 6 "net/http" 7 "strings" 8 "sync" 9 10 "github.com/Azareal/Gosora/common" 11 ) 12 13 // TODO: Support the new handler signatures created by our efforts to move the PreRoute middleware into the generated router 14 // nolint Stop linting the uselessness of this file, we never know when we might need this file again 15 type Router struct { 16 sync.RWMutex 17 routes map[string]func(http.ResponseWriter, *http.Request) 18 } 19 20 // nolint 21 func NewRouter() *Router { 22 return &Router{ 23 routes: make(map[string]func(http.ResponseWriter, *http.Request)), 24 } 25 } 26 27 // nolint 28 func (router *Router) Handle(pattern string, handle http.Handler) { 29 router.Lock() 30 router.routes[pattern] = handle.ServeHTTP 31 router.Unlock() 32 } 33 34 // nolint 35 func (router *Router) HandleFunc(pattern string, handle func(http.ResponseWriter, *http.Request)) { 36 router.Lock() 37 router.routes[pattern] = handle 38 router.Unlock() 39 } 40 41 // nolint 42 func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { 43 if len(req.URL.Path) == 0 || req.URL.Path[0] != '/' { 44 w.WriteHeader(405) 45 w.Write([]byte("")) 46 return 47 } 48 49 var /*extraData, */ prefix string 50 if req.URL.Path[len(req.URL.Path)-1] != '/' { 51 //extraData = req.URL.Path[strings.LastIndexByte(req.URL.Path,'/') + 1:] 52 prefix = req.URL.Path[:strings.LastIndexByte(req.URL.Path, '/')+1] 53 } else { 54 prefix = req.URL.Path 55 } 56 57 router.RLock() 58 handle, ok := router.routes[prefix] 59 router.RUnlock() 60 61 if ok { 62 handle(w, req) 63 return 64 } 65 //log.Print("req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]",req.URL.Path[:strings.LastIndexByte(req.URL.Path,'/')]) 66 common.NotFound(w, req,nil) 67 }