Skip to content

DebAxom/Zapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zapi

Zapi is a lightweight backend web framework built on top of Go’s native net/http package. I created it as a personal passion project to have a simple, minimal, and expressive Go framework for my future work. While I designed the overall structure and features, I collaborated with ChatGPT to fine-tune certain parts especially the router, since I prefer to avoid the pain of working directly with regular expressions. Thanks to this, Zapi’s router supports clean, expressive, and fully parameterized routes right out of the box.

Docs

Initializing a new App

package main

import (
    "fmt"
    "github.com/DebAxom/Zapi"
)

func main() {
    App := zapi.New() // Initializing a new app

    App.Get("/", func(req *zapi.Req, res zapi.Res) {
		res.WriteHeader(200)
		res.Write("Hello World !")
	})

    fmt.Println("Server running on http://localhost:3000")
    App.run(":3000") // Running the server on port 3000
}

Methods

App.Get(route, handlerFunc) // Handle GET Request
App.Post(route, handlerFunc) // Handle POST Request
App.Put(route, handlerFunc) // Handle PUT Request
App.Delete(route, handlerFunc) // Handle DELETE Request

Routing

    App := zapi.New()
    App.Get("/", func(req *zapi.Req, res zapi.Res){ ... })
    App.Get("/blog", func(req *zapi.Req, res zapi.Res){ ... })
    
    // Parameterized Routes
    App.Get("/blog/[id]", func(req *zapi.Req, res zapi.Res){ 
        id := req.Params["id"]
        // Do something ...
    })

    App.Get("/@[username]/p-[id]", func(req *zapi.Req, res zapi.Res){ 
        username := req.Params["username"]
        id := req.Params["id"]
        // Do something ...
    })

    // Handling 404 ; This route should be at the end of all routes
    App.Get("*", func(req *zapi.Req, res zapi.Res){
        res.WriteHeader(404)
        res.Write("404 Not Found !")
    })

Req struct

Built on top of http.Request. Contains additional map Params and an additional function BindJSON.

Res struct

It contains all the methods of http.ResponseWriter along with a few additional features.

CORS

App := zapi.New()
APP.CORS.AllowedMethods = []string{"GET", "POST"}
APP.CORS.AllowedOrigins = []string{"http://localhost:8080"}
APP.CORS.AllowCredentials = true

Handling Cookies

// Get cookie using the GetCookie method on Req struct
c, err := req.GetCookie(cookieName)

// Delete a cookie using the DeleteCookie method on Res struct
res.DeleteCookie(cookieName)

// Set cookie using the SetCookie method on Res struct with default options
res.SetCookie(cookieName, cookieValue, nil)

// Set cookie using the SetCookie method on Res struct with your own options
res.SetCookie(cookieName, cookieValue, &zapi.CookieOptions{...})

// All options available
type CookieOptions struct {
    Path     string
	Domain   string
	Expires  time.Time
	MaxAge   int
	Secure   bool
	HttpOnly bool
	SameSite http.SameSite
}

Serving Static Files

App.Public(route, dirPath)

/*Example :
    App.Public("/assets", path.Join(os.Getwd(), "assets"))
*/

Sending File as response

App.Get("/", func(req *zapi.Req, res zapi.Res) {
	res.WriteHeader(200)
	res.SendFile(filepath)
})

Sending JSON as response

// Sending a map as response

App.Get("/", func(req *zapi.Req, res zapi.Res) {
	res.WriteHeader(200)
	res.JSON(map[string]string{"msg" : "Hello World"})
})

// Sending a struct as respone

type User struct {
	Name    string   `json:"name"`
	Age     int      `json:"age"`
	Hobbies []string `json:"hobbies"`
}

App.Get("/user", func(req *zapi.Req, res zapi.Res) {
	res.WriteHeader(200)
	res.JSON(User{
		Name:    "DebAxom",
		Age:     19,
		Hobbies: []string{"Coding", "Watching Youtube"},
	})
})

Getting JSON data from Body

type Auth struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

App.Get("/auth", func(req *zapi.Req, res zapi.Res) {
	
    var data Auth
    err := req.BindJSON(&data)

    if err!=nil{
        // Handle Error
    }

    username := Auth.Username
    pwd := Auth.Password
	
    // Do whatever you want with this data
})

Redirecting user to a different route

App.Get("/index", func(req *zapi.Req, res zapi.Res) {
	res.Redirect("/")
})

About

Zapi is a backend web-framework writen in Go.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages