Skip to content

Commit

Permalink
Check on ./templates Fall back to /usr/local/share/cosgo/templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aerth committed Apr 30, 2016
1 parent a95f598 commit 83124e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
31 changes: 28 additions & 3 deletions boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,45 @@ func quickSelfTest() (err error) {
}
}

cosgo.Templates = "./templates"
cosgo.Static = cosgo.Dir + "static/"
if *static {
_, err = os.Open(cosgo.Static)
if err != nil {
if os.IsNotExist(err) {
cosgo.Static = "/usr/local/share/cosgo/static/"

_, err = os.Open(cosgo.Static)
if err != nil {
if os.IsNotExist(err) {

log.Fatalln("Need static directory for use with -static flag.")

}
}

}
}
}

cosgo.Templates = cosgo.Dir + "templates/"
log.Println("Trying ./templates")
// Main template. Replace with your own, but keep the {{.Tags}} in it.
_, err = template.New("Index").ParseFiles(cosgo.Templates + "index.html")
if err != nil {
log.Println("Woops: Template Error:", err)
_, err2 := template.New("Index").ParseFiles("/usr/local/share/cosgo/templates/index.html")
if err2 != nil {
log.Println("Fatal: Template Error:", err2)
log.Fatal("Fatal: Template Error\n\n\t\tHint: Copy ./templates and ./static from $GOPATH/src/github.com/aerth/cosgo/ to the location you are running cosgo from.")
log.Fatal("Fatal: Template Error\n\n\t\tHint: Copy templates+static directories from $GOPATH/src/github.com/aerth/cosgo/ to the location you are running cosgo from.")
} else {
log.Println("Using /usr/local/share/cosgo/templates directory")
cosgo.Templates = "/usr/local/share/cosgo/templates/"
}

} else {
cosgo.Templates = "./templates/"
log.Println("Using ./templates directory")
cosgo.Templates = cosgo.Dir + "templates/"

}

// Make sure Error pages template is present
Expand Down
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ var (
type Cosgo struct {
PostKey string
Boottime time.Time
Static string
Templates string // Directory where templates are located. Defaults to ./templates and falls back to /usr/local/share/cosgo/templates
Dir string // Directory where we are currently located (./)
}

var cosgo = new(Cosgo)
Expand Down Expand Up @@ -201,7 +203,11 @@ func main() {

// Future: dont use flags pkg
flag.Parse()

cwd, err := os.Getwd()
if err != nil {
log.Fatalln(err)
}
cosgo.Dir = cwd + "/"
// Custom refresh time
// Example: COSGOREFRESH=10m cosgo -debug
if os.Getenv("COSGOREFRESH") != "" {
Expand Down Expand Up @@ -401,10 +407,12 @@ func main() {
// POST endpoint (emailHandler checks the key)
r.HandleFunc("/{{whatever}}/send", emailHandler)

// TODO: allow changing static and files directory names
// TODO: maybe allow changing static and files directory names
// TODO: mime types
s := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))
ss := http.FileServer(http.Dir("./static/"))

//s := http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))
s := http.StripPrefix("/static/", http.FileServer(http.Dir(cosgo.Static)))
ss := http.FileServer(http.Dir(cosgo.Static))
sf := http.FileServer(http.Dir("./files/"))
sp := http.StripPrefix("/"+*custompages+"/", http.FileServer(http.Dir("./"+*custompages+"/")))

Expand Down

0 comments on commit 83124e3

Please sign in to comment.