File-Based Routing (Next.js Like)
Gothic Framework follows a file-based routing system, similar to Next.js App Router. Folders and files in your project automatically define your routes for pages, components, and API endpoints.
When you create a file in the src/pages, src/components, or src/api folders, Gothic Framework will automatically generate the corresponding routes in src/routes/routes_gen.go, which exposes a RegisterFileBasedRoutes function wired up in your main.go. This file is regenerated every time a .go or .templ file changes.
Important: The routes_gen.go file is auto-generated. Do not modify it directly as your changes will be overwritten.
Here is an example of how the folder structure maps to routes:
src/
├── pages/
│ ├── index.templ → /
│ ├── about.templ → /about
│ └── docs/
│ └── guide.templ → /docs/guide
├── components/
│ └── navbar.templ → /components/navbar
└── api/
└── users.go → /api/usersEach page or component that exports a RouteConfig will have its route automatically registered. The route path is derived from the file location relative to the src folder.
To customize your route behavior, you can define a RouteConfig with the route type (STATIC, DYNAMIC, or ISR), HTTP method, and middleware function:
import routes "github.com/gothicframework/core/router"
type Props = interface{}
var PageConfig = routes.RouteConfig[Props]{
Type: routes.STATIC,
HttpMethod: routes.GET,
Middleware: func(w http.ResponseWriter, r *http.Request) Props {
return nil
},
}Want to create dynamic routes with path parameters? Click the button below to learn about path variables!
