Git Product home page Git Product logo

cenery's Introduction

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 [email protected] http://localhost:2000/upload

Buy Me a Coffee

"Buy Me A Coffee"

cenery's People

Contributors

dreamph avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.