Skip to content

Commit

Permalink
Merge pull request #7 from kings1ay3r/route-definition-express-like
Browse files Browse the repository at this point in the history
refactor: changed app.Route to app.methodName
  • Loading branch information
bgcicca authored Oct 24, 2024
2 parents 4bac438 + 41ffa82 commit 18be2d4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
10 changes: 5 additions & 5 deletions examples/crud/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func main() {
app.Use(loggingMiddleware)

// Register routes
app.Route("GET", "/hello", HelloHandler)
app.Route("POST", "/users/create", CreateUser)
app.Route("GET", "/users/get", GetUser)
app.Route("PUT", "/users/update", UpdateUser)
app.Route("DELETE", "/users/delete", DeleteUser)
app.Get("/hello", HelloHandler)
app.Post("/users/create", CreateUser)
app.Get("/users/get", GetUser)
app.Put("/users/update", UpdateUser)
app.Delete("/users/delete", DeleteUser)

fmt.Println("Server listening on port 3333")
app.Listen(":3333")
Expand Down
2 changes: 1 addition & 1 deletion examples/http_header/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func main() {
app := router.NewApp()

// Define a route that responds to a GET request at "/auth".
app.Route("GET", "/auth", func(r *req.Request, w *req.Response) {
app.Get("/auth", func(r *req.Request, w *req.Response) {
authHeader := r.Header("Authorization")
if authHeader == "" {
w.Status(401).Send("Unauthorized")
Expand Down
2 changes: 1 addition & 1 deletion examples/json_data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
app := router.NewApp()

// Define a route that responds to a GET request at "/json".
app.Route("GET", "/json", func(r *req.Request, w *req.Response) {
app.Get("/json", func(r *req.Request, w *req.Response) {
data := map[string]string{
"message": "Hello, JSON",
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_route/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
app := router.NewApp()

// Define a route that responds to a GET request at "/hello".
app.Route("GET", "/hello", func(r *req.Request, w *req.Response) {
app.Get("/hello", func(r *req.Request, w *req.Response) {
w.Send("Hello, World!")
})

Expand Down
33 changes: 33 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ func (a *App) Route(method string, path string, handler func(req *req.Request, r
a.routes[path][method] = h
}

func (a *App) Get(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodGet, path, handler)
}

func (a *App) Post(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodPost, path, handler)
}

func (a *App) Put(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodPut, path, handler)
}

func (a *App) Delete(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodDelete, path, handler)
}

func (a *App) Patch(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodPatch, path, handler)
}
func (a *App) Options(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodOptions, path, handler)
}
func (a *App) Head(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodHead, path, handler)
}

func (a *App) Connect(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodConnect, path, handler)
}
func (a *App) Trace(path string, handler func(req *req.Request, res *req.Response)) {
a.Route(http.MethodTrace, path, handler)
}

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if handlers, exists := a.routes[r.URL.Path]; exists {
if handler, methodExists := handlers[r.Method]; methodExists {
Expand Down

0 comments on commit 18be2d4

Please sign in to comment.