Skip to content

Using a custom main.go with generated truss services

Leland Batey edited this page Aug 2, 2017 · 3 revisions

Here is a way to wrap an http handler with a custom main.go

  1. Create your own NAME-service/cmd/NAME-custom-server/main.go
  2. Write something like this:
package main

import (
	"context"
	"flag"
	"net/http"

	// This Service
	"github.com/TuneLab/truss/_example/echo-service/svc"
	"github.com/TuneLab/truss/_example/echo-service/svc/server"
	"github.com/TuneLab/truss/_example/echo-service/svc/server/cli"
)

func main() {
	// Update addresses if they have been overwritten by flags
	flag.Parse()

	ctx := context.Background()
	endpoints := server.NewEndpoints()

	h := svc.MakeHTTPHandler(ctx, endpoints)

	h = HandlerWrapper(h)

	http.ListenAndServe(cli.Config.HTTPAddr, h)
}

func HandlerWrapper(h http.Handler) http.Handler {
	return h
}