Directory structure for a Go web application

I have followed Writing Web Applications on the Go website and I am starting to write my own web application. I also read the beginning of "How to write the transition code" and am trying to organize my code with the same workspace structure.

I am writing a simple web application named mygosite that handles all requests by creating one template. Upon startup, go install github.com/wesleym/mygosite

my directory structure now looks like this:

go
+-src
| +-github.com
|   +-wesleym
|     +-mygosite
|       +-mygosite.go
|       +-templates
|         +- index.html
|       +-.git
+-bin
  +-mygosite

      

In my code, I am referring to a template with an outline templates/index.html

. When I run bin/mygosite

, the application cannot find my template because it is in the source tree. Since templates are important to my application, I don't want to move them outside of my mygosite git repository.

Is my directory layout sane given what I am trying to do? Do I have to change the path for my template in my code to be src/github.com/wesleym/mygosite/templates/index.html

? What about static assets like CSS and JavaScript - where should they go when it's time to submit them?

tl; dr: Where to put templates and static files in a Go web application project?

+3


source to share


3 answers


Where to put templates and static files in a Go web app project?



is the "wrong" question. If you ask, "How does my executable find resources?" you're almost on your way: your executable must read its resources from (or multiple) locations that are configurable (and it's always nice to provide reasonable defaults). Command line flags, environment variables, and configuration files in the current working directory are common for such tasks. (Of course, if you just have a small number of small resources: add them to your executable as VonC recommended, this scheme just breaks down when you start including large assets like images or videos.)

+3


source


You may consider using the project jteeuwen/go-bindata

This package will convert any file to Go managed source. Useful for embedding binary data in a go program. The file data is optionally compressed with gzip before being converted to the original byte slice.

You can see that it is used in the " golang embed file for later parsing.



This same page also mentions GeertJohan/go.rice

as an alternative

Another good tool is esc: Nesting Static Assets in Go

a program that:

  • can take some directories and recursively embed all files in them in a way that was compatible with http.FileSystem
  • can optionally be disabled for use with local filesystem for local development
  • will not modify the output file on subsequent runs
  • has a reasonable size difference when changing files.
  • is a seller
+2


source


If you want to embed a template (somewhat like template inheritance) try: go (golang) a template manager, especially for web development .

You can suggest a template for templates:

templates/
├── context
│   ├── layout
│   │   └── layout.tpl.html
│   └── partial
│       └── ads.tpl.html
└── main
    └── demo
        ├── demo1.tpl.html
        ├── demo2.tpl.html
        └── dir1
            └── dir2
                └── any.tpl.html

      

demo1

demo2

any

0


source







All Articles