Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Dynamic resource loading
Browse files Browse the repository at this point in the history
Load resources either from the dir of the executable, or from the
resolved go path, preferring the dir of the executable.
  • Loading branch information
jbowes committed Aug 11, 2013
1 parent 7816b1f commit 9ad5bba
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
Expand All @@ -20,6 +21,8 @@ var (
lastModified = time.Now()
lastModifiedStr = lastModified.UTC().Format(http.TimeFormat)
oneYear = time.Duration(8700) * time.Hour

staticPath, _ = resourcePaths()
)

func shift(s []string) ([]string, string) {
Expand Down Expand Up @@ -105,12 +108,14 @@ func buckle(w http.ResponseWriter, r *http.Request) {
makePngShield(w, d)
}

const basePkg = "github.com/jbowes/buckler"

func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/index.html")
http.ServeFile(w, r, filepath.Join(staticPath, "index.html"))
}

func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "static/favicon.png")
http.ServeFile(w, r, filepath.Join(staticPath, "favicon.png"))
}

func fatal(msg string) {
Expand Down
12 changes: 6 additions & 6 deletions png.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"log"
"math"
"os"
"path/filepath"
"strconv"

"code.google.com/p/freetype-go/freetype"
"code.google.com/p/freetype-go/freetype/raster"
"code.google.com/p/freetype-go/freetype/truetype"
)

type Data struct {
Expand Down Expand Up @@ -55,7 +55,6 @@ var (

edge image.Image
gradient image.Image
font *truetype.Font
c *freetype.Context
)

Expand All @@ -66,7 +65,8 @@ const (
)

func init() {
fi, err := os.Open("data/edge.png")
_, dataPath := resourcePaths()
fi, err := os.Open(filepath.Join(dataPath, "edge.png"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -77,7 +77,7 @@ func init() {
log.Fatal(err)
}

fi, err = os.Open("data/gradient.png")
fi, err = os.Open(filepath.Join(dataPath, "gradient.png"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -88,12 +88,12 @@ func init() {
log.Fatal(err)
}

fontBytes, err := ioutil.ReadFile("data/opensanssemibold.ttf")
fontBytes, err := ioutil.ReadFile(filepath.Join(dataPath, "opensanssemibold.ttf"))
if err != nil {
log.Fatal(err)
}

font, err = freetype.ParseFont(fontBytes)
font, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Fatal(err)
}
Expand Down
44 changes: 44 additions & 0 deletions resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"go/build"
"log"
"os"
"path/filepath"

"bitbucket.org/kardianos/osext"
)

// exists returns whether the given file or directory exists or not
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}

func resourcePaths() (staticPath string, dataPath string) {
base, err := osext.ExecutableFolder()
if err != nil {
log.Fatal("Could not read base dir")
}

staticPath = filepath.Join(base, "static")
dataPath = filepath.Join(base, "data")
if exists(dataPath) && exists(staticPath) {
return
}

p, err := build.Default.Import(basePkg, "", build.FindOnly)
if err != nil {
log.Fatal("Could not find package dir")
}

staticPath = filepath.Join(p.Dir, "static")
dataPath = filepath.Join(p.Dir, "data")
return
}

0 comments on commit 9ad5bba

Please sign in to comment.