diff --git a/.gitignore b/.gitignore index ea84285..3af2489 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ tmp/ bower_components node_modules /webapp/index.html +dist/ diff --git a/gulpfile.js b/gulpfile.js index 906312b..ced6d67 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,6 +1,82 @@ var gulp = require('gulp'); var plugins = require('gulp-load-plugins')({}); var karma = require('karma'); +var es = require('event-stream'); +var bowerFiles = require('main-bower-files'); +var exists = require('path-exists').sync + +var pipes = {}; + +var paths = { + appFiles: 'webapp/js/**/*.js', + cssFiles: 'webapp/css/**/*.css', + partials: 'webapp/js/**/*.html', + pages : ['webapp/html/**/*.html', '!webapp/html/index.html'], + index : 'webapp/html/index.html', + distDev : 'dist/dev', + distProd: 'dist/prod' +} + + +pipes.vendorJsFiles = function() { + return gulp.src(bowerFiles({ + overrides : { + jquery : { + main: [] + }, + bootstrap : { + main: [ "dist/**/*.css" ] + } + } + }).map(function(path, index,arr) { + var newPath = path.replace(/.([^.]+)$/g, '.min.$1'); + console.log('path: ' + path + ' newpath:' + newPath + ' exits: ' + exists(newPath) ) + return exists ( newPath ) ? newPath : path; + })) + .pipe(plugins.debug({title: 'vendor-js:'})) +} + +pipes.validatedAppScripts = function() { + return gulp.src(paths.appFiles) + .pipe(plugins.jshint()) + .pipe(plugins.debug({title: 'validated-js:'})); +} + +pipes.validatedIndex = function() { + return gulp.src(paths.index); +} + +pipes.validatedPartials = function() { + return gulp.src(paths.partials); +} + +pipes.validatedPages = function() { + return gulp.src(paths.pages) +} + +pipes.builtIndexDev = function() { + var vendorFiles = pipes.vendorJsFiles() + .pipe(gulp.dest(paths.distDev + '/vendor')); + var appScript = pipes.validatedAppScripts() + .pipe(plugins.angularFilesort()) + .pipe(gulp.dest(paths.distDev + '/js')) + var appStyle = gulp.src(paths.cssFiles) + .pipe(gulp.dest(paths.distDev + '/css')) + + var partials = pipes.validatedPartials() + .pipe(gulp.dest(paths.distDev + '/js')) + + var pages = pipes.validatedPages() + .pipe(gulp.dest(paths.distDev + '/html')) + + return pipes.validatedIndex() + .pipe(plugins.inject(vendorFiles, {ignorePath: '/'+ paths.distDev, name:'bower'})) + .pipe(plugins.inject(es.merge(appStyle,partials,pages), {ignorePath: '/'+ paths.distDev})) + .pipe(plugins.inject(appScript, {ignorePath: '/'+ paths.distDev})) + .pipe(gulp.dest(paths.distDev)); +} + + gulp.task('test', function(done) { server = new karma.Server({ @@ -17,17 +93,4 @@ gulp.task('test', function(done) { -gulp.task('build', function() { - return gulp.src('./webapp/html/index.html') - .pipe(plugins.inject( - gulp.src(['./webapp/js/**/*.js']).pipe(plugins.angularFilesort()), - { - ignorePath: '/webapp' - } - )) - .pipe(gulp.dest('./webapp')); -}); - -gulp.task('autobuild', function() { - return gulp.watch(['webapp/js/**/*.js','webapp/html/index.html'], ['build']); -}); +gulp.task('build-dev', pipes.builtIndexDev); diff --git a/main.go b/main.go index fc1c0f9..84a2885 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ type Options struct { RequestWindow time.Duration `long:"request-window" description:"Window over which no more --max-request are done"` CacheTTL time.Duration `long:"chache-ttl" description:"TTL of the cached data" ` BatchSize int `long:"batch-size" description:"Sizes of the batch for indexing" default:"1000"` + BasePath string `long:"basepath" short:"b" description:"basepath of the webapp" default:"dist/prod"` } type appData struct { @@ -378,7 +379,7 @@ func Execute() error { log.Printf("Listening on %s", opts.Listen) a.start(srv.StopChan()) - srv.Server.Handler = a.buildRouter() + srv.Server.Handler = a.buildRouter(opts.BasePath) if err := srv.ListenAndServe(); err != nil { if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") { return err diff --git a/package.json b/package.json index a2d72ae..a9c3ea6 100644 --- a/package.json +++ b/package.json @@ -6,18 +6,24 @@ "bower": "^1.7.7", "gulp": "^3.9.1", "gulp-angular-filesort": "^1.1.1", + "gulp-debug": "^2.1.2", "gulp-inject": "^3.0.0", "gulp-jasmine": "^2.2.1", + "gulp-jshint": "^2.0.0", "gulp-karma": "0.0.5", "gulp-load-plugins": "^1.2.0", + "gulp-rename": "^1.2.2", "gulp-util": "^3.0.7", "jasmine-core": "^2.4.1", + "jshint": "^2.9.1", "karma": "^0.13.19", "karma-chrome-launcher": "^0.2.2", "karma-firefox-launcher": "^0.1.7", "karma-jasmine": "^0.3.7", "karma-junit-reporter": "^0.3.8", "karma-safari-launcher": "^0.1.1", + "main-bower-files": "^2.11.1", + "path-exists": "^2.1.0", "protractor": "^3.1.1" }, "scripts": { diff --git a/router.go b/router.go index 0e79905..29fdbe5 100644 --- a/router.go +++ b/router.go @@ -19,7 +19,7 @@ import ( "golang.org/x/net/context" ) -func (a *appData) buildRouter() http.Handler { +func (a *appData) buildRouter(basepath string) http.Handler { router := httprouter.New() logger := narco.NewLogger() @@ -126,19 +126,19 @@ func (a *appData) buildRouter() http.Handler { }))) - dirs := []string{"css", "js", "img", "bower_components", "html"} + dirs := []string{"css", "js", "vendor", "html"} for _, d := range dirs { - router.ServeFiles(path.Join("/", d, "/*filepath"), http.Dir(filepath.Join("webapp", d))) + router.ServeFiles(path.Join("/", d, "/*filepath"), http.Dir(filepath.Join(basepath, d))) } router.GET("/", narco.EndChain(ch, narco.HandlerFunc( func(ctx context.Context, w http.ResponseWriter, req *http.Request, _ httprouter.Params) { - f, err := os.Open(filepath.Join("webapp", "index.html")) + f, err := os.Open(filepath.Join(basepath, "index.html")) if err != nil { narco.Error(ctx, w, err, http.StatusInternalServerError) return } - defer closeOrPanic(f, filepath.Join("webapp", "index.html")) + defer closeOrPanic(f, filepath.Join(basepath, "index.html")) _, err = io.Copy(w, f) if err != nil { diff --git a/webapp/html/index.html b/webapp/html/index.html index 7114341..92b94fd 100644 --- a/webapp/html/index.html +++ b/webapp/html/index.html @@ -4,10 +4,12 @@ - -