Skip to content

Commit

Permalink
added necessary info logs
Browse files Browse the repository at this point in the history
  • Loading branch information
john-deng committed Oct 30, 2018
1 parent 74fb530 commit 969d77b
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -21,3 +21,4 @@ application-fake.yml
vendor/

.vscode/
!/pkg/app/web/application.go
Expand Up @@ -18,7 +18,7 @@ func TestHelloClient(t *testing.T) {
mockHelloClient := mock.NewMockHelloServiceClient(ctrl)
app.Register("protobuf.helloServiceClient", mockHelloClient)

testApp := web.NewTestApplication(t, newHelloController)
testApp := web.RunTestApplication(t, newHelloController)

req := &protobuf.HelloRequest{Name: "Steve"}

Expand Down
Expand Up @@ -18,7 +18,7 @@ func TestHolaClient(t *testing.T) {
mockHolaClient := mock.NewMockHolaServiceClient(ctrl)
app.Register("protobuf.holaServiceClient", mockHolaClient)

testApp := web.NewTestApplication(t, newHolaController)
testApp := web.RunTestApplication(t, newHolaController)

req := &protobuf.HolaRequest{Name: "Steve"}

Expand Down
2 changes: 1 addition & 1 deletion examples/web/helloworld/main.go
Expand Up @@ -40,5 +40,5 @@ func (c *Controller) Get() string {
// main function
func main() {
// create new web application and run it
web.NewApplication(new(Controller)).Run()
web.NewApplication(new(Controller)).SetProperty("app.name", "hello-world").Run()
}
2 changes: 1 addition & 1 deletion examples/web/helloworld/main_test.go
Expand Up @@ -28,7 +28,7 @@ func TestRunMain(t *testing.T) {

func TestController(t *testing.T) {
time.Sleep(time.Second)
web.NewTestApplication(t, new(Controller)).
web.RunTestApplication(t, new(Controller)).
Get("/").
Expect().Status(http.StatusOK)
}
2 changes: 1 addition & 1 deletion examples/web/jwt/controller/bar_test.go
Expand Up @@ -27,7 +27,7 @@ import (
)

func TestBarWithToken(t *testing.T) {
app := web.NewTestApplication(t, newBarController)
app := web.RunTestApplication(t, newBarController)
log.Println(io.GetWorkDir())
jwtToken := jwt.NewJwtToken(&jwt.Properties{
PrivateKeyPath: "config/ssl/app.rsa",
Expand Down
2 changes: 1 addition & 1 deletion examples/web/jwt/controller/foo_test.go
Expand Up @@ -22,7 +22,7 @@ import (
)

func GetTestApplication(t *testing.T) web.TestApplication {
return web.NewTestApplication(t, newFooController)
return web.RunTestApplication(t, newFooController)
}

func TestFooGet(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion examples/web/jwt/controller/login_test.go
Expand Up @@ -8,7 +8,7 @@ import (

func TestFooLogin(t *testing.T) {

testApp := web.NewTestApplication(t, newLoginController)
testApp := web.RunTestApplication(t, newLoginController)

testApp.Post("/login").
WithJSON(userRequest{Username: "mike", Password: "daDg83t"}).
Expand Down
5 changes: 1 addition & 4 deletions pkg/app/cli/application.go
Expand Up @@ -17,7 +17,6 @@ package cli
import (
"github.com/hidevopsio/hiboot/pkg/app"
"github.com/hidevopsio/hiboot/pkg/log"
"github.com/hidevopsio/hiboot/pkg/utils/gotest"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -85,9 +84,7 @@ func (a *application) build() error {
root = r.(Command)
Register(root)
a.root = root
if !gotest.IsRunning() {
root.EmbeddedCommand().Use = basename
}
root.EmbeddedCommand().Use = basename
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/cli/application_test.go
Expand Up @@ -179,7 +179,7 @@ func TestCliApplication(t *testing.T) {
}

func TestNewApplication(t *testing.T) {
go cli.NewApplication().Run()
go cli.NewApplication().SetProperty("app.project", "cli-test-app").Run()
time.Sleep(2 * time.Second)
}

Expand Down
30 changes: 23 additions & 7 deletions pkg/app/web/application.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/kataras/iris/context"
"os"
"regexp"
"time"
)

const (
Expand All @@ -37,16 +38,27 @@ const (
applicationContext = "app.applicationContext"
)

type webApp struct {
*iris.Application
}

func newWebApplication() *webApp {
return &webApp{
Application: iris.New(),
}
}

// Application is the struct of web Application
type application struct {
app.BaseApplication
webApp *iris.Application
webApp *webApp
jwtEnabled bool
//anonControllers []interface{}
//jwtControllers []interface{}
controllers []interface{}
dispatcher dispatcher
//controllerMap map[string][]interface{}
startUpTime time.Time
}

var (
Expand Down Expand Up @@ -75,13 +87,15 @@ func (a *application) Initialize() error {
// Run run web application
func (a *application) Run() (err error) {
serverPort := ":8080"
err = a.build()
conf := a.SystemConfig()
if conf != nil && conf.Server.Port != "" {
serverPort = fmt.Sprintf(":%v", conf.Server.Port)
}

err = a.build()
if err == nil {
log.Infof("Hiboot started on port(s) http://localhost%v", serverPort)
timeDiff := time.Since(a.startUpTime)
log.Infof("Started %v in %f seconds", conf.App.Name, timeDiff.Seconds())
err = a.webApp.Run(iris.Addr(fmt.Sprintf(serverPort)), iris.WithConfiguration(defaultConfiguration()))
}

Expand All @@ -90,6 +104,7 @@ func (a *application) Run() (err error) {

// Init init web application
func (a *application) build() (err error) {

a.Build()

// set custom properties
Expand All @@ -105,11 +120,11 @@ func (a *application) build() (err error) {
log.Infof("Working directory: %v", a.WorkDir)
log.Infof("The following profiles are active: %v, %v", systemConfig.App.Profiles.Active, systemConfig.App.Profiles.Include)
}

log.Infof("Initializing Hiboot Application")
f := a.ConfigurableFactory()
f.AppendComponent(app.ApplicationContextName, a)
f.SetInstance(app.ApplicationContextName, a)
f.AppendComponent(iris.Application{}, a.webApp)
f.AppendComponent(webApp{}, a.webApp)

// fill controllers into component container
for _, ctrl := range a.controllers {
Expand All @@ -134,7 +149,7 @@ func (a *application) RegisterController(controller interface{}) error {
controllerInterfaceName := reflector.GetName(controller)
controllers := a.ConfigurableFactory().GetInstances(controllerInterfaceName)
if controllers != nil {
return a.dispatcher.register(a.webApp, controllers)
return a.dispatcher.register(a.webApp.Application, controllers)
}
return nil
}
Expand All @@ -151,7 +166,7 @@ func (a *application) initialize(controllers ...interface{}) (err error) {
io.EnsureWorkDir(3, "config/application.yml")

// new iris app
a.webApp = iris.New()
a.webApp = newWebApplication()

err = a.Initialize()

Expand All @@ -173,6 +188,7 @@ var RestController = app.Register
func NewApplication(controllers ...interface{}) app.Application {
log.SetLevel("error") // set debug level to error first
a := new(application)
a.startUpTime = time.Now()
err := a.initialize(controllers...)
if err != nil {
os.Exit(1)
Expand Down
24 changes: 14 additions & 10 deletions pkg/app/web/application_test.go
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/hidevopsio/hiboot/pkg/utils/reflector"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"testing"
"time"
)
Expand Down Expand Up @@ -342,8 +341,9 @@ func (c *HelloViewController) Get() {
}

func TestWebViewApplicationWithProperties(t *testing.T) {
os.Args = append(os.Args, "--web.view.enabled=true")
testApp := web.NewTestApp(t, newHelloViewController).SetProperty("").Run(t)
testApp := web.NewTestApp(newHelloViewController).
SetProperty("web.view.enabled", true).
Run(t)
t.Run("should response 200 when GET /", func(t *testing.T) {
testApp.
Get("/").
Expand All @@ -352,8 +352,10 @@ func TestWebViewApplicationWithProperties(t *testing.T) {
}

func TestWebViewApplicationWithArgs(t *testing.T) {
os.Args = append(os.Args, "--web.view.enabled=true")
testApp := web.NewTestApplication(t, newHelloViewController)
testApp := web.NewTestApp(newHelloViewController).
SetProperty("server.port", 8080).
SetProperty("web.view.enabled", true).
Run(t)
t.Run("should response 200 when GET /", func(t *testing.T) {
testApp.
Get("/").
Expand All @@ -362,7 +364,7 @@ func TestWebViewApplicationWithArgs(t *testing.T) {
}

func TestWebApplication(t *testing.T) {
testApp := web.NewTestApplication(t, newHelloController, newFooController, newBarController, newFoobarController)
testApp := web.RunTestApplication(t, newHelloController, newFooController, newBarController, newFoobarController)

t.Run("should response 200 when GET /hello/all", func(t *testing.T) {
testApp.
Expand Down Expand Up @@ -609,7 +611,9 @@ func TestNewApplication(t *testing.T) {

app.Register(new(ExampleController))

testApp := web.NewApplication()
testApp := web.NewApplication().
SetProperty("server.port", 8080).
SetProperty(app.PropertyBannerDisabled, true)
t.Run("should init web application", func(t *testing.T) {
assert.NotEqual(t, nil, testApp)
})
Expand All @@ -625,18 +629,18 @@ func TestNewApplication(t *testing.T) {
assert.Equal(t, "myInterface", typ.Name())
})

go testApp.SetProperty(app.PropertyBannerDisabled, true).Run()
go testApp.Run()
time.Sleep(time.Second)
}

func TestAnonymousController(t *testing.T) {
t.Run("should failed to register anonymous controller", func(t *testing.T) {
testApp := web.NewTestApplication(t, (*Bar)(nil))
testApp := web.RunTestApplication(t, (*Bar)(nil))
assert.NotEqual(t, nil, testApp)
})

t.Run("should failed to register anonymous controller", func(t *testing.T) {
testApp := web.NewTestApplication(t, newBar)
testApp := web.RunTestApplication(t, newBar)
assert.NotEqual(t, nil, testApp)
})
}
2 changes: 1 addition & 1 deletion pkg/app/web/autoconfigure.go
Expand Up @@ -25,7 +25,7 @@ func init() {
}

// Context is the instance of context.Context
func (c *configuration) Context(app *iris.Application) context.Context {
func (c *configuration) Context(app *webApp) context.Context {
ctx := &Context{
Context: irsctx.NewContext(app),
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/web/defaultconfig.go
Expand Up @@ -5,7 +5,7 @@ import "github.com/kataras/iris"
// DefaultConfiguration returns the default configuration for an iris station, fills the main Configuration
func defaultConfiguration() iris.Configuration {
return iris.Configuration{
DisableStartupLog: false,
DisableStartupLog: true,
DisableInterruptHandler: false,
DisableVersionChecker: true,
DisablePathCorrection: false,
Expand Down
3 changes: 3 additions & 0 deletions pkg/app/web/handler.go
Expand Up @@ -151,6 +151,9 @@ func (h *handler) parse(method reflect.Method, object interface{}, path string)
h.responses[i].typeName = typ.Name()
//log.Debug(h.responses[i])
}
if path != "" {
log.Infof("Mapped \"%v\" onto %v.%v()", path, idv.Type(), method.Name)
}
}

func (h *handler) responseData(ctx *Context, numOut int, results []reflect.Value) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/app/web/testapplication.go
Expand Up @@ -45,7 +45,7 @@ type testApplication struct {
}

// RunTestApplication returns the new test application
func NewTestApplication(t *testing.T, controllers ...interface{}) TestApplication {
func RunTestApplication(t *testing.T, controllers ...interface{}) TestApplication {
log.SetLevel(log.DebugLevel)
a := new(testApplication)
err := a.initialize(controllers...)
Expand All @@ -54,7 +54,11 @@ func NewTestApplication(t *testing.T, controllers ...interface{}) TestApplicatio
return a
}

// NewTestApplicationEx returns the new test application
// NewTestApplication is the alias of RunTestApplication
// Deprecated, you should use RunTestApplication instead
var NewTestApplication = RunTestApplication

// NewTestApp returns the new test application
func NewTestApp(controllers ...interface{}) TestApplication {
log.SetLevel(log.DebugLevel)
a := new(testApplication)
Expand All @@ -72,7 +76,7 @@ func (a *testApplication) SetProperty(name string, value ...interface{}) TestApp
func (a *testApplication) Run(t *testing.T) TestApplication {
err := a.build()
assert.Equal(t, nil, err)
a.expect = httptest.New(t, a.webApp)
a.expect = httptest.New(t, a.webApp.Application)
return a
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/factory/instantiate/instantiate.go
Expand Up @@ -68,7 +68,9 @@ func (f *instantiateFactory) AppendComponent(c ...interface{}) {
func (f *instantiateFactory) BuildComponents() (err error) {
// first resolve the dependency graph
var resolved []*factory.MetaData
log.Infof("Resolving dependencies")
resolved, err = depends.Resolve(f.components)
log.Infof("Injecting dependencies")
// then build components
var obj interface{}
var name string
Expand Down Expand Up @@ -106,6 +108,9 @@ func (f *instantiateFactory) BuildComponents() (err error) {
}
}
}
if err == nil {
log.Infof("Injected dependencies")
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/starter/actuator/health_test.go
Expand Up @@ -21,7 +21,7 @@ import (
)

func TestHealthController(t *testing.T) {
web.NewTestApplication(t).
web.RunTestApplication(t).
Get("/health").
Expect().Status(http.StatusOK)
}
2 changes: 1 addition & 1 deletion pkg/starter/grpc/autoconfigure_test.go
Expand Up @@ -66,7 +66,7 @@ func TestGrpcServerAndClient(t *testing.T) {
grpc.Server(helloworld.RegisterGreeterServer, newGreeterServerService)
grpc.Client("greeter-service", helloworld.NewGreeterClient)

testApp := web.NewTestApplication(t)
testApp := web.RunTestApplication(t)
assert.NotEqual(t, nil, testApp)

applicationContext := testApp.(app.ApplicationContext)
Expand Down
4 changes: 2 additions & 2 deletions pkg/starter/jwt/controller_test.go
Expand Up @@ -106,7 +106,7 @@ func (c *barController) Options() {
}

func TestJwtController(t *testing.T) {
testApp := web.NewTestApplication(t, newFooController, newBarController)
testApp := web.RunTestApplication(t, newFooController, newBarController)
ctx := testApp.(app.ApplicationContext)

fc := ctx.GetInstance(fooController{})
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestJwtController(t *testing.T) {

func TestAppWithoutJwtController(t *testing.T) {
fooCtrl := new(fooController)
testApp := web.NewTestApplication(t, fooCtrl)
testApp := web.RunTestApplication(t, fooCtrl)
t.Run("should return http.StatusUnauthorized after GET /bar", func(t *testing.T) {
testApp.Get("/foo").
Expect().Status(http.StatusOK)
Expand Down

0 comments on commit 969d77b

Please sign in to comment.