Categorygithub.com/randlabs/go-webserver/v2
modulepackage
2.0.2
Repository: https://github.com/randlabs/go-webserver.git
Documentation: pkg.go.dev

# 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/v2"
	"github.com/randlabs/go-webserver/v2/middleware"
)

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 *webserver.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
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

Create creates a new webserver.
NewHandlerFromHttpHandler returns a HandlerFunc based on the provided http.Handler.
NewHandlerFromHttpHandlerFunc returns a HandlerFunc based on the provided http.HandlerFunc.

# Constants

4MB.
No description provided by the author
No description provided by the author

# Structs

Options specifies the server creation options.
No description provided by the author
No description provided by the author
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.
RequestErrorHandler is a callback to call if an error is encountered while processing a request.