diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..92f9976 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,9 @@ +kind: pipeline +type: docker +name: default + +steps: +- name: test + image: golang + commands: + - ls -lah \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e895468 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vegvisir.json +*.prof diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2004bdc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.16-alpine + +RUN mkdir /go/src/vegvisir +WORKDIR /go/src/vegvisir + +COPY vegvisir.json go.mod go.sum ./ +COPY pkg/ ./pkg + +RUN go mod download + +CMD ["go", "run", "pkg/main.go"] \ No newline at end of file diff --git a/Makefile b/Makefile index 0418ba9..eefd431 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,17 @@ - - run: - go run . + go run pkg/main.go + +run-profiler: + go run pkg/main.go -cpuprofile cpu.prof -memprofile mem.prof + +profile-cpu-web: + go tool pprof -http=127.0.0.1:9990 cpu.prof + +profile-mem-web: + go tool pprof -http=127.0.0.1:9991 mem.prof build: - go build . + go build pkg/main.go test: - go test . + go test pkg/main.go diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d9b180b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,24 @@ +version: '3.7' + +services: + app: + container_name: vegvisir_app + build: . + ports: + - 8080:8080 + networks: + - vegvisir-network + links: + - redis + + redis: + container_name: vegvisir_redis + image: redis:6.2-alpine + ports: + - 6379:6379 + networks: + - vegvisir-network + +networks: + vegvisir-network: + driver: bridge diff --git a/go.mod b/go.mod index b60d587..3664d03 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,15 @@ module vegvisir go 1.13 + +require ( + github.com/andybalholm/brotli v1.0.3 // indirect + github.com/go-redis/redis v6.15.9+incompatible + github.com/google/go-cmp v0.5.6 // indirect + github.com/klauspost/compress v1.13.1 // indirect + github.com/onsi/ginkgo v1.15.0 // indirect + github.com/onsi/gomega v1.10.5 // indirect + github.com/valyala/fasthttp v1.28.0 + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..35d2356 --- /dev/null +++ b/go.sum @@ -0,0 +1,83 @@ +github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= +github.com/andybalholm/brotli v1.0.3/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.15.0/go.mod h1:hF8qUzuuC8DJGygJH3726JnCZX4MYbRB8yFfISqnKUg= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.10.5/go.mod h1:gza4q3jKQJijlu05nKWRCW/GavJumGt8aNRxWg7mt48= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasthttp v1.28.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA= +github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210510120150-4163338589ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go deleted file mode 100644 index da29a2c..0000000 --- a/main.go +++ /dev/null @@ -1,4 +0,0 @@ -package main - -func main() { -} diff --git a/modd.conf b/modd.conf new file mode 100644 index 0000000..900dd04 --- /dev/null +++ b/modd.conf @@ -0,0 +1,4 @@ +# Exclude all test files of the form *_test.go +**/*.go **/vegvisir.json !**/*_test.go { + daemon +sigterm: make run +} diff --git a/pkg/cache/datastore.go b/pkg/cache/datastore.go new file mode 100644 index 0000000..f61593d --- /dev/null +++ b/pkg/cache/datastore.go @@ -0,0 +1,16 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +type CacheDatastore interface { + SetKey(string, interface{}, int) error + + GetKey(string) (interface{}, error) +} diff --git a/pkg/cache/redis_datastore.go b/pkg/cache/redis_datastore.go new file mode 100644 index 0000000..4fac007 --- /dev/null +++ b/pkg/cache/redis_datastore.go @@ -0,0 +1,57 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +import ( + "strconv" + "time" + + "github.com/go-redis/redis" +) + +func NewRedisDatastore(host string, port int) *RedisDatastore { + return &RedisDatastore{ + client: redis.NewClient(&redis.Options{ + Addr: host + ":" + strconv.Itoa(port), + Password: "", // FIXME: use env or param + DB: 0, // FIXME: use env or param + }), + cache: make(map[string]interface{}), + } +} + +type RedisDatastore struct { + client *redis.Client + cache map[string]interface{} +} + +func (ds *RedisDatastore) SetKey(key string, data interface{}, ttl int) error { + err := ds.client.Set(key, data, time.Duration(ttl)*time.Second).Err() + if err != nil { + return err + } + + // ds.cache[key] = data + + return nil +} + +func (ds *RedisDatastore) GetKey(key string) (interface{}, error) { + // if data, ok := ds.cache[key]; ok { + // return data, nil + // } + + data, err := ds.client.Get(key).Result() + if err != nil { + return nil, err + } + + return data, nil +} diff --git a/pkg/cache/response.go b/pkg/cache/response.go new file mode 100644 index 0000000..c213157 --- /dev/null +++ b/pkg/cache/response.go @@ -0,0 +1,65 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +import ( + "encoding/json" + "log" +) + +type ResponseCache struct { + URL string + Body string + // Headers map[string]string +} + +type ResponseCacheManager struct { + datastore CacheDatastore + prefix string + ttl int +} + +func NewResponseCacheManager(datastore CacheDatastore, ttl int) ResponseCacheManager { + return ResponseCacheManager{ + datastore: datastore, + prefix: "response_", + ttl: ttl, + } +} + +func (rm *ResponseCacheManager) Save(name string, r ResponseCache) { + data, err := json.Marshal(r) + if err != nil { + log.Println("JSON:", err) // FIXME + } + + name = rm.prefix + name + err = rm.datastore.SetKey(name, string(data), rm.ttl) + if err != nil { + log.Println("REDIS:", err, name) // FIXME + } +} + +func (rm *ResponseCacheManager) Load(name string) (bool, *ResponseCache) { + name = rm.prefix + name + + data, err := rm.datastore.GetKey(name) + if err != nil { + return false, &ResponseCache{} + } + + rc := &ResponseCache{} + err = json.Unmarshal([]byte(data.(string)), &rc) + if err != nil { + log.Println("JSON:", err) // FIXME + } + + return true, rc +} diff --git a/pkg/cache/route.go b/pkg/cache/route.go new file mode 100644 index 0000000..ff28a1c --- /dev/null +++ b/pkg/cache/route.go @@ -0,0 +1,63 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package cache + +import ( + "encoding/json" + "log" +) + +type RouteCache struct { + SourceUrl string + TargetUrl string +} + +type RouteCacheManager struct { + datastore CacheDatastore + ttl int +} + +func NewRouteCacheManager(datastore CacheDatastore, ttl int) RouteCacheManager { + return RouteCacheManager{ + datastore: datastore, + ttl: ttl, + } +} + +func (rm *RouteCacheManager) Save(name string, r RouteCache) { + data, err := json.Marshal(r) + if err != nil { + log.Println("JSON:", err) // FIXME + } + + err = rm.datastore.SetKey("route_"+name, data, rm.ttl) + if err != nil { + log.Println("REDIS:", err, name) // FIXME + } +} + +func (rm *RouteCacheManager) Load(name string) (bool, RouteCache) { + name = "route_" + name + + data, err := rm.datastore.GetKey(name) + if err != nil { + log.Println("REDIS:", err, name) // FIXME + + return false, RouteCache{} + } + + rc := RouteCache{} + err = json.Unmarshal([]byte(data.(string)), &rc) + if err != nil { + log.Println("JSON:", err) // FIXME + } + + return true, rc +} diff --git a/pkg/client/http.go b/pkg/client/http.go new file mode 100644 index 0000000..0510e9a --- /dev/null +++ b/pkg/client/http.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package client diff --git a/pkg/client/tcp.go b/pkg/client/tcp.go new file mode 100644 index 0000000..4bd534b --- /dev/null +++ b/pkg/client/tcp.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ + +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package client diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..6fb1785 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,61 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package config + +import ( + "encoding/json" + "io/ioutil" + "os" +) + +type Config struct { + Server Server + Backends map[string]Backend +} + +type Server struct { + Address string + Port int +} + +type Backend struct { + PrefixUrl string + BackendAddress string + Protocol string + Routes []Route +} + +type Route struct { + Pattern string + Target string +} + +var DefaultRoute = Route{ + Pattern: "(.*)", + Target: "", +} + +func (c *Config) Load(cPath string) error { + if _, err := os.Stat(cPath); err != nil { + return err + } + + data, err := ioutil.ReadFile(cPath) + if err != nil { + return err + } + + err = json.Unmarshal(data, &c) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go new file mode 100644 index 0000000..7e14d54 --- /dev/null +++ b/pkg/handler/handler.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ + +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package handler diff --git a/pkg/main.go b/pkg/main.go new file mode 100644 index 0000000..27e15bf --- /dev/null +++ b/pkg/main.go @@ -0,0 +1,59 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package main + +import ( + "flag" + "log" + "os" + "runtime" + "runtime/pprof" + "vegvisir/pkg/server" +) + +var ( + cFile = flag.String("c", "vegvisir.json", "Path to config file") + + // for profiling... + cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") + memprofile = flag.String("memprofile", "", "write memory profile to `file`") +) + +func main() { + + flag.Parse() + // cpu profiling + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + log.Fatal("could not create CPU profile: ", err) + } + defer f.Close() // error handling omitted for example + if err := pprof.StartCPUProfile(f); err != nil { + log.Fatal("could not start CPU profile: ", err) + } + defer pprof.StopCPUProfile() + } + + server.NewServer(*cFile).Run() + + // memory profiling + if *memprofile != "" { + f, err := os.Create(*memprofile) + if err != nil { + log.Fatal("could not create memory profile: ", err) + } + defer f.Close() // error handling omitted for example + runtime.GC() // get up-to-date statistics + if err := pprof.WriteHeapProfile(f); err != nil { + log.Fatal("could not write memory profile: ", err) + } + } +} diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..ee9f769 --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,176 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package server + +import ( + "context" + "fmt" + "log" + "os" + "os/signal" + "regexp" + "strings" + "time" + "vegvisir/pkg/cache" + "vegvisir/pkg/config" + + "github.com/valyala/fasthttp" +) + +const ( + Version = "0.1" + Name = "Vegvisir/" + Version +) + +type Server struct { + Config config.Config + + cFilePath string + rCache map[string]cache.RouteCache // Internal route cache + routeCM cache.RouteCacheManager // Redis route cache + respCM cache.ResponseCacheManager // Redis response cache +} + +func NewServer(cPath string) *Server { + datastore := cache.NewRedisDatastore("127.0.0.7", 6379) // FIXME use config or env... + + return &Server{ + cFilePath: cPath, + rCache: make(map[string]cache.RouteCache), + routeCM: cache.NewRouteCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env... + respCM: cache.NewResponseCacheManager(datastore, 30), //FIXME use ttl(seconds) from config or env... + } +} + +func (s *Server) Run() { + if err := s.Config.Load(s.cFilePath); err != nil { + log.Fatalln("Unable to find config file: ", s.cFilePath, err) + } + + go func() { + serverAddress := s.Config.Server.Address + ":" + fmt.Sprint(s.Config.Server.Port) + if err := fasthttp.ListenAndServe(serverAddress, s.mainHandler); err != nil { + log.Fatalf("Server panic! Error message: %s", err) + } + }() + + log.Println("Server started") + + // Wait for an interrupt + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt, os.Kill) + <-interrupt + + log.Println("SIGKILL or SIGINT caught, shutting down...") + + // Attempt a graceful shutdown + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + s.Shutdown(ctx) + log.Println("Server shutdown successfully.") +} + +func (s *Server) Shutdown(ctx context.Context) { // TODO: wait for all connections to finish + log.Println("Shuting down finished") +} + +func (s *Server) mainHandler(ctx *fasthttp.RequestCtx) { + ctx.Response.Header.Add(fasthttp.HeaderServer, Name) + + // move all below logic to concrete handler or sth.... + reqUri, sReqUri, sReqMethod := ctx.RequestURI(), string(ctx.RequestURI()), string(ctx.Method()) + log.Println("Incomming request:", sReqMethod, sReqUri) + + found, route := s.findRouteByRequestURI(reqUri) + if !found { + // FIXME: return 404/5xx error in repsonse, maybe define it in Backend config? + ctx.SetStatusCode(fasthttp.StatusNotFound) + return + } + + // handle response caching + if ok, data := s.respCM.Load(sReqUri); ok { + log.Println("Read resp from cache: ", route.TargetUrl) + + ctx.SetBody([]byte(data.Body)) + } else { + log.Println("Send req to backend url: ", route.TargetUrl) + + // prepare to send request to backend - separate + bckReq := fasthttp.AcquireRequest() + bckResp := fasthttp.AcquireResponse() + defer fasthttp.ReleaseRequest(bckReq) + defer fasthttp.ReleaseResponse(bckResp) + + // copy headers from backend response and prepare request for backend - separate + bckReq.SetRequestURI(route.TargetUrl) + bckReq.Header.SetMethod(sReqMethod) + + err := fasthttp.Do(bckReq, bckResp) + if err != nil { + ctx.SetStatusCode(fasthttp.StatusInternalServerError) + return + } + + ctx.Response.Header.SetBytesV(fasthttp.HeaderContentType, bckResp.Header.ContentType()) + ctx.SetStatusCode(bckResp.StatusCode()) + ctx.SetBody(bckResp.Body()) + + // save response to cache + respCache := cache.ResponseCache{ + URL: sReqUri, + Body: string(bckResp.Body()), + // Headers: [] + } // FIXME: prepare resp cache struct in respCM.Save method or other service... + s.respCM.Save(sReqUri, respCache) + } +} + +func (s *Server) findRouteByRequestURI(uri []byte) (bool, cache.RouteCache) { + var sUri string = string(uri) + + for bId := range s.Config.Backends { + bck := s.Config.Backends[bId] + if !strings.Contains(sUri, bck.PrefixUrl) { + continue + } + + for rId := range bck.Routes { + route := &bck.Routes[rId] + + // if ok, cRoute := s.rCacheManager.Load(sUri); ok { + // return true, cRoute + // } + if cRoute, ok := s.rCache[sUri]; ok { + return true, cRoute + } + + rgxp := regexp.MustCompile(fmt.Sprintf("%s%s", bck.PrefixUrl, route.Pattern)) + if rgxp.Match(uri) { + targetUrl := bck.BackendAddress + rgxp.ReplaceAllString(sUri, route.Target) + + cRoute := cache.RouteCache{ // FIXME: data duplication and use short alias for backend and route! + // Backend: bck, + // Route: *route, + SourceUrl: sUri, + TargetUrl: targetUrl, + } + + // s.rCacheManager.Save(sUri, cRoute) + s.rCache[sUri] = cRoute + + return true, cRoute + } + } + } + + return false, cache.RouteCache{} +} diff --git a/pkg/server/struct.go b/pkg/server/struct.go new file mode 100644 index 0000000..8dbb0e4 --- /dev/null +++ b/pkg/server/struct.go @@ -0,0 +1,10 @@ +// ___ ____ ___ ___ +// \ \ / / | _ | __| \ \ / / || | __ || || _ | +// \ \/ / |___ | |__ \ \/ / || |___ || ||___| +// \ / | _ | _ | \ / || __ | || ||\\ +// \/ |___ |___ | \/ || ____| || || \\ +// +// Copyright (c) 2021 Piotr Biernat. https://pbiernat.dev. MIT License +// Repo: https://git.pbiernat.dev/golang/vegvisir + +package server diff --git a/vegvisir.json b/vegvisir.json new file mode 100644 index 0000000..7bd5829 --- /dev/null +++ b/vegvisir.json @@ -0,0 +1,39 @@ +{ + "server": { + "address": "127.0.0.1", + "port": 8080 + }, + "backends": { + "news-app": { + "prefixUrl": "news/", + "backendAddress": "http://172.17.0.1:3030", + "routes": [{ + "pattern": "new/(.*)/([0-9]*)", + "target": "new-url/$1/$2" + },{ + "pattern": "new/(.*)", + "target": "$1" + },{ + "pattern": "(.+)", + "target": "url" + }] + }, + "article-app": { + "prefixUrl": "art/", + "backendAddress": "http://172.17.0.1:3030", + "routes": [{ + "pattern": "art1/(.*)", + "target": "art-url/$1" + },{ + "pattern": "([a-zA-Z0-9]*)", + "target": "art-global/$1" + }] + } + }, + "cache": { + "ttl": 300, + "route_ttl": 300, + "response_ttl": 300, + "headers_ttl": 300 + } +} \ No newline at end of file diff --git a/vegvisir.json.dist b/vegvisir.json.dist new file mode 100644 index 0000000..8394cc5 --- /dev/null +++ b/vegvisir.json.dist @@ -0,0 +1,33 @@ +{ + "server": { + "address": "127.0.0.1", + "port": 8080 + }, + "backends": { + "news-app": { + "prefixUrl": "news/", + "backendAddress": "http://127.0.0.1:3030", + "routes": [{ + "pattern": "new/(.*)", + "target": "$1" + },{ + "pattern": "new1/(.*)/([0-9]*)", + "target": "new-url1/$1/$2" + },{ + "pattern": "(.+)", + "target": "url" + }] + }, + "article-app": { + "prefixUrl": "art/", + "backendAddress": "http://127.0.0.1:3030", + "routes": [{ + "pattern": "art1/(.*)", + "target": "art-url1/$1" + },{ + "pattern": "([a-zA-Z0-9]*)", + "target": "article-global/$1" + }] + } + } +} \ No newline at end of file