Skip to content

Commit

Permalink
Merge pull request #106 for v0.8 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Sep 1, 2017
2 parents 34d34a8 + e183431 commit 1241499
Show file tree
Hide file tree
Showing 36 changed files with 1,166 additions and 393 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ branches:

go:
- 1.8
- 1.8.x
- 1.9
- tip

go_import_path: aahframework.org/aah.v0
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# aah web framework for Go
[![Build Status](https://travis-ci.org/go-aah/aah.svg?branch=master)](https://travis-ci.org/go-aah/aah) [![codecov](https://codecov.io/gh/go-aah/aah/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/aah/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/aah.v0)](https://goreportcard.com/report/aahframework.org/aah.v0)
[![Powered by Go](https://img.shields.io/badge/powered_by-go-blue.svg)](https://golang.org)
[![Version](https://img.shields.io/badge/version-0.7-blue.svg)](https://github.com/go-aah/aah/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/aah.v0?status.svg)](https://godoc.org/aahframework.org/aah.v0)
[![Version](https://img.shields.io/badge/version-0.8-blue.svg)](https://github.com/go-aah/aah/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/aah.v0?status.svg)](https://godoc.org/aahframework.org/aah.v0)
[![License](https://img.shields.io/github/license/go-aah/aah.svg)](LICENSE) [![Twitter](https://img.shields.io/badge/[email protected])](https://twitter.com/aahframework)

***Release [v0.7](https://github.com/go-aah/aah/releases/latest) tagged on Aug 01, 2017***
***Release [v0.8](https://github.com/go-aah/aah/releases/latest) tagged on Sep 01, 2017***

aah framework - A scalable, performant, rapid development Web framework for Go.

Expand Down
31 changes: 11 additions & 20 deletions aah.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
"aahframework.org/log.v0"
)

// Version no. of aah framework
const Version = "0.7"

// aah application variables
var (
appName string
Expand All @@ -40,16 +37,15 @@ var (
appIsLetsEncrypt bool
appIsProfileProd bool
appMultipartMaxMemory int64
appMaxBodyBytesSize int64
appPID int
appInitialized bool
appBuildInfo *BuildInfo

appDefaultProfile = "dev"
appProfilePrefix = "env."
appDefaultHTTPPort = "8080"
appDefaultDateFormat = "2006-01-02"
appDefaultDateTimeFormat = "2006-01-02 15:04:05"
appLogFatal = log.Fatal
appDefaultProfile = "dev"
appProfilePrefix = "env."
appDefaultHTTPPort = "8080"
appLogFatal = log.Fatal

goPath string
goSrcDir string
Expand Down Expand Up @@ -106,21 +102,11 @@ func AppHTTPAddress() string {
// AppHTTPPort method returns aah application HTTP port number based on `server.port`
// value. Possible outcomes are user-defined port, `80`, `443` and `8080`.
func AppHTTPPort() string {
port := firstNonEmpty(AppConfig().StringDefault("server.proxyport", ""),
port := firstNonZeroString(AppConfig().StringDefault("server.proxyport", ""),
AppConfig().StringDefault("server.port", appDefaultHTTPPort))
return parsePort(port)
}

// AppDateFormat method returns aah application date format
func AppDateFormat() string {
return AppConfig().StringDefault("format.date", appDefaultDateFormat)
}

// AppDateTimeFormat method returns aah application date format
func AppDateTimeFormat() string {
return AppConfig().StringDefault("format.datetime", appDefaultDateTimeFormat)
}

// AppBuildInfo method return user application version no.
func AppBuildInfo() *BuildInfo {
return appBuildInfo
Expand Down Expand Up @@ -293,6 +279,11 @@ func initAppVariables() error {
return err
}

maxBodySizeStr := cfg.StringDefault("request.max_body_size", "5mb")
if appMaxBodyBytesSize, err = ess.StrToBytes(maxBodySizeStr); err != nil {
return errors.New("'request.max_body_size' value is not a valid size unit")
}

multipartMemoryStr := cfg.StringDefault("request.multipart_size", "32mb")
if appMultipartMaxMemory, err = ess.StrToBytes(multipartMemoryStr); err != nil {
return errors.New("'request.multipart_size' value is not a valid size unit")
Expand Down
28 changes: 17 additions & 11 deletions aah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package aah

import (
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
Expand All @@ -25,8 +24,7 @@ func TestAahInitAppVariables(t *testing.T) {
assert.Nil(t, err)

err = initAppVariables()
assert.NotNil(t, err)
assert.Equal(t, "profile doesn't exists: env.dev", err.Error())
assert.Nil(t, err)

AppConfig().SetString("env.dev.test_value", "dev test value")
err = initAppVariables()
Expand All @@ -36,8 +34,6 @@ func TestAahInitAppVariables(t *testing.T) {
assert.Equal(t, "aah framework test config", AppDesc())
assert.Equal(t, "127.0.0.1", AppHTTPAddress())
assert.Equal(t, "80", AppHTTPPort())
assert.Equal(t, "2006-01-02", AppDateFormat())
assert.Equal(t, "2006-01-02 15:04:05", AppDateTimeFormat())
assert.Equal(t, "en", AppDefaultI18nLang())
assert.True(t, ess.IsStrEmpty(AppImportPath()))
assert.False(t, AppIsSSLEnabled())
Expand Down Expand Up @@ -199,21 +195,21 @@ func TestAahLogDir(t *testing.T) {
assert.Nil(t, err)
appBuildInfo = nil

appLogFatal = func(v ...interface{}) { fmt.Println(v) }
appLogFatal = func(v ...interface{}) { t.Log(v) }
logAsFatal(errors.New("test msg"))

}

func TestWritePID(t *testing.T) {
pidfile := filepath.Join(getTestdataPath(), "test-app.pid")
defer ess.DeleteFiles(pidfile)
pidfile := filepath.Join(getTestdataPath(), "test-app")
defer ess.DeleteFiles(pidfile + ".pid")

cfgDir := filepath.Join(getTestdataPath(), appConfigDir())
err := initConfig(cfgDir)
assert.Nil(t, err)

writePID("test-app", getTestdataPath())
assert.True(t, ess.IsFileExists(pidfile))
writePID(AppConfig(), "test-app", getTestdataPath())
assert.True(t, ess.IsFileExists(pidfile+".pid"))
}

func TestAahBuildInfo(t *testing.T) {
Expand All @@ -235,11 +231,21 @@ func TestAahBuildInfo(t *testing.T) {
func TestAahConfigValidation(t *testing.T) {
err := checkSSLConfigValues(true, false, "/path/to/cert.pem", "/path/to/cert.key")
assert.Equal(t, "SSL cert file not found: /path/to/cert.pem", err.Error())
fmt.Println(err)

certPath := filepath.Join(getTestdataPath(), "cert.pem")
defer ess.DeleteFiles(certPath)
_ = ioutil.WriteFile(certPath, []byte("cert.pem file"), 0755)
err = checkSSLConfigValues(true, false, certPath, "/path/to/cert.key")
assert.Equal(t, "SSL key file not found: /path/to/cert.key", err.Error())
}

func TestAahAppInit(t *testing.T) {
Init("aahframework.org/aah.v0-unstable/testdata")
assert.NotNil(t, appConfig)
assert.NotNil(t, appRouter)
assert.NotNil(t, appSecurityManager)

// reset it
appConfig = nil
appBaseDir = ""
}
9 changes: 6 additions & 3 deletions access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type (
StartTime time.Time
ElapsedDuration time.Duration
Request *ahttp.Request
RequestID string
ResStatus int
ResBytes int
ResHdr http.Header
Expand Down Expand Up @@ -168,20 +169,22 @@ func initAccessLog(logsDir string, appCfg *config.Config) error {

func listenForAccessLog() {
for {
appAccessLog.Print(accessLogFormatter(<-appAccessLogChan))
al := <-appAccessLogChan
appAccessLog.Print(accessLogFormatter(al))
}
}

func sendToAccessLog(ctx *Context) {
al := acquireAccessLog()
al.StartTime = ctx.values[appReqStartTimeKey].(time.Time)
al.StartTime = ctx.Get(appReqStartTimeKey).(time.Time)

// All the bytes have been written on the wire
// so calculate elapsed time
al.ElapsedDuration = time.Since(al.StartTime)

req := *ctx.Req
al.Request = &req
al.RequestID = firstNonZeroString(req.Header.Get(appReqIDHdrKey), "-")
al.ResStatus = ctx.Res.Status()
al.ResBytes = ctx.Res.BytesWritten()
al.ResHdr = ctx.Res.Header()
Expand All @@ -207,7 +210,7 @@ func accessLogFormatter(al *accessLog) string {
case fmtFlagRequestProto:
buf.WriteString(al.Request.Unwrap().Proto)
case fmtFlagRequestID:
buf.WriteString(al.GetRequestHdr(appReqIDHdrKey))
buf.WriteString(al.RequestID)
case fmtFlagRequestHeader:
buf.WriteString(al.GetRequestHdr(part.Format))
case fmtFlagQueryString:
Expand Down
42 changes: 2 additions & 40 deletions access_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func TestAccessLogFormatter(t *testing.T) {
al.Request.Header = al.Request.Raw.Header
al.Request.Header.Add(ahttp.HeaderAccept, "text/html")
al.Request.Header.Set(ahttp.HeaderXRequestID, "5946ed129bf23409520736de")
al.RequestID = "5946ed129bf23409520736de"
al.Request.ClientIP = "127.0.0.1"
al.ResHdr.Add("content-type", "application/json")
allAvailablePatterns := "%clientip %reqid %reqtime %restime %resstatus %ressize %reqmethod %requrl %reqhdr:accept %querystr %reshdr"
expectedForAllAvailablePatterns := fmt.Sprintf(`%s "%s" %s %v %d %d %s %s "%s" "%s" %s`,
expectedForAllAvailablePatterns := fmt.Sprintf(`%s %s %s %v %d %d %s %s "%s" "%s" %s`,
al.Request.ClientIP, al.Request.Header.Get(ahttp.HeaderXRequestID),
al.StartTime.Format(time.RFC3339), fmt.Sprintf("%.4f", al.ElapsedDuration.Seconds()*1e3),
al.ResStatus, al.ResBytes, al.Request.Method,
Expand Down Expand Up @@ -98,45 +99,6 @@ func TestAccessLogInitDefault(t *testing.T) {
`)
}

func TestEngineAccessLog(t *testing.T) {
// App Config
cfgDir := filepath.Join(getTestdataPath(), appConfigDir())
err := initConfig(cfgDir)
assert.Nil(t, err)
assert.NotNil(t, AppConfig())

AppConfig().SetString("server.port", "8080")

// Router
err = initRoutes(cfgDir, AppConfig())
assert.Nil(t, err)
assert.NotNil(t, AppRouter())

// Security
err = initSecurity(AppConfig())
assert.Nil(t, err)
assert.True(t, AppSessionManager().IsStateful())

// Controllers
cRegistry = controllerRegistry{}

AddController((*Site)(nil), []*MethodInfo{
{
Name: "GetInvolved",
Parameters: []*ParameterInfo{},
},
})

AppConfig().SetBool("server.access_log.enable", true)

e := newEngine(AppConfig())
req := httptest.NewRequest("GET", "localhost:8080/get-involved.html", nil)
res := httptest.NewRecorder()
e.ServeHTTP(res, req)

assert.True(t, e.isAccessLogEnabled)
}

func testFormatter(t *testing.T, al *accessLog, pattern, expected string) {
var err error
appAccessLogFmtFlags, err = ess.ParseFmtFlag(pattern, accessLogFmtFlags)
Expand Down
10 changes: 10 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ func (ctx *Context) Reset() {
ctx.decorated = false
}

// Set method is used to set value for the given key in the current request flow.
func (ctx *Context) Set(key string, value interface{}) {
ctx.values[key] = value
}

// Get method returns the value for the given key, otherwise it returns nil.
func (ctx *Context) Get(key string) interface{} {
return ctx.values[key]
}

//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Context Unexported methods
//___________________________________
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestContextMsg(t *testing.T) {
}

msg := ctx.Msg("label.pages.site.get_involved.title")
assert.Equal(t, "", msg)
assert.Equal(t, "en: Get Involved - aah web framework for Go", msg)

msg = ctx.Msgl(ahttp.ToLocale(&ahttp.AcceptSpec{Value: "en", Raw: "en"}), "label.pages.site.get_involved.title")
assert.Equal(t, "en: Get Involved - aah web framework for Go", msg)
Expand Down
2 changes: 1 addition & 1 deletion controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func AddController(c interface{}, methods []*MethodInfo) {
methodMapping := map[string]*MethodInfo{}
for _, method := range methods {
for _, param := range method.Parameters {
param.Type = actualType(param.Type)
param.Type = param.Type.Elem()
}
methodMapping[strings.ToLower(method.Name)] = method
}
Expand Down
Loading

0 comments on commit 1241499

Please sign in to comment.