Skip to content

Commit

Permalink
Refactoring based on rk-entry/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dongxuny committed Mar 8, 2022
1 parent 1af8d00 commit 3b47598
Show file tree
Hide file tree
Showing 48 changed files with 1,945 additions and 1,851 deletions.
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: all
all: test lint doctoc fmt
all: test lint fmt

.PHONY: lint
lint:
Expand All @@ -20,12 +20,6 @@ fmt:
@gofmt -s -w . 2>&1
@echo "------------------------------------[Done]"

.PHONY: doctoc
doctoc:
@echo "[doctoc] Running doctoc..."
@doctoc . 2>&1
@echo "------------------------------------[Done]"

.PHONY: gomod
gomod:
@echo "[gomod] Running go mod tidy..."
Expand Down
733 changes: 385 additions & 348 deletions README.md

Large diffs are not rendered by default.

687 changes: 258 additions & 429 deletions boot/fiber_entry.go

Large diffs are not rendered by default.

202 changes: 75 additions & 127 deletions boot/fiber_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,12 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
rkentry "github.com/rookie-ninja/rk-entry/entry"
rkmidmetrics "github.com/rookie-ninja/rk-entry/middleware/metrics"
rkfibermeta "github.com/rookie-ninja/rk-fiber/interceptor/meta"
rkfibermetrics "github.com/rookie-ninja/rk-fiber/interceptor/metrics/prom"
"github.com/rookie-ninja/rk-entry/v2/entry"
"github.com/rookie-ninja/rk-fiber/middleware/meta"
"github.com/stretchr/testify/assert"
"io/ioutil"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path"
"strconv"
"testing"
"time"
Expand All @@ -44,24 +36,24 @@ fiber:
path: "sw"
commonService:
enabled: true
tv:
docs:
enabled: true
prom:
enabled: true
pusher:
enabled: false
interceptors:
loggingZap:
middleware:
logging:
enabled: true
metricsProm:
prom:
enabled: true
auth:
enabled: true
basic:
- "user:pass"
meta:
enabled: true
tracingTelemetry:
trace:
enabled: true
ratelimit:
enabled: true
Expand All @@ -75,8 +67,6 @@ fiber:
enabled: true
csrf:
enabled: true
gzip:
enabled: true
- name: greeter2
port: 2008
enabled: true
Expand All @@ -85,12 +75,12 @@ fiber:
path: "sw"
commonService:
enabled: true
tv:
docs:
enabled: true
interceptors:
loggingZap:
middleware:
logging:
enabled: true
metricsProm:
prom:
enabled: true
auth:
enabled: true
Expand All @@ -107,10 +97,10 @@ func TestGetFiberEntry(t *testing.T) {
assert.Nil(t, GetFiberEntry("entry-name"))

// happy case
echoEntry := RegisterFiberEntry(WithName("ut"))
assert.Equal(t, echoEntry, GetFiberEntry("ut"))
fiberEntry := RegisterFiberEntry(WithName("ut"))
assert.Equal(t, fiberEntry, GetFiberEntry("ut"))

rkentry.GlobalAppCtx.RemoveEntry("ut")
rkentry.GlobalAppCtx.RemoveEntry(fiberEntry)
}

func TestRegisterFiberEntry(t *testing.T) {
Expand All @@ -121,21 +111,40 @@ func TestRegisterFiberEntry(t *testing.T) {
assert.NotEmpty(t, entry.GetType())
assert.NotEmpty(t, entry.GetDescription())
assert.NotEmpty(t, entry.String())
rkentry.GlobalAppCtx.RemoveEntry(entry.GetName())
rkentry.GlobalAppCtx.RemoveEntry(entry)

// with options
commonServiceEntry := rkentry.RegisterCommonServiceEntry(&rkentry.BootCommonService{
Enabled: true,
})
staticEntry := rkentry.RegisterStaticFileHandlerEntry(&rkentry.BootStaticFileHandler{
Enabled: true,
})
certEntry := rkentry.RegisterCertEntry(&rkentry.BootCert{
Cert: []*rkentry.BootCertE{
{
Name: "ut-cert",
},
},
})
swEntry := rkentry.RegisterSWEntry(&rkentry.BootSW{
Enabled: true,
})
promEntry := rkentry.RegisterPromEntry(&rkentry.BootProm{
Enabled: true,
})

entry = RegisterFiberEntry(
WithZapLoggerEntry(nil),
WithEventLoggerEntry(nil),
WithCommonServiceEntry(rkentry.RegisterCommonServiceEntry()),
WithTvEntry(rkentry.RegisterTvEntry()),
WithStaticFileHandlerEntry(rkentry.RegisterStaticFileHandlerEntry()),
WithCertEntry(rkentry.RegisterCertEntry()),
WithSwEntry(rkentry.RegisterSwEntry()),
WithLoggerEntry(rkentry.LoggerEntryNoop),
WithEventEntry(rkentry.EventEntryNoop),
WithCommonServiceEntry(commonServiceEntry),
WithStaticFileHandlerEntry(staticEntry),
WithCertEntry(certEntry[0]),
WithSwEntry(swEntry),
WithPort(8080),
WithName("ut-entry"),
WithDescription("ut-desc"),
WithPromEntry(rkentry.RegisterPromEntry()))
WithPromEntry(promEntry))

assert.NotEmpty(t, entry.GetName())
assert.NotEmpty(t, entry.GetType())
Expand All @@ -145,20 +154,20 @@ func TestRegisterFiberEntry(t *testing.T) {
assert.True(t, entry.IsStaticFileHandlerEnabled())
assert.True(t, entry.IsPromEnabled())
assert.True(t, entry.IsCommonServiceEnabled())
assert.True(t, entry.IsTvEnabled())
assert.True(t, entry.IsTlsEnabled())
assert.False(t, entry.IsDocsEnabled())
assert.False(t, entry.IsTlsEnabled())

bytes, err := entry.MarshalJSON()
assert.NotEmpty(t, bytes)
assert.Nil(t, err)
assert.Nil(t, entry.UnmarshalJSON([]byte{}))
}

func TestFiberEntry_AddInterceptor(t *testing.T) {
func TestFiberEntry_AddMiddleware(t *testing.T) {
defer assertNotPanic(t)
entry := RegisterFiberEntry()
inter := rkfibermeta.Interceptor()
entry.AddInterceptor(inter)
inter := rkfibermeta.Middleware()
entry.AddMiddleware(inter)
}

func TestFiberEntry_Bootstrap(t *testing.T) {
Expand All @@ -168,26 +177,40 @@ func TestFiberEntry_Bootstrap(t *testing.T) {
entry := RegisterFiberEntry(WithPort(8080))
entry.Bootstrap(context.TODO())
validateServerIsUp(t, 8080, entry.IsTlsEnabled())
assert.Empty(t, entry.ListRoutes())

entry.Interrupt(context.TODO())
time.Sleep(time.Second)

// with enable sw, static, prom, common, tv, tls
certEntry := rkentry.RegisterCertEntry()
certEntry.Store.ServerCert, certEntry.Store.ServerKey = generateCerts()
commonServiceEntry := rkentry.RegisterCommonServiceEntry(&rkentry.BootCommonService{
Enabled: true,
})
staticEntry := rkentry.RegisterStaticFileHandlerEntry(&rkentry.BootStaticFileHandler{
Enabled: true,
})
certEntry := rkentry.RegisterCertEntry(&rkentry.BootCert{
Cert: []*rkentry.BootCertE{
{
Name: "ut-cert",
},
},
})
swEntry := rkentry.RegisterSWEntry(&rkentry.BootSW{
Enabled: true,
})
promEntry := rkentry.RegisterPromEntry(&rkentry.BootProm{
Enabled: true,
})

entry = RegisterFiberEntry(
WithPort(8081),
WithCommonServiceEntry(rkentry.RegisterCommonServiceEntry()),
WithTvEntry(rkentry.RegisterTvEntry()),
WithStaticFileHandlerEntry(rkentry.RegisterStaticFileHandlerEntry()),
WithCertEntry(certEntry),
WithSwEntry(rkentry.RegisterSwEntry()),
WithPromEntry(rkentry.RegisterPromEntry()))
WithCommonServiceEntry(commonServiceEntry),
WithStaticFileHandlerEntry(staticEntry),
WithCertEntry(certEntry[0]),
WithSwEntry(swEntry),
WithPromEntry(promEntry))
entry.Bootstrap(context.TODO())
validateServerIsUp(t, 8081, entry.IsTlsEnabled())
assert.NotEmpty(t, entry.ListRoutes())

entry.Interrupt(context.TODO())
time.Sleep(time.Second)
Expand All @@ -198,19 +221,17 @@ func TestFiberEntry_startServer_ServerFail(t *testing.T) {
entry := RegisterFiberEntry(
WithPort(808080))

event := rkentry.NoopEventLoggerEntry().GetEventFactory().CreateEventNoop()
logger := rkentry.NoopZapLoggerEntry().GetLogger()
event := rkentry.EventEntryNoop.CreateEventNoop()
logger := rkentry.LoggerEntryNoop.Logger

entry.startServer(event, logger)
}

func TestRegisterFiberEntriesWithConfig(t *testing.T) {
func TestRegisterFiberEntryYAML(t *testing.T) {
assertNotPanic(t)

// write config file in unit test temp directory
tempDir := path.Join(t.TempDir(), "boot.yaml")
assert.Nil(t, ioutil.WriteFile(tempDir, []byte(defaultBootConfigStr), os.ModePerm))
entries := RegisterFiberEntriesWithConfig(tempDir)
entries := RegisterFiberEntryYAML([]byte(defaultBootConfigStr))
assert.NotNil(t, entries)
assert.Len(t, entries, 2)

Expand All @@ -225,79 +246,6 @@ func TestRegisterFiberEntriesWithConfig(t *testing.T) {
assert.Nil(t, greeter3)
}

func TestFiberEntry_Apis(t *testing.T) {
entry := RegisterFiberEntry()

app := fiber.New()
app.Get("/apis", entry.Apis)
entry.App = app

req := httptest.NewRequest(http.MethodGet, "/apis", nil)
resp, err := app.Test(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestFiberEntry_Req_HappyCase(t *testing.T) {
defer assertNotPanic(t)

entry := RegisterFiberEntry(
WithCommonServiceEntry(rkentry.RegisterCommonServiceEntry()),
WithPort(8082),
WithName("ut"))

entry.AddInterceptor(rkfibermetrics.Interceptor(
rkmidmetrics.WithEntryNameAndType("ut", "Fiber"),
rkmidmetrics.WithRegisterer(prometheus.NewRegistry())))

app := fiber.New()
app.Get("/req", entry.Req)
entry.App = app

entry.Bootstrap(context.TODO())

req := httptest.NewRequest(http.MethodGet, "/req", nil)
resp, err := app.Test(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

entry.Interrupt(context.TODO())
time.Sleep(time.Second)
}

func TestFiberEntry_TV(t *testing.T) {
defer assertNotPanic(t)

entry := RegisterFiberEntry(
WithCommonServiceEntry(rkentry.RegisterCommonServiceEntry()),
WithTvEntry(rkentry.RegisterTvEntry()),
WithPort(8083),
WithName("ut"))

entry.AddInterceptor(rkfibermetrics.Interceptor(
rkmidmetrics.WithEntryNameAndType("ut", "Echo")))

app := fiber.New()
app.Get("/ut/*", entry.TV)
entry.App = app

entry.Bootstrap(context.TODO())

req := httptest.NewRequest(http.MethodGet, "/ut/apis", nil)
resp, err := app.Test(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

// for default
req = httptest.NewRequest(http.MethodGet, "/ut/other", nil)
resp, err = app.Test(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

entry.Interrupt(context.TODO())
time.Sleep(time.Second)
}

func generateCerts() ([]byte, []byte) {
// Create certs and return as []byte
ca := &x509.Certificate{
Expand Down
Loading

0 comments on commit 3b47598

Please sign in to comment.