Rocinante is a gin inspired web framework built on top of net/http.

Rocinante

Rocinante is a gin inspired web framework built on top of net/http.

⚙️ Installation

$ go get -u github.com/fskanokano/rocinante-go

⚡️ Quickstart

package main

import (
	"log"

	"github.com/fskanokano/rocinante-go"
)

func main() {
	r := rocinante.Default()
	r.GET("/", func(c *rocinante.Context) {
		c.String("hello world")
	})
	err := r.Run()
	if err != nil {
		log.Fatal(err)
	}
}

👀 Examples

Some common examples are listed below.

📖 Param

func main() {
	r := rocinante.Default()
	r.GET("/param1/:name/:id", func(c *rocinante.Context) {
		name := c.Param("name")
		id := c.Param("id")
		c.JSON(rocinante.Map{
			"name": name,
			"id":   id,
		})
	})
	r.GET("/param2/file/*file_path", func(c *rocinante.Context) {
		filePath := c.Param("file_path")
		c.JSON(rocinante.Map{
			"file_path": filePath,
		}, http.StatusOK)
	})
}
$ curl http://127.0.0.1:8000/param1/kano/1
# {"name":"kano","id":"1"}

$ curl http://127.0.0.1:8000/param2/file/one/two/three.xxx
# {"file_path":“/one/two/three.xxx”}

📖 Serving Static File

func main() {
	r := rocinante.Default()
	r.Static("/image", "image")
}

📖 Middleware

func main() {
	r := rocinante.Default()
	//global middleware
	r.Use(func(c *rocinante.Context) {
		fmt.Println("before")
		c.Next()
		fmt.Println("after")
	})
	//specific middleware on handler
	r.GET("/foo/:foo_id",
		func(c *rocinante.Context) {
			fooID := c.Param("foo_id")
			if fooID != "1" {
				c.AbortWithJSON(rocinante.Map{
					"error": "foo_id is not 1",
				}, http.StatusBadRequest)
				return
			}
			c.Next()
		},
		func(c *rocinante.Context) {
			c.String("foo")
		})
}

📖 Model binding and validation

Rocinante uses go-playground/validator/v10 for validation. Check the full docs on tags usage here.

type Login struct {
	Username string `json:"username"  validate:"required"`
	Password string `json:"password" validate:"required"`
}

func main() {
	r := rocinante.Default()
	//bind json
	r.POST("/json_login", func(c *rocinante.Context) {
		var loginJSON Login
		if err := c.BindJSON(&loginJSON); err != nil {
			c.JSON(rocinante.Map{
				"error": err.Error(),
			}, http.StatusBadRequest)
			return
		}
		c.String("login success")
	})
	//bind query
	r.POST("/query_login", func(c *rocinante.Context) {
		var loginQuery Login
		if err := c.BindQuery(&loginQuery); err != nil {
			c.JSON(rocinante.Map{
				"error": err.Error(),
			}, http.StatusBadRequest)
			return
		}
		c.String("login success")
	})
	//bind form
	r.POST("/form_login", func(c *rocinante.Context) {
		var loginForm Login
		if err := c.BindForm(&loginForm); err != nil {
			c.JSON(rocinante.Map{
				"error": err.Error(),
			}, http.StatusBadRequest)
			return
		}
		c.String("login success")
	})
}

📖 WebSocket

func main() {
	r := rocinante.Default()
	r.WebSocket("/test",
		func(conn *websocket.Conn) {
			for {
				mt, data, err := conn.ReadMessage()
				if err != nil {
					break
				}
				fmt.Println("received message: " + string(data))
				err = conn.WriteMessage(mt, data)
				if err != nil {
					break
				}
			}
		},
		//use specific middleware on websocket handler
		func(c *rocinante.Context) {
			fmt.Println("before")
			c.Next()
			fmt.Println("after")
		})
}

📖 MVC

type TestController struct {
	*rocinante.Controller
}

func (t *TestController) GET(c *rocinante.Context) {
	c.String("mvc get")
}

func (t *TestController) POST(c *rocinante.Context) {
	c.String("mvc post")
}

func main() {
	r := rocinante.Default()
	r.Route("/mvc", &TestController{})
}

📖 Group

The use of group and router is the same, group can generate unlimited subgroups, the same level of group does not affect each other, the subgroup will inherit some properties of the parent group (url prefix, middleware...).

func main() {
	r := rocinante.Default()
	{
		v1 := r.Group("/v1")
		v1.Use(func(c *rocinante.Context) {
			fmt.Println("v1 middleware")
			c.Next()
		})
		v1.GET("/index", func(c *rocinante.Context) {
			c.String("v1 index")
		})
		//curl http://127.0.0.1:8000/v1/index

		v2 := v1.Group("/v2")
		v2.Use(func(c *rocinante.Context) {
			fmt.Println("v2 middleware")
		})
		v2.GET("/index", func(c *rocinante.Context) {
			c.String("v2 index")
		})
		//curl http://127.0.0.1:8000/v1/v2/index
	}
}

📖 CORS

func main() {
	r := rocinante.Default()
	r.Use(cors.New(cors.Option{
		AllowOrigins:     []string{"www.example.com"},
		AllowMethods:     []string{"GET", "POST", "DELETE", "PUT"},
		AllowHeaders:     []string{"Custom-Header"},
		AllowCredentials: true,
		ExposeHeaders:    []string{"Exposed-Header"},
		MaxAge:           3600,
	}))
}
Similar Resources

Simple and lightweight Go web framework inspired by koa

Simple and lightweight Go web framework inspired by koa

VOX A golang web framework for humans, inspired by Koa heavily. Getting started Installation Using the go get power: $ go get -u github.com/aisk/vox B

Dec 14, 2022

Gouweb: A web framework for go inspired by laravel

gouweb is a web framework for go inspired by laravel Installation go get -u gith

Feb 17, 2022

Go-igni: monolith-based web-framework in Go Inspired by classical PHP Frameworks like CodeIgnier & Laravel

go-igni Web Framework in Go About This is research project about developing monolith-based web-framework in Go Inspired by classical PHP Frameworks li

Feb 25, 2022

Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.

Flamingo Framework and Core Library. Flamingo is a go based framework for pluggable web projects. It is used to build scalable and maintainable (web)applications.

Flamingo Framework Flamingo is a web framework based on Go. It is designed to build pluggable and maintainable web projects. It is production ready, f

Jan 5, 2023

Golanger Web Framework is a lightweight framework for writing web applications in Go.

/* Copyright 2013 Golanger.com. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except

Nov 14, 2022

A gin-like simple golang web framework.

webgo A gin-like simple golang web framework.

Aug 24, 2022

A gin-like simple golang web framework.

A gin-like simple golang web framework.

Aug 24, 2022

Percobaan membuat API dengan Golang menggunakan web framework Gin dan Swagger docs.

Percobaan membuat API dengan Golang menggunakan web framework Gin dan Swagger docs.

Percobaan Gin Framework Percobaan membuat API dengan bahasa Go-lang. Tech Stack Gin - Web framework Gin Swaggo - Swagger Docs integration for Gin web

Feb 11, 2022

A minimal framework to build web apps; with handler chaining, middleware support; and most of all standard library compliant HTTP handlers(i.e. http.HandlerFunc).

A minimal framework to build web apps; with handler chaining, middleware support; and most of all standard library compliant HTTP handlers(i.e. http.HandlerFunc).

WebGo v4.1.3 WebGo is a minimalistic framework for Go to build web applications (server side) with zero 3rd party dependencies. Unlike full-fledged fr

Jan 1, 2023
Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Swagger + Gin = SwaGin, a web framework based on Gin and Swagger

Swagger + Gin = SwaGin Introduction SwaGin is a web framework based on Gin and Swagger, which wraps Gin and provides built-in swagger api docs and req

Dec 30, 2022
Swagger + Gin = SwaGin, a web framework based on Gin and Swagger
Swagger + Gin = SwaGin, a web framework based on Gin and Swagger

Swagger + Gin = SwaGin Introduction SwaGin is a web framework based on Gin and Swagger, which wraps Gin and provides built-in swagger api docs and req

Dec 30, 2022
Lightweight web framework based on net/http.

Goweb Light weight web framework based on net/http. Includes routing middleware logging easy CORS (experimental) Goweb aims to rely only on the standa

Dec 21, 2022
⚡ Rux is an simple and fast web framework. support middleware, compatible http.Handler interface. 简单且快速的 Go web 框架,支持中间件,兼容 http.Handler 接口

Rux Simple and fast web framework for build golang HTTP applications. NOTICE: v1.3.x is not fully compatible with v1.2.x version Fast route match, sup

Dec 8, 2022
Gin is a HTTP web framework written in Go (Golang).
Gin is a HTTP web framework written in Go (Golang).

Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.

Jan 3, 2023
A web app built using Go Buffalo web framework

Welcome to Buffalo Thank you for choosing Buffalo for your web development needs. Database Setup It looks like you chose to set up your application us

Feb 7, 2022
a golang web mvc framework, like asp.net mvc.

goku goku is a Web Mvc Framework for golang, mostly like ASP.NET MVC. doc & api Installation To install goku, simply run go get github.com/QLeelulu/go

Dec 7, 2022
⚡️ Express inspired web framework written in Go
⚡️ Express inspired web framework written in Go

Fiber is an Express inspired web framework built on top of Fasthttp, the fastest HTTP engine for Go. Designed to ease things up for fast development w

Jan 2, 2023
Mango is a modular web-application framework for Go, inspired by Rack, and PEP333.

Mango Mango is a modular web-application framework for Go, inspired by Rack and PEP333. Note: Not actively maintained. Overview Mango is most of all a

Nov 17, 2022
A Go framework for building JSON web services inspired by Dropwizard

Tiger Tonic A Go framework for building JSON web services inspired by Dropwizard. If HTML is your game, this will hurt a little. Like the Go language

Dec 9, 2022