// Copyright 2021 Harran Ali . All rights reserved. // Copyright (c) 2024 Zeni Kim // Use of this source code is governed by MIT-style // license that can be found in the LICENSE file. package core // Route defines an HTTP route with a method, path, controller function, and optional hooks for customization. type Route struct { Method string Path string Controller Controller Hooks []Hook } // Router represents a structure for storing and managing routes in the application. type Router struct { Routes []Route } // router is a global instance of the Router struct used to manage and resolve application routes. var router *Router // NewRouter initializes and returns a new Router instance with an empty slice of routes. func NewRouter() *Router { router = &Router{ []Route{}, } return router } // ResolveRouter returns the singleton instance of the Router. It initializes and manages HTTP routes configuration. func ResolveRouter() *Router { return router } // Get registers a GET route with the specified path, controller, and optional hooks, returning the updated Router. func (r *Router) Get(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: GET, Path: path, Controller: controller, Hooks: hooks, }) return r } // Post registers a route with the HTTP POST method, associating it with the given path, controller, and optional hooks. func (r *Router) Post(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: POST, Path: path, Controller: controller, Hooks: hooks, }) return r } // Delete registers a DELETE HTTP method route with the specified path, controller, and optional hooks. func (r *Router) Delete(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: DELETE, Path: path, Controller: controller, Hooks: hooks, }) return r } // Patch registers a new route with the HTTP PATCH method, a specified path, a controller, and optional hooks. func (r *Router) Patch(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: PATCH, Path: path, Controller: controller, Hooks: hooks, }) return r } // Put registers a new route with the HTTP PUT method, associating it to the given path, controller, and optional hooks. func (r *Router) Put(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: PUT, Path: path, Controller: controller, Hooks: hooks, }) return r } // Options registers a new route with the OPTIONS HTTP method, a specified path, controller, and optional hooks. func (r *Router) Options(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: OPTIONS, Path: path, Controller: controller, Hooks: hooks, }) return r } // Head registers a route with the HTTP HEAD method, associates it with a path, controller, and optional hooks. func (r *Router) Head(path string, controller Controller, hooks ...Hook) *Router { r.Routes = append(r.Routes, Route{ Method: HEAD, Path: path, Controller: controller, Hooks: hooks, }) return r } // GetRoutes returns a slice of Route objects currently registered within the Router. func (r *Router) GetRoutes() []Route { return r.Routes }