PRs Welcome

Go-Mojito is a super-modular library to bootstrap your next golang web project. It can be used for strict API-only purposes as well as server-side rendering.

Features

Dependency InjectionRoutingTemplatingLoggingMiddlewareCachingREST Router (beta)

Documentation

Read our GitHub Wiki, check out the Example Project or try run the code below

Icon made with Gopherize and flaticon.

package main

import (
	"github.com/infinytum/go-mojito"
)

var (
	address  = "0.0.0.0:8123"
	cacheKey = "greeting"

	Logger mojito.Logger
	Router mojito.Router
)

func main() {
	Logger = mojito.DefaultLogger()
	Router = mojito.DefaultRouter()

	Logger.Info("Registering application routes...")
	Router.Group("/hello", func(router mojito.Routeable) {
		router.GET("", helloHandler)
		router.GET("/:name", setHelloHandler)
	})

	Logger.Infof("Server has started on %s", address)
	Logger.Error(Router.ListenAndServe(address))
}

type HelloContext struct {
	Cache mojito.Cache `container:"type"`
}

func helloHandler(ctx HelloContext, res *mojito.Response, req *mojito.Request) {
	var name string
	ctx.Cache.GetOrDefault(cacheKey, &name, "world")
	res.String("Hello " + name + "!")
}

func setHelloHandler(ctx HelloContext, res *mojito.Response, req *mojito.Request) {
	var newName = req.ParamOrDefault("name", "world")
	Logger.Field("name", newName).Infof("A new name to greet has been set.")
	ctx.Cache.Set(cacheKey, newName)
	res.String("A new name has been set!")
}

How to use this example code

Once you start this quick example code, a webserver will listen on any IP with the port 8123. On https://localhost:8123/hello you will be greeted with “Hello world!” by default.

To change “world” into something else, you can open https://localhost:8123/hello/yourname and now you will be greeted with whatever name you used.

GitHub

View Github