api

command
v0.0.0-...-54e7c99 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

Braza: Simple example of an API application

$ git clone [email protected]:ethoDomingues/braza/examples/api.git
$ cd api
$ go mod init && go mod tidy
$ go run .

access 'localhost:5000' with the browser and see the application in operation

Explanation

// in main.go

type Post struct {
	ID   string
	Text string
} // data structure example

var db = map[string]*Post{} // simple example of database connection

func main() {
	cfg := &braza.Config{
		SecretKey:      "shiiii!!... it's is secret", // used to subscribe to sessions
		Servername:     "localhost:5000",             // reference for using subdomains
		TemplateFolder: "./",                         // reference ctx.RenderTemplate func
	}

	app := braza.NewApp(cfg) // create a new app and set the config
	app.BeforeRequest = beforeReq // exec before each request

	api := getApiRouter() // get a Router
	app.Mount(api)        // mount the router into the app

	app.GET("", func(ctx *braza.Ctx) {
		ctx.RenderTemplate("index.html")
	})            // add a new router to serve the index file
	app.Listen() // run the server
}

func beforeReq(ctx *braza.Ctx) {
	ctx.Global["db"] = db // add db in global context 
}

to be continue....

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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