Skip to content

Commit

Permalink
Merge pull request #67 from mageddo/feature/48
Browse files Browse the repository at this point in the history
Feature/48
  • Loading branch information
mageddo authored May 6, 2018
2 parents 429bee8 + 4559ede commit 8dd6004
Show file tree
Hide file tree
Showing 60 changed files with 809 additions and 2,298 deletions.
3 changes: 3 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.5.0
* Migrate to static logging tool

### 2.4.1
* Service restart command was with typo

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.1
2.5.0
3 changes: 1 addition & 2 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/mageddo/dns-proxy-server/flags"
"os"
"github.com/mageddo/dns-proxy-server/utils/env"
"github.com/mageddo/go-logging"
"strings"
)

Expand Down Expand Up @@ -47,7 +46,7 @@ func GetResolvConf() string {
}

func getConf() (*local.LocalConfiguration, error) {
return local.LoadConfiguration(logging.NewContext())
return local.LoadConfiguration()
}

func LogLevel() string {
Expand Down
14 changes: 5 additions & 9 deletions conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/mageddo/dns-proxy-server/flags"
"github.com/mageddo/dns-proxy-server/events/local"
"github.com/mageddo/go-logging"
"github.com/mageddo/dns-proxy-server/utils"
"flag"
"github.com/mageddo/dns-proxy-server/utils/env"
Expand All @@ -29,8 +28,7 @@ func TestFlagValuesFromArgs(t *testing.T) {
func TestFlagValuesFromConf(t *testing.T) {

defer local.ResetConf()
ctx := logging.NewContext()
local.LoadConfiguration(ctx)
local.LoadConfiguration()

err := utils.WriteToFile(`{ "webServerPort": 8080, "dnsServerPort": 62, "defaultDns": false }`, utils.GetPath(*flags.ConfPath))
assert.Nil(t, err)
Expand All @@ -48,11 +46,10 @@ func TestLogLevel_DefaultValue(t *testing.T) {
func TestLogLevel_ReadFromConfig(t *testing.T) {

// arrange
ctx := logging.NewContext()
c, err := local.LoadConfiguration(ctx)
c, err := local.LoadConfiguration()
assert.Nil(t, err)
c.LogLevel = "INFO"
local.SaveConfiguration(ctx, c)
local.SaveConfiguration(c)

// act
level := LogLevel()
Expand Down Expand Up @@ -90,11 +87,10 @@ func TestLogFile_DefaultValue(t *testing.T) {
func TestLogFile_ReadFromConfig(t *testing.T) {

// arrange
ctx := logging.NewContext()
c, err := local.LoadConfiguration(ctx)
c, err := local.LoadConfiguration()
assert.Nil(t, err)
c.LogFile = "false"
local.SaveConfiguration(ctx, c)
local.SaveConfiguration(c)

// act
level := LogFile()
Expand Down
15 changes: 7 additions & 8 deletions controller/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"github.com/mageddo/dns-proxy-server/utils"
"github.com/mageddo/dns-proxy-server/cache/store"
"github.com/mageddo/go-logging"
. "github.com/mageddo/go-httpmap"
)
const (
CACHE_V1 = "/v1/caches"
CACHE_SIZE_V1 = "/v1/caches/size"
)
func init() {

Get(CACHE_V1, func(ctx context.Context, res http.ResponseWriter, req *http.Request, url string) {
Get(CACHE_V1, func(ctx context.Context, res http.ResponseWriter, req *http.Request) {

logger := logging.NewLog(ctx)
c, encoder := store.GetInstance(), utils.GetJsonEncoder(res)
logger.Debugf("m=%s, size=%d", CACHE_V1, c.Size())
logging.Debugf("m=%s, size=%d", CACHE_V1, c.Size())
res.Header().Add("Content-Type", "application/json")

cacheObject := make(map[string]interface{})
Expand All @@ -26,20 +26,19 @@ func init() {
}

if err := encoder.Encode(cacheObject); err != nil {
logger.Errorf("m=%s, err=%v", CACHE_V1, err)
logging.Errorf("m=%s, err=%v", CACHE_V1, err)
RespMessage(res, http.StatusServiceUnavailable, "Could not get caches, please try again later")
}
})

Get(CACHE_SIZE_V1, func(ctx context.Context, res http.ResponseWriter, req *http.Request, url string) {
Get(CACHE_SIZE_V1, func(ctx context.Context, res http.ResponseWriter, req *http.Request) {

logger := logging.NewLog(ctx)
c, encoder := store.GetInstance(), utils.GetJsonEncoder(res)
logger.Debugf("m=%s, size=%d", CACHE_SIZE_V1, c.Size())
logging.Debugf("m=%s, size=%d", CACHE_SIZE_V1, c.Size())
res.Header().Add("Content-Type", "application/json")

if err := encoder.Encode(map[string]interface{}{"size": c.Size()}); err != nil {
logger.Errorf("m=%s, err=%v", CACHE_SIZE_V1, err)
logging.Errorf("m=%s, err=%v", CACHE_SIZE_V1, err)
RespMessage(res, http.StatusServiceUnavailable, "Temporary unavailable, please try again later")
}
})
Expand Down
54 changes: 26 additions & 28 deletions controller/env.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package controller

import (
log "github.com/mageddo/go-logging"
"net/http"
"github.com/mageddo/dns-proxy-server/events/local"
"encoding/json"
"context"
"github.com/mageddo/dns-proxy-server/utils"
. "github.com/mageddo/go-httpmap"
"github.com/mageddo/go-logging"
)

const (
Expand All @@ -18,80 +19,77 @@ const (
func init(){


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

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

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

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

if conf, _ := local.LoadConfiguration(ctx); conf != nil {
if err := conf.SetActiveEnv(ctx, envVo); err != nil {
logger.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
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
}
logger.Infof("m=/env/active/, status=success, action=active-env")
logging.Infof("m=/env/active/, status=success, action=active-env")
return
}
confLoadError(res)

})

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

Post(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request, url string){
logger := log.NewLog(ctx)
Post(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
logger.Infof("m=/env/, status=begin, action=create-env")
logging.Infof("m=/env/, status=begin, action=create-env")
var envVo local.EnvVo
json.NewDecoder(req.Body).Decode(&envVo)
logger.Infof("m=/env/, status=parsed-host, env=%+v", envVo)
if conf, _ := local.LoadConfiguration(ctx); conf != nil {
logging.Infof("m=/env/, status=parsed-host, env=%+v", envVo)
if conf, _ := local.LoadConfiguration(); conf != nil {
if err := conf.AddEnv(ctx, envVo); err != nil {
logger.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
logging.Infof("m=/env/, status=error, action=create-env, err=%+v", err)
BadRequest(res, err.Error())
return
}
logger.Infof("m=/env/, status=success, action=create-env")
logging.Infof("m=/env/, status=success, action=create-env")
return
}
confLoadError(res)
})

Delete(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request, url string){
logger := log.NewLog(ctx)
Delete(ENV, func(ctx context.Context, res http.ResponseWriter, req *http.Request){
res.Header().Add("Content-Type", "application/json")
logger.Infof("m=/env/, status=begin, action=delete-env")
logging.Infof("m=/env/, status=begin, action=delete-env")
var env local.EnvVo
json.NewDecoder(req.Body).Decode(&env)
logger.Infof("m=/env/, status=parsed-host, action=delete-env, env=%+v", env)
if conf, _ := local.LoadConfiguration(ctx); conf != nil {
if err := conf.RemoveEnvByName(ctx, env.Name); err != nil {
logger.Infof("m=/env/, status=error, action=delete-env, err=%+v", err)
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 {
logging.Infof("m=/env/, status=error, action=delete-env, err=%+v", err)
BadRequest(res, err.Error())
return
}
logger.Infof("m=/env/, status=success, action=delete-env")
logging.Infof("m=/env/, status=success, action=delete-env")
return
}
confLoadError(res)
Expand Down
14 changes: 3 additions & 11 deletions controller/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"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/go-logging"
)

func TestGetActiveEnvSuccess(t *testing.T) {
Expand Down Expand Up @@ -42,8 +41,7 @@ func TestPutChangeActiveEnvSuccess(t *testing.T) {

defer local.ResetConf()

ctx := logging.NewContext()
local.LoadConfiguration(ctx)
local.LoadConfiguration()

err := utils.WriteToFile(`{
"remoteDnsServers": [], "envs": [{ "name": "testEnv" }]
Expand Down Expand Up @@ -72,12 +70,8 @@ func TestPutChangeActiveEnvSuccess(t *testing.T) {
func TestGetEnvsSuccess(t *testing.T) {

defer local.ResetConf()

ctx := logging.NewContext()
local.LoadConfiguration(ctx)

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

assert.Nil(t, err)

s := httptest.NewServer(nil)
Expand Down Expand Up @@ -120,9 +114,7 @@ func TestPostEnvSuccess(t *testing.T) {
func TestDeleteEnvSuccess(t *testing.T) {

defer local.ResetConf()

ctx := logging.NewContext()
local.LoadConfiguration(ctx)
local.LoadConfiguration()

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

Expand Down
Loading

0 comments on commit 8dd6004

Please sign in to comment.