Nothing Special   »   [go: up one dir, main page]

Skip to content

Light Web Framework, written in Golang Native HTTP.

License

Notifications You must be signed in to change notification settings

wibu-gaptek/go-oppai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Oppai

Lightweight Web Framework, written in Golang Native HTTP.

OPPAI

Install

go get -u github.com/wibu-gaptek/go-oppai/oppai

Get Started

Start by importing go-oppai to your project

import (
	"net/http"
	"github.com/wibu-gaptek/go-oppai/oppai"
)

Server example running on port 3000

// Construct oppai first
oppaiServer := oppai.New()

// Add route to your root path for GET request and return with HTTP status 200 OK
oppaiServer.GET("/", func(ctx *oppai.Context) {
	ctx.Status(http.StatusOk)
})

// Start your server and listen to 0.0.0.0:3000, you could change this with your desired address
oppaiServer.Run(":3000")

// or if you want listen to 192.168.0.1:8080
r.Run("192.168.0.1:8080")

Docs

Oppai Constructor

func New() *Engine

Construct Oppai engine, it automatically initiate router engine.

Oppai Run

func (e *Engine) Run(addr string) error

Run listens on the TCP network address addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

If addr is blank, ":http" (:80) is used.

Oppai Router

// GET Method
func (e *RouterGroup) GET(pattern string, handler HandlerFunc)

// POST Method
func (e *RouterGroup) POST(pattern string, handler HandlerFunc)

// PUT Method
func (e *RouterGroup) PUT(pattern string, handler HandlerFunc)

// DELETE Method
func (e *RouterGroup) DELETE(pattern string, handler HandlerFunc)

// OPTION Method
func (e *RouterGroup) OPTION(pattern string, handler HandlerFunc)

// by default, go-oppai had built with GET, POST, PUT, DELETE, OPTION
// However, you could add more route with another defined method
func (e *RouterGroup) AddRoutes(method string, comp string, handler HandlerFunc)

Serve Oppai with specific Method on certain route defined in pattern.

Oppai Route group

func (e *RouterGroup) Group(prefix string) *RouterGroup

Serve Oppai Router

net/http

For more information, please read on https://pkg.go.dev/net/http#pkg-constants

Examples

Add parameter to router

// Construct oppai first
oppaiServer := oppai.New()

// Catch any request under /echo like /echo/hello or /echo/world et cetera
oppaiServer.GET("/echo/:name", func(ctx *oppai.Context) {
	name := ctx.Param("name")
	ctx.JSON(http.StatusOK, oppai.H{
		"name": name,
	})
})

// Listen to  port 3000
oppaiServer.Run(":3000")

Router Group

With router group, you could subroute like normal

// Construct oppai first
oppaiServer := oppai.New()

group := oppaiServer.Group("/echo")
{
	group.GET("/", func(ctx *oppai.Context) {
		ctx.JSON(http.StatusOK, oppai.H{
			"message": "Subroute with Router Group",
		})
	})
}

// Listen to  port 3000
oppaiServer.Run(":3000")