lymon

package module
v0.0.0-...-fa12827 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 15 Imported by: 0

README

Lymon

Go Report Card

Advanced Web API Framework for Go, Project Status : WIP

go get github.com/GoWebFramework/lymon

Feature

  • Include MongoDB & Redis
  • JSON & Form payload validation
  • BeforeAll middleware
  • StatusCodeHandler middleware
  • cmd feature
  • CRUD Generator
  • Built-in cache wrapper
  • Simplify Wrapper
  • Sample

Usage

package main

import (
	"context"
	"log"
	"time"

	"./lymon"
)

func users(ctx lymon.Context, g *lymon.Global) {
	var result lymon.M

	to, _ := context.WithTimeout(context.Background(), 5*time.Second)
	g.DB().C("users").FindOne(to, lymon.M{}).Decode(&result)

	ctx.JSON(result)
}

func index(ctx lymon.Context, g *lymon.Global) {
	if ctx.V.IsValidated {
		ctx.JSON(map[string]string{
			"message": "valid payload!",
		})
	} else {
		ctx.W.Write([]byte(`{"message": "invalid payload !"}`))
	}
}

func main() {
	web := lymon.Global{}
	// web.UseDefaultConfig()
	web.UseConfig(lymon.Config{
		MongoURI: "mongodb://127.0.0.1:27017",
		RedisURI: "redis://127.0.0.1:6379/0",
		Listen:   "127.0.0.1:8080",
	})

	// set default mongo database
	web.Database = "my_site"

	web.HandleFunc("/users", "GET", users, nil)

	// Look at https://github.com/asaskevich/govalidator#validatestruct-2
	// for validation reference
	web.HandleFunc("/", "POST", index, map[string]interface{}{
		// curl request for test
		// invalid payload :  curl -d '{"name": "val2Conta1nNumb3r"}' http://localhost:8080
		// valid payload :  curl -d '{"name": "alphaOnly"}' http://localhost:8080
		"name": "required,alpha",
	})

	// before all middleware support
	web.BeforeAll(func(ctx lymon.Context, g *lymon.Global) {
		ctx.W.Header().Add("Access-Control-Allow-Origin", "*")
		ctx.W.Write([]byte("before all modifier \n"))
		return
	})

	// handle specific custom code, currently only applied for 404
	web.HandleStatusCode(404, func(ctx lymon.Context, g *lymon.Global) {
		ctx.W.Write([]byte("this is custom 404 page"))
	})

	log.Println("starting server...")
	web.Start()
}

Credits

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormToMAP

func FormToMAP(r *http.Request, maxMemory int64) map[string]interface{}

FormToMAP convert r.Form into map[string]val

Types

type Config

type Config struct {
	MongoURI string
	RedisURI string

	Listen             string
	MaxMultipartMemory int64
}

Config used when UseConfig function called

type Context

type Context struct {
	W http.ResponseWriter
	R *http.Request
	V additional
}

Context will hold single lymon route state

func (Context) JSON

func (c Context) JSON(source interface{})

JSON simplify json.Marshal

type D

type D = bson.D

type Database

type Database struct {
	Database   *mongo.Database
	Collection *mongo.Collection
}

Database export

func (Database) C

func (db Database) C(collection string) *mongo.Collection

C mongo collection

type Global

type Global struct {
	Config Config

	Database string
	Mongo    *mongo.Client
	Redis    *redis.Client

	// MiddlewareHandler store middleware handler with ordered-array
	MiddlewareHandler []handler

	// Path store main route handler based on route+method that used as key
	Path map[string]route

	// StatusCodeHandler store handler based on http status code
	StatusCodeHandler map[int]handler
}

Global hold Configuration, Database, Path and Handler data

func (*Global) BeforeAll

func (g *Global) BeforeAll(handler handler)

BeforeAll Before filters are evaluated before each request within the same context as the routes. They can modify the request and response.

func (Global) DB

func (c Global) DB(dbname ...string) Database

DB mongo database

func (*Global) HandleFunc

func (g *Global) HandleFunc(pattern, method string, handler handler, validator ...map[string]interface{})

HandleFunc registers the handler function for the given pattern.

func (*Global) HandleStatusCode

func (g *Global) HandleStatusCode(StatusCode int, handler handler)

HandleStatusCode with this middleware, You can customize the built-in error response, currently only works for 404

func (*Global) ServeHTTP

func (g *Global) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP called from net/http that will calls f(w, r).

func (*Global) Start

func (g *Global) Start()

Start invoke http.ListenAndServe

func (*Global) UseConfig

func (g *Global) UseConfig(conf Config)

UseConfig use config from user

func (*Global) UseDefaultConfig

func (g *Global) UseDefaultConfig()

UseDefaultConfig no comment

type M

type M = bson.M

M called as lymon.M aka bson.M wrapper

Jump to

Keyboard shortcuts

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