Skip to content

Commit

Permalink
implement the consul storage (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
sillygod authored Jun 7, 2021
1 parent 4a3c6c1 commit 53f6c12
Show file tree
Hide file tree
Showing 9 changed files with 508 additions and 14 deletions.
3 changes: 3 additions & 0 deletions cmd/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# debug
order http_cache before reverse_proxy
admin 0.0.0.0:7777

}

:9991 {
Expand All @@ -19,6 +20,8 @@

}



log {
output file /tmp/logs/caddy/access.log
format console
Expand Down
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
caddycmd "github.com/caddyserver/caddy/v2/cmd"

// plug in Caddy modules here
_ "github.com/caddyserver/caddy/v2/modules/standard"
_ "github.com/sillygod/cdp-cache"
_ "github.com/sillygod/cdp-cache/extends/influxlog"

_ "github.com/caddyserver/caddy/v2/modules/standard"
_ "github.com/sillygod/cdp-cache/extends/storage"
)

func main() {
Expand Down
36 changes: 36 additions & 0 deletions example/consul_storage/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
order http_cache before reverse_proxy
admin 0.0.0.0:7777
debug

storage consul {
addr "consul:9500"
token ""
key_prefix "caddy_https"
}

}

:9991 {
reverse_proxy {
to localhost:9995
}

http_cache {
cache_type in_memory
match_path /

default_max_age 1m
}

}

:9995 {
header Cache-control "public"
root * /tmp/caddy-benchmark
file_server

log {
level info
}
}
15 changes: 7 additions & 8 deletions example/distributed_cache/readme.org
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
* Distributed Cache Example

Note! It's still under development. A lot of issues are remained to be solved.


* Experiment

Before we start, spinning up your environment with ~PROJECT_PATH=/app docker-compose --project-directory=./ -f example/distributed_cache/docker-compose.yaml up~. Then, provision the test data with the helper scrips in the below section. ~docker exec -w /app/benchmark file bash provision.sh~ this will provision test data to the container file whose port is exported 9995.

Start to test the cache is sync or not.
First, ~curl http://localhost:9991/pg31674.txt~. It should go the source(file) to ask the content and then cache it.
Second, ~curl http://localhost:9992/pg31674.txt~. This will try to get the cache from the peer if things are on track.


#+begin_src sh
uri=("http://localhost:9991/pg31674.txt" "http://localhost:9992/pg31674.txt" "http://localhost:9993/pg31674.txt" "http://localhost:9994/pg31674.txt")

Expand All @@ -21,18 +20,18 @@
#+end_src

* Helper scripts

To provision the test data with the one of the following two commands.

#+begin_src sh
PROJECT_PATH=/app docker-compose --project-directory=./ -f example/distributed_cache/docker-compose.yaml exec -w /app/provision cdp "bash provision.sh"
#+end_src

#+begin_src sh
docker exec -w /app/benchmark file bash provision.sh
#+end_src

If you want to run an interactive shell for development, the following command will do it for you.
#+begin_src sh
PROJECT_PATH=/app docker-compose --project-directory=./ -f example/distributed_cache/docker-compose.yaml run -p9992:9991 --entrypoint="/bin/bash" cdp2
#+end_src
#+end_src
62 changes: 62 additions & 0 deletions extends/storage/caddyfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mystorage

import (
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)

const (
keyAddr = "addr"
keyToken = "token"
keyKeyPrefix = "key_prefix"
)

// Config is the configuration for consul storage
type Config struct {
Addr string `json:"addr.omitempty"`
Token string `json:"token,omitempty"`
KeyPrefix string `json:"key_prefix,omitempty"`
}

func getDefaultConfig() *Config {
return &Config{
KeyPrefix: "_consul_cert_",
Addr: "localhost:8500",
}
}

// UnmarshalCaddyfile deserialize Caddyfile tokens into consul storage's config
// storage consul {
// addr
// token
// key_prefix
// }
func (s *Storage) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
config := getDefaultConfig()

for d.Next() {
for d.NextBlock(0) {
parameter := d.Val()
args := d.RemainingArgs()

switch parameter {
case keyAddr:
config.Addr = args[0]
case keyToken:
config.Token = args[0]
case keyKeyPrefix:
config.KeyPrefix = args[0]

default:
return d.Errf("unrecognized subdirective %s", parameter)

}

}
}
s.Config = config
return nil
}

var (
_ caddyfile.Unmarshaler = (*Storage)(nil)
)
Loading

0 comments on commit 53f6c12

Please sign in to comment.