simpified ron frameworks and correct some fixes
added example still learning how to works http package
This commit is contained in:
parent
b8a0e3cde4
commit
fb9904cf03
26
example/main.go
Normal file
26
example/main.go
Normal file
@ -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"))
|
||||
}
|
||||
74
main.go
74
main.go
@ -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!"))
|
||||
}
|
||||
56
ron.go
Normal file
56
ron.go
Normal file
@ -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})
|
||||
})
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user