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.
To install gotham package, you need to install Go and set your Go workspace first.
- 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
- Import it in your code:
import "github.com/sleep2death/gotham"
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))
}