api

package
v2.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2021 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Overview

Example
package main

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

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

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.UseDefaultRecover()
	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
}
Output:

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"`
	Port              int    `json:"port" yaml:"port"`
	EnableCORS        bool   `json:"enable_cors" yaml:"enable_cors"`
	ApiRequestTimeout int    `json:"api_request_timeout" yaml:"api_request_timeout"` // nolint:golint
}

Config is a parameters for `http.Server`.

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 (*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