Skip to content

Commit

Permalink
Refactoring Storage API (#122)
Browse files Browse the repository at this point in the history
* Refactoring storage api
* Setup build on pull request
* fixing docs typo
  • Loading branch information
mageddo committed Apr 22, 2019
1 parent d101324 commit 5585df2
Show file tree
Hide file tree
Showing 33 changed files with 1,086 additions and 486 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
sudo: required

language: c
services:
- docker

script: ./builder.bash deploy-ci
script:
- 'if [ "$TRAVIS_BRANCH" = "master" ]; then ./builder.bash deploy-ci; fi'
- 'if [ "$TRAVIS_BRANCH" != "master" ]; then docker-compose rm -f && docker-compose up --build tests; fi'

branches:
only:
- master
- /^(feature|bugfix).*$/
3 changes: 3 additions & 0 deletions Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ RUN npm install &&\
rm -f `find ./build -name *.map`

FROM golang:1.11 AS GOLANG
ENV GOPATH=/app
ENV MG_WORK_DIR=/app/src/github.com/mageddo/dns-proxy-server
WORKDIR /app/src/github.com/mageddo/dns-proxy-server
COPY --from=BUILDER /app/build /static
COPY ./builder.bash /bin/builder.bash
6 changes: 6 additions & 0 deletions Dockerfile.go.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.11 AS GOLANG
ENV GOPATH=/app
ENV MG_WORK_DIR=/app/src/github.com/mageddo/dns-proxy-server
WORKDIR /app/src/github.com/mageddo/dns-proxy-server
COPY ./builder.bash /bin/builder.bash
RUN mkdir /static
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 2.14.0
* Making some refactoring facilitating to the feature requested at #121
* Fixing nil pointer sometimes when the hostname were not found

### 2.13.2
* Fixing broken answer when hostname is not found
* Fixing ping slowness
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.13.2
2.14.0
4 changes: 4 additions & 0 deletions builder.bash
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ case $1 in

;;

assemble )
assemble
;;

build )

assemble
Expand Down
7 changes: 4 additions & 3 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package conf
import (
"fmt"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/dns-proxy-server/events/local/localvo"
"github.com/mageddo/dns-proxy-server/flags"
"os"
"github.com/mageddo/dns-proxy-server/utils/env"
"strings"
"github.com/mageddo/go-logging"
"os"
"strings"
)

func CpuProfile() string {
Expand Down Expand Up @@ -47,7 +48,7 @@ func GetResolvConf() string {
return GetString(os.Getenv(env.MG_RESOLVCONF), "/etc/resolv.conf")
}

func getConf() (*local.LocalConfiguration, error) {
func getConf() (*localvo.Configuration, error) {
return local.LoadConfiguration()
}

Expand Down
9 changes: 6 additions & 3 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ func TestFlagValuesFromArgs(t *testing.T) {

func TestFlagValuesFromConf(t *testing.T) {

defer local.ResetConf()
local.LoadConfiguration()
// arrange
local.ResetConf()

// act
err := utils.WriteToFile(`{ "webServerPort": 8080, "dnsServerPort": 62, "defaultDns": false }`, utils.GetPath(*flags.ConfPath))
assert.Nil(t, err)

// assert
assert.Nil(t, err)
assert.Equal(t, 8080, WebServerPort())
assert.Equal(t, 62, DnsServerPort())
assert.Equal(t, false, SetupResolvConf())
Expand All @@ -47,6 +49,7 @@ func TestLogLevel_DefaultValue(t *testing.T) {
func TestLogLevel_ReadFromConfig(t *testing.T) {

// arrange
local.ResetConf()
c, err := local.LoadConfiguration()
assert.Nil(t, err)
c.LogLevel = "INFO"
Expand Down
2 changes: 1 addition & 1 deletion controller/cache.go → controller/v1/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package v1

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion controller/cache_test.go → controller/v1/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package v1

import (
"testing"
Expand Down
74 changes: 35 additions & 39 deletions controller/env.go → controller/v1/env.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package controller
package v1

import (
"net/http"
"github.com/mageddo/dns-proxy-server/events/local"
"encoding/json"
"context"
"encoding/json"
"github.com/mageddo/dns-proxy-server/controller/v1/vo"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/dns-proxy-server/pkg/mageddo/uuid"
"github.com/mageddo/dns-proxy-server/reference"
"github.com/mageddo/dns-proxy-server/utils"
. "github.com/mageddo/go-httpmap"
"github.com/mageddo/go-logging"
"net/http"
"strconv"
"time"
)

const (
Expand All @@ -18,84 +23,75 @@ const (

func init(){


Get(ENV_ACTIVE, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
if conf, _ := local.LoadConfiguration(); conf != nil {
utils.GetJsonEncoder(res).Encode(local.EnvVo{Name: conf.ActiveEnv})
return
if conf, err := local.LoadConfiguration(); conf != nil {
utils.GetJsonEncoder(res).Encode(vo.EnvV1{Name: conf.ActiveEnv})
} else {
confLoadError(res, err)
}
confLoadError(res)
})

Put(ENV_ACTIVE, func(ctx context.Context, res http.ResponseWriter, req *http.Request){

logging.Infof("m=/env/active/, status=begin")
res.Header().Add("Content-Type", "application/json")

var envVo local.EnvVo
var envVo vo.EnvV1
json.NewDecoder(req.Body).Decode(&envVo)
logging.Infof("m=/env/active/, status=parsed-host, env=%+v", envVo)

if conf, _ := local.LoadConfiguration(); conf != nil {
if err := conf.SetActiveEnv(envVo); err != nil {
logging.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
BadRequest(res, err.Error())
return
}
if err := local.SetActiveEnv(envVo.ToEnv()); err == nil {
logging.Infof("m=/env/active/, status=success, action=active-env")
return
} else {
logging.Infof("m=/env/active, status=error, action=create-env, err=%+v", err)
BadRequest(res, err.Error())
}
confLoadError(res)

})

Get(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
if conf, _ := local.LoadConfiguration(); conf != nil {
utils.GetJsonEncoder(res).Encode(conf.Envs)
if conf, err := local.LoadConfiguration(); conf != nil {
utils.GetJsonEncoder(res).Encode(vo.FromEnvs(conf.Envs))
return
} else {
confLoadError(res, err)
}
confLoadError(res)
})

Post(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
logging.Infof("m=/env/, status=begin, action=create-env")
var envVo local.EnvVo
var envVo vo.EnvV1
json.NewDecoder(req.Body).Decode(&envVo)
logging.Infof("m=/env/, status=parsed-host, env=%+v", envVo)
if conf, _ := local.LoadConfiguration(); conf != nil {
if err := conf.AddEnv(ctx, envVo); err != nil {
logging.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
BadRequest(res, err.Error())
return
}
for i := range envVo.Hostnames {
envVo.Hostnames[i].Id = strconv.FormatInt(time.Now().UnixNano(), 10)
}
if err := local.AddEnv(ctx, envVo.ToEnv()); err == nil {
logging.Infof("m=/env/, status=success, action=create-env")
return
} else {
logging.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
BadRequest(res, err.Error())
}
confLoadError(res)
})

Delete(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
logging.Infof("m=/env/, status=begin, action=delete-env")
var env local.EnvVo
var env vo.EnvV1
json.NewDecoder(req.Body).Decode(&env)
logging.Infof("m=/env/, status=parsed-host, action=delete-env, env=%+v", env)
if conf, _ := local.LoadConfiguration(); conf != nil {
if err := conf.RemoveEnvByName(env.Name); err != nil {
if err := local.RemoveEnvByName(context.WithValue(ctx, reference.UUID, uuid.UUID()), env.Name); err != nil {
logging.Infof("m=/env/, status=error, action=delete-env, err=%+v", err)
BadRequest(res, err.Error())
return
}
} else {
logging.Infof("m=/env/, status=success, action=delete-env")
return
}
confLoadError(res)
})
}

func confLoadError(res http.ResponseWriter){
func confLoadError(res http.ResponseWriter, err error){
logging.Errorf("could-not-load-conf, err=%+v", err, err)
BadRequest(res, "Could not load conf")
}
59 changes: 41 additions & 18 deletions controller/env_test.go → controller/v1/env_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package controller
package v1

import (
"testing"
"net/http/httptest"
"github.com/go-resty/resty"
"github.com/stretchr/testify/assert"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/dns-proxy-server/utils"
"github.com/mageddo/dns-proxy-server/flags"
"github.com/mageddo/dns-proxy-server/utils"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"testing"
)

func TestGetActiveEnvSuccess(t *testing.T) {
// arrange

local.ResetConf()
s := httptest.NewServer(nil)
defer s.Close()

// act
r, err := resty.R().Get(s.URL + ENV_ACTIVE)

// assert
assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode())
assert.Equal(t, `{"name":""}`, r.String())
Expand All @@ -39,23 +44,25 @@ func TestPutChangeActiveEnvThatDoesNotExistsError(t *testing.T) {

func TestPutChangeActiveEnvSuccess(t *testing.T) {

defer local.ResetConf()

local.LoadConfiguration()
// arrange
local.ResetConf()

err := utils.WriteToFile(`{
"remoteDnsServers": [], "envs": [{ "name": "testEnv" }]
}`, utils.GetPath(*flags.ConfPath))

assert.Nil(t, err)


s := httptest.NewServer(nil)
defer s.Close()

// act

r, err := resty.R().
SetBody(`{"name": "testEnv"}`).
Put(s.URL + ENV_ACTIVE)

// assert
assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode())
assert.Empty(t, r.String())
Expand All @@ -69,15 +76,19 @@ func TestPutChangeActiveEnvSuccess(t *testing.T) {

func TestGetEnvsSuccess(t *testing.T) {

defer local.ResetConf()
local.LoadConfiguration()
// arrange
local.ResetConf()

err := utils.WriteToFile(`{ "remoteDnsServers": [], "envs": [{ "name": "SecondEnv" }]}`, utils.GetPath(*flags.ConfPath))
assert.Nil(t, err)

// act
s := httptest.NewServer(nil)
defer s.Close()

r, err := resty.R().Get(s.URL + ENV)


// assert
assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode())
assert.Equal(t, `[{"name":"SecondEnv"}]`, r.String())
Expand All @@ -86,7 +97,7 @@ func TestGetEnvsSuccess(t *testing.T) {

func TestPostEnvSuccess(t *testing.T) {

defer local.ResetConf()
local.ResetConf()

s := httptest.NewServer(nil)
defer s.Close()
Expand All @@ -104,17 +115,22 @@ func TestPostEnvSuccess(t *testing.T) {
r, err = resty.R().Get(s.URL + ENV)
assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode())
assert.Equal(t,
`[{"name":""},{"name":"ThirdEnv","hostnames":[{"id":1,"hostname":"github.com","ip":[1,2,3,4],"target":"","ttl":30,"type":"A"}]}]`,
assert.Equal(
t,
utils.Replace(
`[{"name":""},{"name":"ThirdEnv","hostnames":[{"id":"$1","hostname":"github.com","ip":[1,2,3,4],"target":"","ttl":30,"type":"A","env":"ThirdEnv"}]}]`,
r.String(),
`"id":"(\d+)"`,
),
r.String(),
)
}


func TestDeleteEnvSuccess(t *testing.T) {

defer local.ResetConf()
local.LoadConfiguration()
// arrange
local.ResetConf()

err := utils.WriteToFile(`{ "remoteDnsServers": [], "envs": [{ "name": "SecondEnv" }]}`, utils.GetPath(*flags.ConfPath))

Expand All @@ -126,7 +142,14 @@ func TestDeleteEnvSuccess(t *testing.T) {
assert.Equal(t, 200, r.StatusCode())
assert.Equal(t, `[{"name":"SecondEnv"}]`, r.String())

r, err = resty.R().SetBody(`{"name": "SecondEnv"}`).Delete(s.URL + ENV)
// act
r, err = resty.R().
SetBody(`{"name": "SecondEnv"}`).
Delete(s.URL + ENV)


// assert

assert.Nil(t, err)
assert.Equal(t, 200, r.StatusCode())
assert.Empty(t, r.String())
Expand Down
Loading

0 comments on commit 5585df2

Please sign in to comment.