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 ¶
func WithShutdownTimeout ¶
WithShutdownTimeout sets timeout for graceful shutdown.
func WithTLSConfig ¶ added in v2.4.1
WithTLSConfig sets http.Server.TLSConfig.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func New ¶
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 ¶
Init starts net.Listener after applying all options. Options are applied in same order as they were provided.
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. |
Click to show internal directories.
Click to hide internal directories.