Compare commits

...

4 Commits

Author SHA1 Message Date
c5ab1debdb stop: use graceful shutdown for 5s 2024-09-26 23:05:05 +02:00
c0fea38790 stop: use graceful shutdown 2024-09-26 22:58:55 +02:00
b98513ad36 stop: catch signals 2024-09-26 22:27:26 +02:00
0786a97a77 context: Get params from url 2024-09-26 21:15:15 +02:00
6 changed files with 68 additions and 13 deletions

View File

@ -19,7 +19,7 @@ type Context struct {
// current handler index // current handler index
index int index int
params map[string]any params map[string]string
hasTimeout bool hasTimeout bool
writerMux *sync.Mutex writerMux *sync.Mutex
@ -117,4 +117,8 @@ func (ctx *Context) SetHandlers(handlers []ControllerHandler) {
ctx.handlers = handlers ctx.handlers = handlers
} }
func (ctx *Context) SetParams(params map[string]string) {
ctx.params = params
}
// }}} // }}}

View File

@ -66,7 +66,7 @@ func (c *Core) Use(middlewares ...ControllerHandler) {
} }
// FindRouteByRequest finds route using the request // FindRouteByRequest finds route using the request
func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler { func (c *Core) FindRouteByRequest(r *http.Request) *node {
upperMethod := strings.ToUpper(r.Method) upperMethod := strings.ToUpper(r.Method)
mapper, ok := c.router[upperMethod] mapper, ok := c.router[upperMethod]
@ -75,13 +75,13 @@ func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
return nil return nil
} }
controllers := mapper.FindRoute(r.URL.Path) node := mapper.FindRoute(r.URL.Path)
if controllers == nil { if node == nil {
log.Printf("URI %q is not recognized\n", r.URL.Path) log.Printf("URI %q is not recognized\n", r.URL.Path)
return nil return nil
} }
return controllers return node
} }
func (c *Core) Group(prefix string) IGroup { func (c *Core) Group(prefix string) IGroup {
@ -97,13 +97,16 @@ func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := NewContext(w, r) ctx := NewContext(w, r)
handlers := c.FindRouteByRequest(r) node := c.FindRouteByRequest(r)
if handlers == nil { if node == nil {
ctx.WriteJSON(http.StatusNotFound, "Request not found") ctx.WriteJSON(http.StatusNotFound, "Request not found")
return return
} }
ctx.SetHandlers(handlers) params := node.ParseParamsFromEndNode(r.URL.Path)
ctx.SetParams(params)
ctx.SetHandlers(node.handlers)
if err := ctx.Next(); err != nil { if err := ctx.Next(); err != nil {
ctx.WriteJSON(http.StatusInternalServerError, "Internal error") ctx.WriteJSON(http.StatusInternalServerError, "Internal error")

View File

@ -14,11 +14,11 @@ func NewTrie() *Trie {
return &Trie{root: newNode("")} return &Trie{root: newNode("")}
} }
func (t *Trie) FindRoute(uri string) []ControllerHandler { func (t *Trie) FindRoute(uri string) *node {
uri = strings.ToUpper(uri) uri = strings.ToUpper(uri)
uri = strings.TrimPrefix(uri, "/") uri = strings.TrimPrefix(uri, "/")
if uri == "" { if uri == "" {
return t.root.handlers return t.root
} }
found := t.root.findRoute(uri) found := t.root.findRoute(uri)
@ -26,7 +26,7 @@ func (t *Trie) FindRoute(uri string) []ControllerHandler {
return nil return nil
} }
return found.handlers return found
} }
func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error { func (t *Trie) AddRouter(uri string, handlers []ControllerHandler) error {
@ -58,6 +58,29 @@ type node struct {
segment string segment string
handlers []ControllerHandler handlers []ControllerHandler
children []*node children []*node
parent *node
}
func (n *node) ParseParamsFromEndNode(uri string) map[string]string {
ret := map[string]string{}
uri = strings.ToUpper(uri)
uri = strings.TrimPrefix(uri, "/")
if uri == "" {
// root
return ret
}
segments := strings.Split(uri, "/")
cnt := len(segments)
cur := n
for i := cnt - 1; i >= 0; i-- {
if isWildcard(cur.segment) {
// set params
ret[cur.segment[1:]] = segments[i]
}
cur = cur.parent
}
return ret
} }
func newNode(segment string) *node { func newNode(segment string) *node {
@ -138,6 +161,7 @@ func (n *node) addRoute(uri string, handlers []ControllerHandler) error {
// create a new node // create a new node
new := newNode(splitted[0]) new := newNode(splitted[0])
new.parent = n
if isLast { if isLast {
// this is the end // this is the end
new.handlers = append(new.handlers, handlers...) new.handlers = append(new.handlers, handlers...)

View File

@ -58,6 +58,7 @@ func FooControllerHandler(ctx *framework.Context) error {
} }
func UserLoginController(ctx *framework.Context) error { func UserLoginController(ctx *framework.Context) error {
time.Sleep(10 * time.Second)
ctx.WriteJSON(http.StatusOK, "ok") ctx.WriteJSON(http.StatusOK, "ok")
return nil return nil
} }
@ -74,6 +75,7 @@ func SubjectUpdateController(ctx *framework.Context) error {
func SubjectGetController(ctx *framework.Context) error { func SubjectGetController(ctx *framework.Context) error {
ctx.WriteJSON(http.StatusAccepted, "got") ctx.WriteJSON(http.StatusAccepted, "got")
log.Println(ctx.ParamInt("ID", 0))
return nil return nil
} }

25
main.go
View File

@ -1,8 +1,14 @@
package main package main
import ( import (
"context"
"fmt"
"log" "log"
"net/http" "net/http"
"os"
"os/signal"
"syscall"
"time"
"git.vinchent.xyz/vinchent/go-web/framework" "git.vinchent.xyz/vinchent/go-web/framework"
) )
@ -15,7 +21,22 @@ func main() {
Handler: core, Handler: core,
} }
if err := server.ListenAndServe(); err != nil { go func() {
log.Panic(err) server.ListenAndServe()
}()
// create quit channel
quit := make(chan os.Signal, 1)
// set notifier
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-quit
fmt.Println("YOLO")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal("server shutdown: ", err)
} }
} }

View File

@ -15,6 +15,7 @@ func registerRouter(core *framework.Core) {
subjectApi.Delete("/:id", SubjectDelController) subjectApi.Delete("/:id", SubjectDelController)
subjectApi.Put("/:id", SubjectUpdateController) subjectApi.Put("/:id", SubjectUpdateController)
subjectApi.Get("/:id", SubjectGetController) subjectApi.Get("/:id", SubjectGetController)
subjectApi.Get("/:id/test", SubjectGetController)
subjectApi.Get("/list/all", SubjectListController) subjectApi.Get("/list/all", SubjectListController)
} }
} }