zlorgoncho1 / sprint

A lightweight, high-performance Go web framework focusing on simplicity, with built-in support for JSON & HTML responses. Perfect for building efficient, scalable server-side applications. Currently in active development. Contributions welcome!
MIT License
13 stars 5 forks source link
framework go golang golang-library hacktoberfest webframework

Sprint Web Framework

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.

Table of Contents

Getting Started

Installation

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

Creating Your First Sprint Application

Step 1: Creating the Project

Start by setting up a new Go project:

mkdir app && cd app
go mod init app

Step 2: Project Structure

Organize your project with the following structure:

📂 app
┣━━ 📂 hello
┃   ┣━━ 📄 controllers.go
┃   ┗━━ 📄 module.go
┣━━ 📄 go.mod
┣━━ 📄 go.sum
┣━━ 📄 main.go

Step 3: Writing Code

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}
}

Step 4: Running Your Application

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.

Key Features

Main Components

Core (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 (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 (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 (server.go)

The server.go file is the heart of the framework where the HTTP server is configured and launched. Key functionalities include:

Design Decisions and Highlights

Contributing

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.

License

Sprint is licensed under the MIT License.