go-web/framework/core.go

113 lines
2.6 KiB
Go
Raw Normal View History

2024-09-05 15:30:59 +00:00
package framework
import (
"log"
2024-09-05 15:30:59 +00:00
"net/http"
2024-09-16 09:39:38 +00:00
"strings"
2024-09-05 15:30:59 +00:00
)
// Core is the core struct of the framework
type Core struct {
router map[string]*Trie
middlewares []ControllerHandler
}
2024-09-05 15:30:59 +00:00
// NewCore initialize the Core.
func NewCore() *Core {
2024-09-16 16:25:09 +00:00
getRouter := NewTrie()
postRouter := NewTrie()
putRouter := NewTrie()
deleteRouter := NewTrie()
2024-09-16 09:39:38 +00:00
2024-09-16 16:25:09 +00:00
router := map[string]*Trie{}
2024-09-16 09:39:38 +00:00
router["GET"] = getRouter
router["POST"] = postRouter
router["PUT"] = putRouter
router["DELETE"] = deleteRouter
return &Core{router: router}
}
// Get is a simple get router
func (c *Core) Get(url string, handlers ...ControllerHandler) {
allHandlers := append(c.middlewares, handlers...)
if err := c.router["GET"].AddRouter(url, allHandlers); err != nil {
2024-09-24 21:06:39 +00:00
log.Println(err)
}
2024-09-16 09:39:38 +00:00
}
// Post is a simple post router
func (c *Core) Post(url string, handlers ...ControllerHandler) {
allHandlers := append(c.middlewares, handlers...)
if err := c.router["POST"].AddRouter(url, allHandlers); err != nil {
2024-09-24 21:06:39 +00:00
log.Println(err)
}
2024-09-16 09:39:38 +00:00
}
// Put is a simple put router
func (c *Core) Put(url string, handlers ...ControllerHandler) {
allHandlers := append(c.middlewares, handlers...)
if err := c.router["PUT"].AddRouter(url, allHandlers); err != nil {
2024-09-24 21:06:39 +00:00
log.Println(err)
}
2024-09-16 09:39:38 +00:00
}
// Delete is a simple delete router
func (c *Core) Delete(url string, handlers ...ControllerHandler) {
allHandlers := append(c.middlewares, handlers...)
if err := c.router["DELETE"].AddRouter(url, allHandlers); err != nil {
2024-09-24 21:06:39 +00:00
log.Println(err)
}
2024-09-16 09:39:38 +00:00
}
// Use registers middlewares
func (c *Core) Use(middlewares ...ControllerHandler) {
c.middlewares = append(c.middlewares, middlewares...)
}
// FindRouteByRequest finds route using the request
2024-09-24 21:33:33 +00:00
func (c *Core) FindRouteByRequest(r *http.Request) []ControllerHandler {
2024-09-16 09:39:38 +00:00
upperMethod := strings.ToUpper(r.Method)
mapper, ok := c.router[upperMethod]
if !ok {
log.Printf("Method %q is not recognized\n", upperMethod)
return nil
}
controllers := mapper.FindRoute(r.URL.Path)
2024-09-24 21:33:33 +00:00
if controllers == nil {
2024-09-16 09:39:38 +00:00
log.Printf("URI %q is not recognized\n", r.URL.Path)
return nil
}
2024-09-24 21:33:33 +00:00
return controllers
2024-09-05 15:30:59 +00:00
}
func (c *Core) Group(prefix string) IGroup {
return &Group{
core: c,
prefix: prefix,
}
}
2024-09-05 15:30:59 +00:00
// ServeHTTP implements the Handler interface
func (c *Core) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println("Welcome to the Araneae framework")
ctx := NewContext(w, r)
2024-09-24 21:33:33 +00:00
handlers := c.FindRouteByRequest(r)
if handlers == nil {
2024-09-16 09:39:38 +00:00
ctx.WriteJSON(http.StatusNotFound, "Request not found")
return
}
2024-09-24 21:33:33 +00:00
ctx.SetHandlers(handlers)
if err := ctx.Next(); err != nil {
2024-09-16 09:39:38 +00:00
ctx.WriteJSON(http.StatusInternalServerError, "Internal error")
return
}
2024-09-05 15:30:59 +00:00
}