api

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Overview

Example
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/lancer-kit/uwe/v3"
)

func main() {
	// fill configurations for the predefined worker that start an HTTP server
	apiCfg := Config{
		Host:              "0.0.0.0",
		Port:              8080,
		EnableCORS:        false,
		APIRequestTimeout: 0,
	}

	// initialize new instance of Chief
	chief := uwe.NewChief()
	chief.SetEventHandler(uwe.STDLogEventHandler())

	// will add workers into the pool
	chief.AddWorker("app-server", NewServer(apiCfg, getRouter()))

	// init all registered workers and run it all
	chief.Run()
}

// getRouter is used to declare an API scheme,
func getRouter() http.Handler {
	// instead default can be used any another compatible router
	mux := http.NewServeMux()
	mux.HandleFunc("/hello/uwe", func(w http.ResponseWriter, r *http.Request) {
		_, _ = fmt.Fprintln(w, "hello world")
	})

	log.Println("REST API router initialized")
	return mux
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Host       string `json:"host" yaml:"host" toml:"host"`
	Port       int    `json:"port" yaml:"port" toml:"port"`
	EnableCORS bool   `json:"enable_cors" yaml:"enable_cors" toml:"enable_cors"`
	// nolint:lll
	APIRequestTimeout int `json:"api_request_timeout" yaml:"api_request_timeout" toml:"api_request_timeout"`
	ReadHeaderTimeout int `json:"read_header_timeout" yaml:"read_header_timeout" toml:"read_header_timeout"`
}

Config is a parameters for `http.Server`. APIRequestTimeout and ReadHeaderTimeout time.Duration in Seconds.

func (*Config) TCPAddr

func (c *Config) TCPAddr() string

TCPAddr returns tcp address for server.

func (*Config) Validate

func (c *Config) Validate() error

Validate - Validate config required fields

type Server

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

Server is worker by default for starting a standard HTTP server. Server requires configuration and filled `http.Handler`. The HTTP server will work properly and will be correctly disconnected upon a signal from Supervisor (Chief). Warning: this Server does not process SSL/TLS certificates on its own.

To start an HTTPS server, look for a specific worker.

func NewServer

func NewServer(config Config, router http.Handler) *Server

NewServer returns a new instance of `Server` with the passed configuration and HTTP router.

func NewServerWithInitFunc

func NewServerWithInitFunc(config Config, routerInit func(ctx uwe.Context) (http.Handler, error)) *Server

NewServerWithInitFunc returns a new instance of `Server` with the passed configuration. Server router will be initialized during the call of Run() method. This allows to pass and use uwe.Context in handlers, e.g. to use imq and Mailbox.

func (*Server) Init

func (s *Server) Init() error

Init is a method to satisfy `uwe.Worker` interface.

func (*Server) Run

func (s *Server) Run(ctx uwe.Context) error

Run starts serving the passed `http.Handler` with HTTP server.

Jump to

Keyboard shortcuts

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