httpserver

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2026 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Overview

Package httpserver provides http server as module.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opt

type Opt func(s *Server) error

func WithAddr

func WithAddr(addr string) Opt

WithAddr sets http.Server.Addr.

func WithHandler

func WithHandler(h http.Handler) Opt

WithHandler sets http.Server.Handler.

func WithName added in v2.3.0

func WithName(name string) Opt

WithName sets server name.

func WithServer

func WithServer(srv *http.Server) Opt

WithServer sets http.Server for module.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Opt

WithShutdownTimeout sets timeout for graceful shutdown.

func WithTLSConfig added in v2.4.1

func WithTLSConfig(tlsConfig *tls.Config) Opt

WithTLSConfig sets http.Server.TLSConfig.

type Server

type Server struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Opt) *Server

New creates Server module with given options.

Example
package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/elisasre/go-common/v2/service/module/httpserver"
)

func main() {
	httpserver.New(
		httpserver.WithServer(&http.Server{ReadHeaderTimeout: time.Second}),
		httpserver.WithAddr("127.0.0.1:0"),
		httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "Hello")
		})),
	)
}
Example (Https)
package main

import (
	"crypto/tls"
	"fmt"
	"net/http"

	"github.com/elisasre/go-common/v2/service/module/httpserver"
)

func main() {
	// Load your certificate and key files
	cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		panic(err)
	}

	tlsConfig := &tls.Config{
		Certificates: []tls.Certificate{cert},
		MinVersion:   tls.VersionTLS12,
	}

	httpserver.New(
		httpserver.WithAddr("127.0.0.1:8443"),
		httpserver.WithTLSConfig(tlsConfig),
		httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, "Hello HTTPS")
		})),
	)
}

func (*Server) Init

func (s *Server) Init() error

Init starts net.Listener after applying all options. Options are applied in same order as they were provided.

func (*Server) Name

func (s *Server) Name() string

func (*Server) Run

func (s *Server) Run() error

Run starts serving http/https request and can be called after initialization.

func (*Server) Stop

func (s *Server) Stop() error

Stop calls shutdown for server.

func (*Server) URL

func (s *Server) URL() string

URL returns server's URL and can be called after initialization.

Directories

Path Synopsis
Package pprof provides pprof handler options for httpserver module.
Package pprof provides pprof handler options for httpserver module.
Package prom provides prometheus metrics handler options for httpserver module.
Package prom provides prometheus metrics handler options for httpserver module.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL