Skip to content

Commit

Permalink
Merging develop to master for v0.13.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 6, 2016
2 parents a7b72c9 + e3f4697 commit 18b9e8f
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 64 deletions.
10 changes: 7 additions & 3 deletions harness/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func Build(buildFlags ...string) (app *App, compileError *revel.Error) {
versionLinkerFlags := fmt.Sprintf("-X %s/app.APP_VERSION \"%s\"", revel.ImportPath, appVersion)
flags := []string{
"build",
"-i",
"-ldflags", versionLinkerFlags,
"-tags", buildTags,
"-o", binName}
Expand Down Expand Up @@ -173,12 +174,16 @@ func cleanDir(dir string) {
tmpPath := path.Join(revel.AppPath, dir)
f, err := os.Open(tmpPath)
if err != nil {
revel.ERROR.Println("Failed to clean dir:", err)
if !os.IsNotExist(err) {
revel.ERROR.Println("Failed to clean dir:", err)
}
} else {
defer f.Close()
infos, err := f.Readdir(0)
if err != nil {
revel.ERROR.Println("Failed to clean dir:", err)
if !os.IsNotExist(err) {
revel.ERROR.Println("Failed to clean dir:", err)
}
} else {
for _, info := range infos {
path := path.Join(tmpPath, info.Name())
Expand All @@ -198,7 +203,6 @@ func cleanDir(dir string) {
}
}


// genSource renders the given template to produce source code, which it writes
// to the given directory and file.
func genSource(dir, filename, templateSource string, args map[string]interface{}) {
Expand Down
40 changes: 28 additions & 12 deletions harness/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package harness
import (
"crypto/tls"
"fmt"
"github.com/revel/revel"
"go/build"
"io"
"net"
Expand All @@ -22,10 +21,11 @@ import (
"net/url"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"sync/atomic"

"github.com/revel/revel"
)

var (
Expand All @@ -52,7 +52,7 @@ func renderError(w http.ResponseWriter, r *http.Request, err error) {

// ServeHTTP handles all requests.
// It checks for changes to app, rebuilds if necessary, and forwards the request.
func (hp *Harness) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *Harness) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Don't rebuild the app for favicon requests.
if lastRequestHadError > 0 && r.URL.Path == "/favicon.ico" {
return
Expand All @@ -71,18 +71,19 @@ func (hp *Harness) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Reverse proxy the request.
// (Need special code for websockets, courtesy of bradfitz)
if strings.EqualFold(r.Header.Get("Upgrade"), "websocket") {
proxyWebsocket(w, r, hp.serverHost)
proxyWebsocket(w, r, h.serverHost)
} else {
hp.proxy.ServeHTTP(w, r)
h.proxy.ServeHTTP(w, r)
}
}

// Return a reverse proxy that forwards requests to the given port.
// NewHarness method returns a reverse proxy that forwards requests
// to the given port.
func NewHarness() *Harness {
// Get a template loader to render errors.
// Prefer the app's views/errors directory, and fall back to the stock error pages.
revel.MainTemplateLoader = revel.NewTemplateLoader(
[]string{path.Join(revel.RevelPath, "templates")})
[]string{filepath.Join(revel.RevelPath, "templates")})
revel.MainTemplateLoader.Refresh()

addr := revel.HttpAddr
Expand All @@ -101,12 +102,12 @@ func NewHarness() *Harness {
port = getFreePort()
}

serverUrl, _ := url.ParseRequestURI(fmt.Sprintf(scheme+"://%s:%d", addr, port))
serverURL, _ := url.ParseRequestURI(fmt.Sprintf(scheme+"://%s:%d", addr, port))

harness := &Harness{
port: port,
serverHost: serverUrl.String()[len(scheme+"://"):],
proxy: httputil.NewSingleHostReverseProxy(serverUrl),
serverHost: serverURL.String()[len(scheme+"://"):],
proxy: httputil.NewSingleHostReverseProxy(serverURL),
}

if revel.HttpSsl {
Expand All @@ -117,7 +118,7 @@ func NewHarness() *Harness {
return harness
}

// Rebuild the Revel application and run it on the given port.
// Refresh method rebuilds the Revel application and run it on the given port.
func (h *Harness) Refresh() (err *revel.Error) {
if h.app != nil {
h.app.Kill()
Expand All @@ -140,10 +141,14 @@ func (h *Harness) Refresh() (err *revel.Error) {
return
}

// WatchDir method returns false to file matches with doNotWatch
// otheriwse true
func (h *Harness) WatchDir(info os.FileInfo) bool {
return !revel.ContainsString(doNotWatch, info.Name())
}

// WatchFile method returns true given filename HasSuffix of ".go"
// otheriwse false
func (h *Harness) WatchFile(filename string) bool {
return strings.HasSuffix(filename, ".go")
}
Expand Down Expand Up @@ -204,7 +209,18 @@ func getFreePort() (port int) {
// proxyWebsocket copies data between websocket client and server until one side
// closes the connection. (ReverseProxy doesn't work with websocket requests.)
func proxyWebsocket(w http.ResponseWriter, r *http.Request, host string) {
d, err := net.Dial("tcp", host)
var (
d net.Conn
err error
)
if revel.HttpSsl {
// since this proxy isn't used in production,
// it's OK to set InsecureSkipVerify to true
// no need to add another configuration option.
d, err = tls.Dial("tcp", host, &tls.Config{InsecureSkipVerify: true})
} else {
d, err = net.Dial("tcp", host)
}
if err != nil {
http.Error(w, "Error contacting backend server.", 500)
revel.ERROR.Printf("Error dialing websocket backend %s: %v", host, err)
Expand Down
5 changes: 3 additions & 2 deletions harness/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ProcessSource(roots []string) (*SourceInfo, *revel.Error) {
}

// Start walking the directory tree.
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
_ = revel.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Println("Error scanning app source:", err)
return nil
Expand Down Expand Up @@ -433,7 +433,8 @@ func appendAction(fset *token.FileSet, mm methodMap, decl ast.Decl, pkgImportPat
var importPath string
typeExpr := NewTypeExpr(pkgName, field.Type)
if !typeExpr.Valid {
return // We didn't understand one of the args. Ignore this action. (Already logged)
log.Printf("Didn't understand argument '%s' of action %s. Ignoring.\n", name, getFuncName(funcDecl))
return // We didn't understand one of the args. Ignore this action.
}
if typeExpr.PkgName != "" {
var ok bool
Expand Down
20 changes: 15 additions & 5 deletions revel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import (
"path/filepath"
"strings"

"github.com/revel/revel"
"github.com/revel/cmd/harness"
"github.com/revel/revel"
)

var cmdBuild = &Command{
UsageLine: "build [import path] [target path]",
UsageLine: "build [import path] [target path] [run mode]",
Short: "build a Revel application (e.g. for deployment)",
Long: `
Build the Revel web application named by the given import path.
This allows it to be deployed and run on a machine that lacks a Go installation.
The run mode is used to select which set of app.conf configuration should
apply and may be used to determine logic in the application itself.
Run mode defaults to "dev".
WARNING: The target path will be completely deleted, if it already exists!
For example:
Expand All @@ -31,14 +36,18 @@ func init() {
}

func buildApp(args []string) {
if len(args) != 2 {
if len(args) < 2 {
fmt.Fprintf(os.Stderr, "%s\n%s", cmdBuild.UsageLine, cmdBuild.Long)
return
}

appImportPath, destPath := args[0], args[1]
appImportPath, destPath, mode := args[0], args[1], "dev"
if len(args) >= 3 {
mode = args[2]
}

if !revel.Initialized {
revel.Init("", appImportPath, "")
revel.Init(mode, appImportPath, "")
}

// First, verify that it is either already empty or looks like a previous
Expand Down Expand Up @@ -96,6 +105,7 @@ func buildApp(args []string) {
tmplData, runShPath := map[string]interface{}{
"BinName": filepath.Base(app.BinaryPath),
"ImportPath": appImportPath,
"Mode": mode,
}, path.Join(destPath, "run.sh")

mustRenderTemplate(
Expand Down
21 changes: 13 additions & 8 deletions revel/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For example:
revel clean github.com/revel/samples/chat
It removes the app/tmp directory.
It removes the app/tmp and app/routes directory.
`,
}

Expand All @@ -37,12 +37,17 @@ func cleanApp(args []string) {
return
}

// Remove the app/tmp directory.
tmpDir := path.Join(appPkg.Dir, "app", "tmp")
fmt.Println("Removing:", tmpDir)
err = os.RemoveAll(tmpDir)
if err != nil {
fmt.Fprintln(os.Stderr, "Abort:", err)
return
purgeDirs := []string{
path.Join(appPkg.Dir, "app", "tmp"),
path.Join(appPkg.Dir, "app", "routes"),
}

for _, dir := range purgeDirs {
fmt.Println("Removing:", dir)
err = os.RemoveAll(dir)
if err != nil {
fmt.Fprintln(os.Stderr, "Abort:", err)
return
}
}
}
35 changes: 31 additions & 4 deletions revel/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"fmt"
"go/build"
"log"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/revel/revel"
)
Expand Down Expand Up @@ -60,6 +62,8 @@ func newApp(args []string) {
errorf("Too many arguments provided.\nRun 'revel help new' for usage.\n")
}

revel.ERROR.SetFlags(log.LstdFlags)

// checking and setting go paths
initGoPaths()

Expand Down Expand Up @@ -96,22 +100,45 @@ func initGoPaths() {
"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
}

// set go src path
srcRoot = filepath.Join(filepath.SplitList(gopath)[0], "src")

// check for go executable
var err error
gocmd, err = exec.LookPath("go")
if err != nil {
errorf("Go executable not found in PATH.")
}

// revel/revel#1004 choose go path relative to current working directory
workingDir, _ := os.Getwd()
goPathList := filepath.SplitList(gopath)
for _, path := range goPathList {
if strings.HasPrefix(strings.ToLower(workingDir), strings.ToLower(path)) {
srcRoot = path
break
}

path, _ = filepath.EvalSymlinks(path)
if len(path) > 0 && strings.HasPrefix(strings.ToLower(workingDir), strings.ToLower(path)) {
srcRoot = path
break
}
}

if len(srcRoot) == 0 {
revel.ERROR.Fatalln("Abort: could not create a Revel application outside of GOPATH.")
}

// set go src path
srcRoot = filepath.Join(srcRoot, "src")
}

func setApplicationPath(args []string) {
var err error
importPath = args[0]
if filepath.IsAbs(importPath) {

// revel/revel#1014 validate relative path, we cannot use built-in functions
// since Go import path is valid relative path too.
// so check basic part of the path, which is "."
if filepath.IsAbs(importPath) || strings.HasPrefix(importPath, ".") {
errorf("Abort: '%s' looks like a directory. Please provide a Go import path instead.",
importPath)
}
Expand Down
20 changes: 16 additions & 4 deletions revel/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ package main

import (
"fmt"
"github.com/revel/revel"
"io/ioutil"
"os"
"path/filepath"

"github.com/revel/revel"
)

var cmdPackage = &Command{
UsageLine: "package [import path]",
UsageLine: "package [import path] [run mode]",
Short: "package a Revel application (e.g. for deployment)",
Long: `
Package the Revel web application named by the given import path.
This allows it to be deployed and run on a machine that lacks a Go installation.
The run mode is used to select which set of app.conf configuration should
apply and may be used to determine logic in the application itself.
Run mode defaults to "dev".
For example:
revel package github.com/revel/samples/chat
Expand All @@ -31,8 +37,14 @@ func packageApp(args []string) {
return
}

// Determine the run mode.
mode := "dev"
if len(args) >= 2 {
mode = args[1]
}

appImportPath := args[0]
revel.Init("", appImportPath, "")
revel.Init(mode, appImportPath, "")

// Remove the archive if it already exists.
destFile := filepath.Base(revel.BasePath) + ".tar.gz"
Expand All @@ -42,7 +54,7 @@ func packageApp(args []string) {
tmpDir, err := ioutil.TempDir("", filepath.Base(revel.BasePath))
panicOnError(err, "Failed to get temp dir")

buildApp([]string{args[0], tmpDir})
buildApp([]string{args[0], tmpDir, mode})

// Create the zip file.
archiveName := mustTarGzDir(destFile, tmpDir)
Expand Down
2 changes: 1 addition & 1 deletion revel/package_run.bat.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
{{.BinName}} -importPath {{.ImportPath}} -srcPath %CD%\src -runMode prod
{{.BinName}} -importPath {{.ImportPath}} -srcPath %CD%\src -runMode {{.Mode}}
2 changes: 1 addition & 1 deletion revel/package_run.sh.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh
SCRIPTPATH=$(cd "$(dirname "$0")"; pwd)
"$SCRIPTPATH/{{.BinName}}" -importPath {{.ImportPath}} -srcPath "$SCRIPTPATH/src" -runMode prod
"$SCRIPTPATH/{{.BinName}}" -importPath {{.ImportPath}} -srcPath "$SCRIPTPATH/src" -runMode {{.Mode}}
Loading

0 comments on commit 18b9e8f

Please sign in to comment.