Coder Social home page Coder Social logo

rusq / vhoster Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 64 KB

Vhoster is a library and a dynamic virtual host gateway

License: BSD 3-Clause "New" or "Revised" License

Go 96.79% Dockerfile 1.18% Makefile 2.03%
docker docker-compose gateway library network utility virtual-host

vhoster's Introduction

Vhoster

Vhoster is a library for creating virtual hosts in a Go server. It is intended to be used as a library, but it also includes a gateway that can be used to create and delete vhosts dynamically.

The gateway can also be used as a central point of entry for all requests, and it can be used to load balance requests to multiple servers.

Quick Start

Locally

  1. Start the server listening on port 8082

    go run cmd/server/main.go
  2. Start the gateway listening on port 8083

    go run cmd/gateway/main.go
  3. Create the vhost in the gateway

    curl -X POST -H "Content-Type: application/json" -d '{"host_prefix": "test", "target": "http://localhost:8082"}' http://localhost:8083/vhost/

    then, to test, run curl test.localhost:8081, and you'll get the "Hello, World!"

  4. Delete the vhost in the gateway

    curl -X DELETE localhost:8083/vhost/test

In docker compose

  1. Run docker compose up, it will build and start two container: (a) the gateway on port 8080 and vhost api on port 8083, and (b) the test server on port 8082, but the testserver is unreachable from the local machine directly. We will set up a virtual host to access it.

    If you want to verify that testserver is not accessible, try running curl localhost:8082 and you'll get an error.

  2. It comes with preconfigured vhost "test.localhost:8080" that routes to "http://testserver:8082". You can verify that it works by running curl test.localhost:8080, and you'll get the "Hello, World!"

  3. Run the following command to instruct gateway to route all requests to "hello.localhost:8080" to "http://testserver:8082":

    curl -X POST -H "Content-Type: application/json" -d '{"host_prefix": "hello", "target": "http://testserver:8082"}' http://localhost:8083/vhost/

    This will instruct the gateway to route all requests that arrive at "test.localhost:8080" to "http://testserver:8082".

  4. Now, you can access the test server by running curl hello.localhost:8080, and you'll get the "Hello, World!"

    At this point, both "test.localhost:8080" and "hello.localhost:8080" will route to the test server.

  5. To list existing routes, and verify that, run:

    curl -X GET localhost:8083/vhost/

    (-X GET is optional and instructs curl to use GET method, which is the default method for curl, so you can omit it: curl localhost:8083/vhost/)

    The API server will respond on this GET request with JSON that lists all existing vhosts.

  6. You can delete the route now, by running:

    curl -X DELETE localhost:8083/vhost/hello

vhoster's People

Contributors

rusq avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.