From fb9904cf03548875f2c7ac0fbb5cbc8ec5b85d6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20P=C3=A9rez?= Date: Tue, 12 Nov 2024 10:00:14 +0100 Subject: [PATCH] simpified ron frameworks and correct some fixes added example still learning how to works http package --- example/main.go | 26 +++++++++++++++++ go.mod | 2 +- main.go | 74 ------------------------------------------------- ron.go | 56 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 example/main.go delete mode 100644 main.go create mode 100644 ron.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..8bbb53a --- /dev/null +++ b/example/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "log/slog" + "net/http" + + "ron" +) + +func main() { + r := ron.New() + + r.GET("/", helloWorld) + r.POST("/another", anotherHelloWorld) + + slog.Info("Server is running at http://localhost:8080") + http.ListenAndServe(":8080", r) +} + +func helloWorld(c *ron.Context) { + c.W.Write([]byte("hello world")) +} + +func anotherHelloWorld(c *ron.Context) { + c.W.Write([]byte("another hello world")) +} diff --git a/go.mod b/go.mod index cba0205..2277ce4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module ron -go 1.22.1 +go 1.23.2 diff --git a/main.go b/main.go deleted file mode 100644 index 8f29fe2..0000000 --- a/main.go +++ /dev/null @@ -1,74 +0,0 @@ -package main - -import ( - "context" - "log/slog" - "net/http" -) - -// Context is the context of the current HTTP request. -type Context struct { - Ctx context.Context - W http.ResponseWriter - R *http.Request -} - -// HandlerFunc defines the handler used by the framework. -type HandlerFunc func(c *Context) - -// Engine is the framework's instance, it contains the router and middleware. -type Engine struct { - router *RouterGroup -} - -// RouterGroup is used internally to configure router, associated with a prefix and an array of handlers (middlewares). -type RouterGroup struct { - Handlers []HandlerFunc - engine *Engine -} - -// New creates a new Engine instance. -func New() *Engine { - engine := &Engine{} - engine.router = &RouterGroup{engine: engine} - return engine -} - -// ServeHTTP conforms to the http.Handler interface. -func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() - engine.router.handleHTTPRequest(ctx, w, r) -} - -// handleHTTPRequest handles the HTTP request. -func (group *RouterGroup) handleHTTPRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) { - c := &Context{Ctx: ctx, W: w, R: r} - for _, handler := range group.Handlers { - handler(c) - } -} - -// Use adds middleware to the router group. -func (group *RouterGroup) Use(middleware ...HandlerFunc) { - group.Handlers = append(group.Handlers, middleware...) -} - -// GET adds a GET route to the router group. -func (group *RouterGroup) GET(path string, handler HandlerFunc) { - // Implement route registration logic here - group.Handlers = append(group.Handlers, handler) -} - -func main() { - engine := New() - - engine.router.GET("/", sayHelloHandler) - - slog.Info("Server is running at http://localhost:8080") - http.ListenAndServe(":8080", engine) -} - -func sayHelloHandler(c *Context) { - slog.Info("called sayHelloHandler") - c.W.Write([]byte("Hello, World!")) -} diff --git a/ron.go b/ron.go new file mode 100644 index 0000000..df6909f --- /dev/null +++ b/ron.go @@ -0,0 +1,56 @@ +package ron + +import ( + "context" + "net/http" +) + +type Context struct { + C context.Context + W http.ResponseWriter + R *http.Request + E *Engine +} + +type Engine struct { + mux *http.ServeMux +} + +func New() *Engine { + engine := &Engine{ + mux: http.NewServeMux(), + } + return engine +} + +func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { + engine.handleRequest(w, r) +} + +func (engine *Engine) Run(addr string) error { + return http.ListenAndServe(addr, engine) +} + +func (engine *Engine) handleRequest(w http.ResponseWriter, r *http.Request) { + engine.mux.ServeHTTP(w, r) +} + +func (engine *Engine) GET(path string, handler func(*Context)) { + engine.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) + return + } + handler(&Context{W: w, R: r, E: engine}) + }) +} + +func (engine *Engine) POST(path string, handler func(*Context)) { + engine.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed) + return + } + handler(&Context{W: w, R: r, E: engine}) + }) +}