bagoette

package module
v0.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 10 Imported by: 0

README

[Development 🛠️] BAGOETTE: Best Go RestfulAPI Library for Beginner 🥖

Teto want to say hello

yo whats up gng ✌️

So currently i'am trying to construct a Golang RestfulAPI library like gin, fiber and echo 🏗️ but... its specialized to help beginner to create their own Golang backend service.

yeah, i know the codes and the project structures are still really messy because i'm new to this Programming language🥀

so yeah maybe you guys can give me some advice?

documentation

installation

just give it a try dawg 🥖

go get github.com/sevelfatt/bagoette
quick example
package main

import "github.com/sevelfatt/bagoette"

func main() {
	b := bagoette.NewClient() // create new client
	r := b.NewRouter() // create new router

	r.Get("/ping", func(c *bagoette.Ctx){
		c.Respond(200, "pong") // respond to client with status code 200 and message "pong"
	}) // open new route with GET method on /ping path

	b.Serve() // start the server
}
use middleware
package main

import (
	"github.com/sevelfatt/bagoette"
)

func main() {
	b := bagoette.NewClient()
	r := b.NewRouter()

	r.Get("/ping", loggerMiddleware, pong) // you can add more middleware like this

	b.Serve()
}

func loggerMiddleware(c *bagoette.Ctx) {
	bagoette.Logger.Println("Request: " + c.request.Method + " " + c.request.URL.Path)
	c.Next()
}

func pong(c *bagoette.Ctx) {
	c.Respond(200, "pong")
}
use router group
package main

import "github.com/sevelfatt/bagoette"

func main() {
	b := bagoette.NewClient()
	r := b.NewRouter()

	userRouter := r.Group("/users") //you can wrap some routes with same prefix like this
	userRouter.Get("/ping", pong) //so the path will be /users/ping

	b.Serve()
}

func pong(c *bagoette.Ctx) {
	c.Respond(200, "pong")
}
use middleware in router group
package main

import (
	"github.com/sevelfatt/bagoette"
)

func main() {
	b := bagoette.NewClient()
	r := b.NewRouter()

	userRouter := r.Group("/users") 
    userRouter.Use(loggerMiddleware) // you can add more middleware like this

	userRouter.Get("/ping", pong)

	b.Serve()
}

func loggerMiddleware(c *bagoette.Ctx) {
	bagoette.Logger.Println("Request: " + c.request.Method + " " + c.request.URL.Path)
	c.Next()
}

func pong(c *bagoette.Ctx) {
	c.Respond(200, "pong")
}

Documentation

Index

Constants

View Source
const (
	Reset  = "\033[0m"
	Red    = "\033[31m"
	Green  = "\033[32m"
	Yellow = "\033[33m"
	Blue   = "\033[34m"
	White  = "\033[37m"
)

Variables

View Source
var Logger = log.New(os.Stdout, Red+"[BAGOETTE] "+Reset, log.LstdFlags)
View Source
var RequestStatusColours = map[int]string{
	1: Blue,
	2: Green,
	3: Blue,
	4: Yellow,
	5: Red,
}

Functions

func Log

func Log(message string)

Types

type BagoetteClient

type BagoetteClient struct {
	Opts *Options

	Routes *[]Route
	// contains filtered or unexported fields
}

Bagoette main struct: work as the core of the library and provide all the features like router, middleware, context, etc

func NewClient

func NewClient() *BagoetteClient

NewClient: create a new BagoetteClient

func (*BagoetteClient) Close

func (b *BagoetteClient) Close() error

func (*BagoetteClient) GetHTTPHandler

func (b *BagoetteClient) GetHTTPHandler() *http.ServeMux

func (*BagoetteClient) GetRoutes

func (b *BagoetteClient) GetRoutes() []Route

func (*BagoetteClient) MaxUploadSize added in v0.4.0

func (b *BagoetteClient) MaxUploadSize(size int64) (*BagoetteClient, error)

MaxUploadSize: set the max upload size of the server

func (*BagoetteClient) NewRouter

func (b *BagoetteClient) NewRouter() *RouteGroup

func (*BagoetteClient) Port

func (b *BagoetteClient) Port(port int) *BagoetteClient

Port: set the port of the server

func (*BagoetteClient) Serve

func (b *BagoetteClient) Serve() error

func (*BagoetteClient) ServeAppearance

func (b *BagoetteClient) ServeAppearance()

func (*BagoetteClient) SetAllowCredentials added in v0.5.2

func (b *BagoetteClient) SetAllowCredentials(allowCredentials bool) *BagoetteClient

func (*BagoetteClient) SetAllowedHeaders added in v0.5.2

func (b *BagoetteClient) SetAllowedHeaders(headers []string) *BagoetteClient

func (*BagoetteClient) SetAllowedMethods added in v0.5.2

func (b *BagoetteClient) SetAllowedMethods(methods []string) *BagoetteClient

func (*BagoetteClient) SetAllowedOrigins added in v0.5.2

func (b *BagoetteClient) SetAllowedOrigins(origins []string) *BagoetteClient

func (*BagoetteClient) SetExposedHeaders added in v0.5.2

func (b *BagoetteClient) SetExposedHeaders(headers []string) *BagoetteClient

func (*BagoetteClient) SetMaxAge added in v0.5.2

func (b *BagoetteClient) SetMaxAge(maxAge int) *BagoetteClient

func (*BagoetteClient) SetOptions added in v0.5.1

func (b *BagoetteClient) SetOptions(opts Options) *BagoetteClient

func (*BagoetteClient) ShowRoutes

func (b *BagoetteClient) ShowRoutes()

type Cors added in v0.5.0

type Cors struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposedHeaders   []string
	MaxAge           int
	AllowCredentials bool
}

func NewCors added in v0.5.0

func NewCors() *Cors

type Ctx

type Ctx struct {
	Writer  http.ResponseWriter
	Request *http.Request

	Data map[string]any
	// contains filtered or unexported fields
}

Context struct: work as the container of the request and response

func NewContext

func NewContext(opts *Options, writer http.ResponseWriter, request *http.Request, route *Route) *Ctx

func (*Ctx) Abort

func (c *Ctx) Abort() error

Abort: abort the request

func (*Ctx) Bind

func (c *Ctx) Bind(body any) error

Bind: bind the request body to a struct

func (*Ctx) Check

func (c *Ctx) Check() error

func (*Ctx) DownloadFile added in v0.4.2

func (c *Ctx) DownloadFile(path string) error

DownloadFile: Download a file

func (*Ctx) Error

func (c *Ctx) Error(status int, message string) error

Error: respond with an error

func (*Ctx) Get

func (c *Ctx) Get(key string) (any, error)

Get: get a value from the context data

func (*Ctx) GetAndSaveFiles added in v0.4.2

func (c *Ctx) GetAndSaveFiles(key string, path string) error

GetAndSaveFiles: get files from the request and save them by calling Getfiles and then save them directly using SaveFile example: c.GetAndSaveFiles("myFiles", "/path/to/save") will get files with key "myFiles" and save them to "/path/to/save"

func (*Ctx) GetFiles added in v0.4.2

func (c *Ctx) GetFiles(key string) ([]*multipart.FileHeader, error)

GetFiles: get files from the request example: c.GetFiles("myFiles") will return files with key "myFiles"

func (*Ctx) Header

func (c *Ctx) Header(key string) (string, error)

Header: get a header example: c.Header("Content-Type") will return "application/json"

func (*Ctx) Next

func (c *Ctx) Next() error

Next: call the next handler and reset the context after the last handler is called

func (*Ctx) Param

func (c *Ctx) Param(key string) (string, error)

Param: get a path parameter example: /users/:id c.Param("id") will return the id of the user

func (*Ctx) Query

func (c *Ctx) Query(key string) (string, error)

Query: get a query parameter by the key of query example: /users?name=john&age=20 c.Query("name") will return "john" c.Query("age") will return "20"

func (*Ctx) Reset

func (c *Ctx) Reset() error

Reset: reset the context

func (*Ctx) Respond added in v0.4.2

func (c *Ctx) Respond(status int, data any) error

Responsd: respond with a JSON

func (*Ctx) RespondFile added in v0.4.2

func (c *Ctx) RespondFile(path string) error

RespondFile: Respond with a file

func (*Ctx) SaveFile added in v0.4.1

func (c *Ctx) SaveFile(file *multipart.FileHeader, path string) error

SaveFile: save a file example: c.SaveFile(file, "/path/to/save") will save the file to the given path

func (*Ctx) Set

func (c *Ctx) Set(key string, value any) error

Set: set a value in the context data

func (*Ctx) SetHeader

func (c *Ctx) SetHeader(key string, value string) error

SetHeader: set a header example: c.SetHeader("Content-Type", "application/json")

type HandlerFunc

type HandlerFunc func(c *Ctx)

HandlerFunc type: Define the handler function that use bagoette context

type Options added in v0.4.0

type Options struct {
	Port          int
	MaxUploadSize int64
	Cors          *Cors
}

func NewOptions added in v0.4.0

func NewOptions() *Options

type Route

type Route struct {
	// contains filtered or unexported fields
}

Route struct: Define individual route

func (*Route) Check added in v0.4.3

func (r *Route) Check() error

type RouteGroup added in v0.4.0

type RouteGroup struct {
	Middlewares []HandlerFunc
	Prefix      string
	// contains filtered or unexported fields
}

Router struct: work as the router of the server

func (*RouteGroup) AddNewRouteToBagoetteClient added in v0.4.0

func (r *RouteGroup) AddNewRouteToBagoetteClient(route *Route) error

func (*RouteGroup) Connect added in v0.4.0

func (r *RouteGroup) Connect(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Delete added in v0.4.0

func (r *RouteGroup) Delete(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Get added in v0.4.0

func (r *RouteGroup) Get(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Group added in v0.4.0

func (r *RouteGroup) Group(prefix string) *RouteGroup

func (*RouteGroup) Head added in v0.4.0

func (r *RouteGroup) Head(path string, handlers ...HandlerFunc) error

func (*RouteGroup) NewRoute added in v0.4.0

func (r *RouteGroup) NewRoute(method string, path string, handlers []HandlerFunc) error

func (*RouteGroup) Options added in v0.4.0

func (r *RouteGroup) Options(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Patch added in v0.4.0

func (r *RouteGroup) Patch(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Post added in v0.4.0

func (r *RouteGroup) Post(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Put added in v0.4.0

func (r *RouteGroup) Put(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Trace added in v0.4.0

func (r *RouteGroup) Trace(path string, handlers ...HandlerFunc) error

func (*RouteGroup) Use added in v0.4.0

func (r *RouteGroup) Use(handlers ...HandlerFunc) *RouteGroup

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL