Giter Site home page Giter Site logo

caddy-consul's Introduction

Consul Plugin for Caddy

Forked from https://github.com/krishamoud/caddy-consul

Installation

1. Get the source

go get github.com/chenjpu/caddy-consul

2. Get the Caddy source

go get github.com/mholt/caddy/caddy

3. Add the plugin

// github.com/mholt/caddy/caddy/caddymain/run.go
package caddymain

import (
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"runtime"
	"strconv"
	"strings"

	"gopkg.in/natefinch/lumberjack.v2"

	"github.com/xenolf/lego/acme"

	"github.com/mholt/caddy"
	// plug in the HTTP server type
	_ "github.com/mholt/caddy/caddyhttp"

	"github.com/mholt/caddy/caddytls"
	// This is where other plugins get plugged in (imported)
	_ "github.com/chenjpu/caddy-consul" // ADD THIS LINE
)

4. Build Caddy

cd github.com/mholt/caddy/caddy
go get github.com/caddyserver/builds
go run build.go

5. Set CONSUL ENV variable

This tells consul where to connect

export CONSUL=http://10.0.0.1:8500

6. Set CADDYFILE_PATH env

This tells caddy where to look for the Caddyfile.tmpl template

export CADDYFILE_PATH=/path/to/Caddyfile.tmpl

7. Run Caddy

Either add your newly built binary to your $PATH or just run ./caddy from where you built the application.

Example Caddyfile.tmpl

{{range $domain, $services := .}} http://{{$domain}}.xxx.com {
  root /var/www/dev.xxx.com
  gzip
  proxy /services {
     header_upstream Host {host}
     header_upstream X-Real-IP {remote}
     header_upstream X-Forwarded-For {remote}
     header_upstream X-Forwarded-Proto {scheme}
     max_conns 200
     policy ip_hash
    {{range $key, $service := $services}}upstream {{.Address}}:{{.ServicePort}}
    {{end}}
  }
  rewrite / {
   if {uri} not_starts_with /services
   to {uri} /
  }
}
{{end}}

The above will output something like this:

 http://test-dev.xxx.com {
  root /var/www/dev.xxx.com
  gzip
  proxy /services {
     header_upstream Host {host}
     header_upstream X-Real-IP {remote}
     header_upstream X-Forwarded-For {remote}
     header_upstream X-Forwarded-Proto {scheme}
     max_conns 200
     policy ip_hash
    upstream 10.110.200.26:23530

  }
  rewrite / {
   if {uri} not_starts_with /services
   to {uri} /
  }
}

TODOS:

  1. Tests
  2. Refactoring

caddy-consul's People

Contributors

chenjpu avatar krishamoud avatar

Watchers

 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.