Skip to content

bom-d-van/train

 
 

Repository files navigation

Train

Asset Management Package for web app in Go language. Inspired by Rails Asset Pipeline.

Build Status

Quick Look

Use Train to manage your asset's dependencies. Enables you to write javascript or stylesheet in the following way:

Javascript

assets/javascripts/base.js

appName = "...";

assets/javascripts/app.js

//= require javascripts/base

$(function(){
  // Do something cool
})

GET /assets/javascripts/app.js

appName = "...";

$(function(){
  // Do something cool
})

Stylesheet

assets/stylesheets/base.css

h1, h2{ padding:0; }

assets/stylesheets/app.css

/*
 *= require stylesheets/base
 */

body{...}

GET /assets/stylesheets/app.css

h1, h2{ padding:0; }

body{...}

CoffeeScript

assets/javascripts/app.coffee

alert "Hello CoffeeScript!"

GET /assets/javascripts/app.js

alert("Hello CoffeeScript!");

SASS

assets/stylesheets/app.sass

body
  color: red

GET /assets/stylesheets/app.js

body{
  color: red; }

Usages

Http Handler

  package main

  import (
    "fmt"
    "github.com/shaoshing/train"
    "net/http"
  )

  func main() {
    http.Handle(train.Config.AssetsUrl, http.HandlerFunc(train.Handler))
    fmt.Println("Listening to localhost:8000")
    http.ListenAndServe(":8000", nil)
  }

Template Helpers

  import "github.com/shaoshing/train"

  func main() {
    tmpl := template.New("index")
    tmpl.Funcs(train.HelperFuncs)
    tmpl.Parse(`
    {{define "index"}}
      {{javascript_tag "app"}}

      {{stylesheet_tag "app"}}
    {{end}}
    `)

    tmpl.Execute(os.Stdout, "index", nil)
    //
    // <script src="/assets/javascripts/base.js?12345"></script>
    // <script src="/assets/javascripts/app.js?12345"></script>
    //
    // <link rel="stylesheet" href="/assets/stylesheets/base.css?12345">
    // <link rel="stylesheet" href="/assets/stylesheets/app.css?12345">
  }

Production

Install the command line tool to bundle and compress assets automatically:

go build -o $GOPATH/bin/train github.com/shaoshing/train/cmd

train
-> clean bundled assets
-> copy assets from assets
-> bundle assets with require directive
-> compress assets

ls public/assets

The train tool will bundle your assets into the public/assets folder, with all files expaneded and compressed (by YUI compressor). You can then use any web servers (nginx, apache, or the Go's file server) to serve these static files. The template helpers will also stop expanding files if it found the public assets folder. That is, the following code:

{{javascript_tag "app"}}
{{stylesheet_tag "app"}}

Will become:

<script src="/assets/javascripts/app.js?12345"></script>
<link rel="stylesheet" href="/assets/stylesheets/app.css?12345">

License

Train is released under the MIT License.

About

Asset Management for web app using Golang.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 93.2%
  • Ruby 5.1%
  • JavaScript 1.5%
  • CoffeeScript 0.2%