cenery

package module
v0.0.0-...-2080e1e Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 0 Imported by: 0

README

cenery

Golang Web Framework

  • Dynamic change engine between Fiber and Echo
  • Simple & Easy

Install

go get github.com/dreamph/cenery

Examples

package main

import (
	"github.com/dreamph/cenery"
	echoengine "github.com/dreamph/cenery/engine/echo"
	fiberengine "github.com/dreamph/cenery/engine/fiber"
	gojson "github.com/goccy/go-json"
	"github.com/gofiber/fiber/v2"
	fiberrecover "github.com/gofiber/fiber/v2/middleware/recover"
	"github.com/labstack/echo/v4"
	echomiddleware "github.com/labstack/echo/v4/middleware"
	"log"
)

type ErrorResponse struct {
	Message string `json:"message"`
}

type CreateRequest struct {
	Name string `json:"name"`
}

type CreateResponse struct {
	Data string `json:"data"`
}

type UploadRequest struct {
	Name string           `json:"name"`
	File *cenery.FileData `json:"-"`
}

type UploadResponse struct {
	Name     string `json:"name"`
	FileName string `json:"fileName"`
}

func NewEcho() cenery.App {
	echoApp := echo.New()
	echoApp.Use(echomiddleware.Recover())
	return echoengine.New(echoApp)
}

func NewFiber() cenery.App {
	fiberApp := fiber.New(fiber.Config{
		JSONDecoder: gojson.Unmarshal,
		JSONEncoder: gojson.Marshal,
	})
	fiberApp.Use(fiberrecover.New())
	return fiberengine.New(fiberApp)
}

func main() {
	app := NewFiber() //NewEcho()

	app.Get("/", func(c cenery.Ctx) error {
		return c.SendString(200, "hello")
	})

	app.Post("/create", func(c cenery.Ctx) error {
		request := &CreateRequest{}
		err := c.BodyParser(request)
		if err != nil {
			return c.SendJSON(400, &ErrorResponse{Message: err.Error()})
		}
		return c.SendJSON(200, &CreateResponse{Data: "welcome : " + request.Name})
	})

	app.Post("/upload", func(c cenery.Ctx) error {
		request := &UploadRequest{}
		err := c.BodyParser(request)
		if err != nil {
			return c.SendJSON(400, &ErrorResponse{Message: err.Error()})
		}

		request.File = c.FormFile("file")

		return c.SendJSON(200, &UploadResponse{Name: request.Name, FileName: request.File.FileName})
	})

	err := app.Listen(":2000")
	if err != nil {
		log.Fatal(err.Error())
	}
}

//curl -X POST http://localhost:2000/create -d '{"name":"cenery"}'
//curl -v -F name=cenery -F file=@test.txt http://localhost:2000/upload

Buy Me a Coffee

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	Get(path string, handlers ...Handler)
	Post(path string, handlers ...Handler)
	Put(path string, handlers ...Handler)
	Delete(path string, handlers ...Handler)
	Head(path string, handlers ...Handler)
	Options(path string, handlers ...Handler)
	Connect(path string, handlers ...Handler)
	Patch(path string, handlers ...Handler)
	Trace(path string, handlers ...Handler)
	//Use(middlewares ...func(next Handler) Handler)
	Listen(addr string) error
}

type Ctx

type Ctx interface {
	Params(key string, defaultValue ...string) string
	QueryParam(key string, defaultValue ...string) string
	BodyParser(out interface{}) error
	FormFile(fileKey string) *FileData
	FormFiles(fileKey string) *[]FileData

	SendString(status int, data string) error
	Send(status int, data []byte) error
	SendJSON(status int, data interface{}) error

	Request() Request
	Response() Response
}

type FileData

type FileData struct {
	FileData        []byte `json:"fileData"`
	FileName        string `json:"fileName"`
	FileSize        int64  `json:"fileSize"`
	FileContentType string `json:"fileContentType"`
}

type Handler

type Handler = func(Ctx) error

type Request

type Request interface {
	Body() []byte
	SetBody(data []byte)

	GetHeader(key string) string
	SetHeader(key string, val string)
	AddHeader(key string, val string)
}

type Response

type Response interface {
	Body() []byte
	SetBody(data []byte)

	GetHeader(key string) string
	SetHeader(key string, val string)
	AddHeader(key string, val string)
}

Directories

Path Synopsis
engine

Jump to

Keyboard shortcuts

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