# README
go-webserver
HTTP web server library for Go based on FastHttp
Usage with example
package example
import (
"fmt"
"os"
"os/signal"
"syscall"
webserver "github.com/randlabs/go-webserver"
"github.com/randlabs/go-webserver/middleware"
"github.com/randlabs/go-webserver/request"
)
type testApiOutput struct {
Status string `json:"status"`
}
func main() {
// Options struct has all the documentation
srvOpts := webserver.Options{
Address: "127.0.0.1",
Port: 3000,
}
srv, err := webserver.Create(srvOpts)
if err != nil {
fmt.Printf("unable to create web server [%v]\n", err)
return
}
// Add some middlewares
srv.Use(middleware.DefaultCORS())
// Setup a route
srv.GET("/test", getTestApi)
// Start server
err = srv.Start()
if err != nil {
fmt.Printf("unable to start web server [%v]\n", err)
return
}
fmt.Println("Server running. Press CTRL+C to stop.")
// Wait for CTRL+C
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
fmt.Println("Shutting down...")
// Stop web server
srv.Stop()
}
func getTestApi(req *request.RequestContext) error {
// Prepare output
output := testApiOutput{
Status: "all systems operational",
}
// Encode and send output
req.WriteJSON(output)
req.Success()
return nil
}
License
See LICENSE
file for details.
# Packages
No description provided by the author
No description provided by the author
No description provided by the author
# Functions
Create creates a new webserver.
HandlerFromHttpHandler returns a HandlerFunc based on the provided http.Handler.
HandlerFromHttpHandlerFunc returns a HandlerFunc based on the provided http.HandlerFunc.
# Constants
4MB.
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
Options specifies the server creation options.
Server is the main server object.
ServerFilesOptions sets the parameters to use in a ServeFiles call.
# Type aliases
HandlerFunc defines a function that handles a request.
ListenErrorHandler is a callback to call if an error is encountered in the network listener.
MiddlewareFunc defines a function that is executed when a request is received.
RequestErrorHandler is a callback to call if an error is encountered while processing a request.