A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/gramework/gramework below:

gramework/gramework: Fast and Reliable Golang Web Framework

The Good Framework

Gramework long-term testing stand metrics screenshot made with Gramework Stats Dashboard and metrics middleware

Gramework is a fast, highly effective, reliable, SPA-first, go-way web framework made by a fasthttp maintainer. You get the simple yet powerful API, we handle optimizations internally. We're always glad to see your feature requests and PRs.

Reasons to use Gramework

Go >= 1.10.8 is the oldest continously tested and supported version.

If you encounter any vulnerabilities then please feel free to submit them via k@gramework.win.

This project exists thanks to our awesome contributors! [Contribute].

Thank you to all our backers! 🙏 [Become a backer]

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

The example below will serve "hello, grameworld". Gramework will register the bind flag for you, that allows you to choose another ip/port that gramework should listen on:

package main

import (
	"github.com/gramework/gramework"
)

func main() {
	app := gramework.New()

	app.GET("/", "hello, grameworld")

	app.ListenAndServe()
}

If you don't want to support the bind flag then pass the optional address argument to ListenAndServe.

NOTE: all examples below will register the bind flag.

From version: 1.1.0-rc1

The example below will serve {"hello":"grameworld"} from the map. Gramework will register the bind flag for you, that allows you to choose another ip/port that gramework should listen on:

package main

import (
	"github.com/gramework/gramework"
)

func main() {
	app := gramework.New()

	app.GET("/", func() map[string]interface{} {
		return map[string]interface{}{
			"hello": "gramework",
		}
	})

	app.ListenAndServe()
}

From version: 1.1.0-rc1

The example below will serve {"hello":"grameworld"} from the struct. Gramework will register the bind flag for you, that allows you to choose another ip/port that gramework should listen on:

package main

import (
	"github.com/gramework/gramework"
)

type SomeResponse struct {
	hello string
}

func main() {
	app := gramework.New()

	app.GET("/", func() interface{} {
		return SomeResponse{
			hello: "gramework",
		}
	})

	app.ListenAndServe()
}

The example below will serve static files from ./files:

package main

import (
	"github.com/gramework/gramework"
)

func main() {
	app := gramework.New()

	app.GET("/*any", app.ServeDir("./files"))

	app.ListenAndServe()
}

The example below will serve a byte slice:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/gramework/gramework"
)

type SomeData struct {
	Name string
	Age  uint8
}

func main() {
	app := gramework.New()

	d := SomeData{
		Name: "Grame",
		Age:  20,
	}

	// service-wide CORS. you can also instead of using middleware
	// call ctx.CORS() manually
	app.Use(app.CORSMiddleware())

	app.GET("/someJSON", func(ctx *gramework.Context) {
		// send json, no metter if user asked for json, xml or anything else.
		if err := ctx.JSON(d); err != nil {
			// you can return err instead of manual checks and Err500() call.
			// See next handler for example.
			ctx.Err500()
		}
	})

	app.GET("/simpleJSON", func(ctx *gramework.Context) error {
		return ctx.JSON(d)
	})

	app.GET("/someData", func(ctx *gramework.Context) error {
		// send data in one of supported encodings user asked for.
		// Now we support json, xml and csv. More coming soon.
		sentType, err := ctx.Encode(d)
		if err != nil {
			ctx.Logger.WithError(err).Error("could not process request")
			return err
		}
		ctx.Logger.WithField("sentType", sentType).Debug("some request-related message")
		return nil
	})

	// you can omit context if you want, return `interface{}`, `error` or both.
	app.GET("/simplestJSON", func() interface{} {
		return d
	})

	// you can also use one of built-in types as a handler, we got you covered too
	app.GET("/hostnameJSON", fmt.Sprintf(`{"hostname": %q}`, os.Hostname()))

	wait := make(chan struct{})
	go func() {
		time.Sleep(10 * time.Minute)
		app.Shutdown()
		wait <- struct{}{}
	}()

	app.ListenAndServe()

	// allow Shutdown() to stop the app properly.
	// ListenAndServe will return before Shutdown(), so we should wait.
	<-wait
}
Using dynamic handlers, example 2. Simple FastHTTP-compatible handlers.

This example demonstrates how to migrate from fasthttp to gramework without rewriting your handlers.

package main

import (
	"github.com/gramework/gramework"
	"github.com/valyala/fasthttp"
)

func main() {
	app := gramework.New()

	app.GET("/someJSON", func(ctx *fasthttp.RequestCtx) {
		ctx.WriteString("another data")
	})

	app.ListenAndServe()
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4