Skip to content

Latest commit

 

History

History
73 lines (52 loc) · 1.99 KB

README.md

File metadata and controls

73 lines (52 loc) · 1.99 KB

gotham

Build Status Go Report Card codecov

A well-tested/high-performace tcp/protobuf router written in go, highly inspired by Gin, and the source of standard http library:net/http/server.go.

content

Installation

To install gotham package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.11+ is required), then you can use the below Go command to install Gotham.
$ go get -u github.com/sleep2death/gotham
  1. Import it in your code:
import "github.com/sleep2death/gotham"

Quick start

All examples you can find are in /examples folder.

package main

import (
	"log"
	"net/http"

	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes"

	"github.com/sleep2death/gotham"
	"github.com/sleep2death/gotham/examples/pb"
)

func main() {
	// SERVER
	// Starts a new gotham instance without any middleware attached.
	router := gotham.New()

	// Define your handlers.
	router.Handle("/pb/EchoMessage", func(c *gotham.Context) {
		message := new(pb.EchoMessage)

		// If some error fires, you can abort the request.
		if err := proto.Unmarshal(c.Data(), message); err != nil {
			c.AbortWithStatus(http.StatusBadRequest)
			return
		}

		// log.Printf("Ping request received at %s", ptypes.TimestampString(message.Ts))
		message.Message = "Pong"
		message.Ts = ptypes.TimestampNow()
		c.Write(message)
	})

	// Run, gotham, Run...
	addr := ":9090"
	log.Fatal(router.Run(addr))
}