Miniature Monkey: small http framework layer built on top of gorilla/mux
Miniature Monkey simply bootstraps a simple http application with gorilla/mux
, adding a couple of endpoints like status and the prometheus metrics handler.
The API
struct offers a set of methods to add endpoints via the BlueprintInterface
abstraction, also you can register Middleware functions
Examples
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/shipperizer/miniature-monkey/config"
"github.com/shipperizer/miniature-monkey/core"
"github.com/shipperizer/miniature-monkey/utils"
monitoringLibrary "github.com/some/monitoring/library"
)
func main() {
monitor := monitoringLibrary.NewMonitor()
logger := utils.NewLogger("info")
apiCfg := config.NewAPIConfig(
"test-api",
config.NewCORSConfig("google.com"),
monitor,
logger,
)
srv := &http.Server{
Addr: "0.0.0.0:8000",
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: core.NewApi(apiCfg).Handler(),
}
go func() {
if err := srv.ListenAndServe(); err != nil {
logger.Fatal(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
logger.Info("Shutting down")
os.Exit(0)
}