httpserver

package module
Version: v0.0.0-...-453335e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 7 Imported by: 0

README

HTTP Server Module

PkgGoDev goreport

Environment Variables

HOST (Default: localhost)
PORT (Default: 5000)

Getting Started

Provide a http.Handler for httpserver fx Module Simple usage using echo

package main

import (
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"go.uber.org/fx"

	"github.com/lucaskatayama/fx-contrib/httpserver"
)

func router() http.Handler {
	e := echo.New()
	e.Pre(middleware.RemoveTrailingSlash())

	e.Use(middleware.Logger())
	e.Use(middleware.Gzip())

	g := e.Group("/api")

	g.GET("/hello", func(c echo.Context) error {
		return c.String(http.StatusOK, "Hello World!")
	})
	return e
}

func main() {
	app := fx.New(
		httpserver.Module,
		fx.Provide(router),
	)
	app.Run()
}

HealthCheck Module

Environment Variables

HEALTHCHECK_HEALTHZ_PATH (Default: /healthz)
HEALTHCHECK_READINESS_PATH (Default: /readiness)

Documentation

Overview

Package httpserver provides a httpserver fx module.

import "github.com/lucaskatayama/fx-contrib/httpserver"

If HOST and PORT environment variables are not set, the default values are used

HOST: localhost
PORT: 5000
Example

This is a simple usage of httpserver.Module with fx. You must provide a http.Handler to the module.

package main

import (
	"net/http"

	"github.com/lucaskatayama/fx-contrib/httpserver"
	"go.uber.org/fx"
)

func main() {
	app := fx.New(
		httpserver.Module,
		fx.Provide(func() http.Handler {
			mux := http.NewServeMux()
			mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
				w.Write([]byte("Hello"))
				return
			})
			return mux
		}),
	)
	app.Run()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var Module = fx.Options(
	fx.Provide(new),
	fx.Invoke(start),
)

Module provides a fx module

Use on fx app declation

app := fx.New(
   httpserver.Module,
)
View Source
var ModuleHealthCheck = fx.Options(
	fx.Provide(newHealthCheck),
)

ModuleHealthCheck provides a healthcheck module for fx.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL