Sprint is a modular, high-performance web framework for Go, inspired by the architecture of NestJS. Designed to streamline backend development, Sprint empowers developers to build scalable and maintainable applications with ease. It emphasizes a modular structure, where functionality is divided into controllers and modules, allowing for clear organization and flexibility in development.
Before you start using Sprint, ensure that you have Go installed on your machine. If not, you can follow Go's official documentation for installation guidance.
Once you have Go installed, you can install Sprint as a Go module using the following command:
go get github.com/zlorgoncho1/sprint/server
Start by setting up a new Go project:
mkdir app && cd app
go mod init app
Organize your project with the following structure:
📂 app
┣━━ 📂 hello
┃ ┣━━ 📄 controllers.go
┃ ┗━━ 📄 module.go
┣━━ 📄 go.mod
┣━━ 📄 go.sum
┣━━ 📄 main.go
main.go
Set up the main entry point of your application:
package main
import (
"app/hello"
"github.com/zlorgoncho1/sprint/server"
)
func main() {
server := &server.Server{Host: "localhost", Port: "8000"}
server.Start(hello.HelloModule())
}
module.go
Define your application module:
package hello
import (
"github.com/zlorgoncho1/sprint/core"
)
// Module Definition
func HelloModule() *core.Module {
return &core.Module{
Name: "HelloModule",
Controllers: []*core.Controller{
HelloController(),
},
}
}
controllers.go
Create a basic controller:
package hello
import (
"github.com/zlorgoncho1/sprint/core"
)
func HelloController() *core.Controller {
var HelloController = &core.Controller{Name: "HelloController", Path: "hello"}
HelloController.AddRoute(core.GET, ":name", hello)
HelloController.AddRoute(core.GET, "JSON/:name", helloJSON)
HelloController.AddRoute(core.GET, "HTML/:name", helloHTML)
return HelloController
}
func hello(request core.Request) core.Response {
return core.Response{Content: "Bonjour " + request.Params["name"]}
}
func helloHTML(request core.Request) core.Response {
name := request.Params["name"]
return core.Response{Content: "<h1>Bonjour " + name + "</h1>", ContentType: core.HTML}
}
func helloJSON(request core.Request) core.Response {
type greetingObj struct {
Nom string `json:"nom"`
Message string `json:"message"`
}
name := request.Params["name"]
return core.Response{Content: greetingObj{name, "Bonjour"}, ContentType: core.JSON}
}
Run your application:
go run main.go
Your Sprint application should now be running on localhost:8000
. Visiting localhost:8000/hello/HTML/Sprint
in your browser or through a tool like curl
should return a "Hello World" message.
core.go
)The core of our framework lies within core.go
, which defines the primary structs and interfaces used throughout the application. This includes definitions for:
utils.go
)This utility package (utils.go
) provides a set of handy functions for converting dictionaries to JSON strings, formatting HTTP responses, and generating standard headers. It abstracts some repetitive tasks, such as:
logger.go
)Logging is a critical aspect of any application. Our logger.go
file describes a custom logger capable of handling various logging levels (e.g., Info, Error) and providing a way to output logs with different severities. This is crucial for both debugging and runtime monitoring of the application.
server.go
)The server.go
file is the heart of the framework where the HTTP server is configured and launched. Key functionalities include:
server.go
) allows efficient resolution of endpoints. This is particularly advantageous for applications with a large number of routes./user/:id
), allowing for more flexible endpoint definitions.server.go
, includes comprehensive error handling, enhancing the robustness of the application.We welcome contributions to Sprint! If you'd like to get involved, here are some areas where you can make a difference:
For more details, please refer to our Contribution Guidelines or explore the Wiki.
Sprint is licensed under the MIT License.