proxy

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2022 License: MIT Imports: 6 Imported by: 1

README

JJC-REVERSE-PROXY

Minimal and convenience library to share the same port between multiple services (api, frontend, auth, ...) using reverse proxies.

How to use

In this example there are 3 servers running in the same machine:

  • Frontend server on port 8081
  • Api server on port 8082
  • Reverse proxy server on port 8080 (the only port accessible from internet)
    The reverse proxy redirects all request to local ports 8081 or 8082 depending on its routes.
import(
    "sync"
    proxy "github.com/jjcapellan/jjc-reverse-proxy"    
)

func main(){
    wg := &sync.WaitGroup{}
    
    // Api server is running and listening on port 8082.
    // All request to routes with path prefix "api" (Example: www.site.com/api/user/23415)
    // will be redirected to port 8082.
    proxy.AddProxy("api", "8082")
    // Frontend server is running and listening on port 8081.
    // Must be at least one proxy configured with this path "\". All request not redirected
    // by other proxies will be redirected to port 8081.
    proxy.AddProxy("/", "8081")

    go func(){
        defer wg.Done()
        wg.Add(1)
        // Starts the main server on public port 8080. All request are received in this port
        // and redirected to the api port or frontend port according to its routes.
        proxy.Start("8080")
        defer proxy.Server.Close()
    }

    wg.Wait()
}

Dependencies

This library is built over standard golang libraries, so it hasn't external dependencies.

Documentation

Index

Constants

This section is empty.

Variables

Contains all configured reverse proxies. map[route]proxy

View Source
var Server *http.Server

Proxy manager server

Functions

func AddProxy

func AddProxy(route string, port string) error

AddProxy adds and configure one reverse proxy. All request with path prefix "route" will be redirected to the given port.

Example: AddProxy("api", "8084") -> redirects www.site.com:8080/api/user/john to port 8084

func Start

func Start(port string) error

Start inits the proxy manager server on the given port.

Types

This section is empty.

Jump to

Keyboard shortcuts

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