commit 11d242c18549beae13f44307eb98f7a297a672aa
Author: Arlo Breault <[email protected]>
Date:   Sat Oct 26 13:36:48 2013 -0700

    Add a command line arg for a base path.
---
 check.go          |   16 +++++++++-------
 datastore.go      |   10 +++++-----
 datastore_test.go |    4 ++--
 handlers.go       |    4 +---
 utils.go          |   23 ++++++++++++-----------
 5 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/check.go b/check.go
index 76cdd25..73e6a7c 100644
--- a/check.go
+++ b/check.go
@@ -14,7 +14,8 @@ func main() {
 
        // command line args
        logPath := flag.String("log", "", "path to log file; otherwise stdout")
-       pidPath := flag.String("pid", "./", "path to create pid")
+       pidPath := flag.String("pid", "./check.pid", "path to create pid")
+       basePath := flag.String("base", "./", "path to base dir")
        port := flag.Int("port", 8000, "port to listen on")
        flag.Parse()
 
@@ -28,7 +29,7 @@ func main() {
        }
 
        // write pid
-       pid, err := os.Create(path.Join(*pidPath, "check.pid"))
+       pid, err := os.Create(*pidPath)
        if err != nil {
                log.Fatal(err)
        }
@@ -40,24 +41,25 @@ func main() {
        }
 
        // load i18n
-       domain, err := gettext.NewDomain("check", "locale")
+       domain, err := gettext.NewDomain("check", path.Join(*basePath, 
"locale"))
        if err != nil {
                log.Fatal(err)
        }
+       Locales := GetLocaleList(*basePath)
 
        // Load Tor exits and listen for SIGUSR2 to reload
        exits := new(Exits)
-       exits.Run()
+       exits.Run(path.Join(*basePath, "data/exit-policies"))
 
        // files
-       files := http.FileServer(http.Dir("./public"))
+       files := http.FileServer(http.Dir(path.Join(*basePath, "public")))
        Phttp := http.NewServeMux()
        Phttp.Handle("/torcheck/", http.StripPrefix("/torcheck/", files))
        Phttp.Handle("/", files)
 
        // routes
-       http.HandleFunc("/", RootHandler(CompileTemplate(domain, "index.html"), 
exits, domain, Phttp))
-       bulk := BulkHandler(CompileTemplate(domain, "bulk.html"), exits, domain)
+       http.HandleFunc("/", RootHandler(CompileTemplate(*basePath, domain, 
"index.html"), exits, domain, Phttp, Locales))
+       bulk := BulkHandler(CompileTemplate(*basePath, domain, "bulk.html"), 
exits, domain)
        http.HandleFunc("/torbulkexitlist", bulk)
        http.HandleFunc("/cgi-bin/TorBulkExitList.py", bulk)
 
diff --git a/datastore.go b/datastore.go
index e1e981d..c01acc6 100644
--- a/datastore.go
+++ b/datastore.go
@@ -182,8 +182,8 @@ func (e *Exits) Load(source io.Reader, update bool) error {
        return nil
 }
 
-func (e *Exits) LoadFromFile(update bool) {
-       file, err := os.Open(os.ExpandEnv("${TORCHECKBASE}data/exit-policies"))
+func (e *Exits) LoadFromFile(filePath string, update bool) {
+       file, err := os.Open(os.ExpandEnv(filePath))
        defer file.Close()
        if err != nil {
                log.Fatal(err)
@@ -193,15 +193,15 @@ func (e *Exits) LoadFromFile(update bool) {
        }
 }
 
-func (e *Exits) Run() {
+func (e *Exits) Run(filePath string) {
        e.ReloadChan = make(chan os.Signal, 1)
        signal.Notify(e.ReloadChan, syscall.SIGUSR2)
        go func() {
                for {
                        <-e.ReloadChan
-                       e.LoadFromFile(true)
+                       e.LoadFromFile(filePath, true)
                        log.Println("Exit list updated.")
                }
        }()
-       e.LoadFromFile(false)
+       e.LoadFromFile(filePath, false)
 }
diff --git a/datastore_test.go b/datastore_test.go
index 204f371..ccde0a5 100644
--- a/datastore_test.go
+++ b/datastore_test.go
@@ -183,7 +183,7 @@ func TestPastHours(t *testing.T) {
 
 func BenchmarkIsTor(b *testing.B) {
        e := new(Exits)
-       e.LoadFromFile(false)
+       e.LoadFromFile("data/exit-policies", false)
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
                e.IsTor("91.121.43.80")
@@ -193,7 +193,7 @@ func BenchmarkIsTor(b *testing.B) {
 
 func BenchmarkDumpList(b *testing.B) {
        e := new(Exits)
-       e.LoadFromFile(false)
+       e.LoadFromFile("data/exit-policies", false)
        buf := new(bytes.Buffer)
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
diff --git a/handlers.go b/handlers.go
index f4a308f..08fc64b 100644
--- a/handlers.go
+++ b/handlers.go
@@ -13,8 +13,6 @@ import (
        "time"
 )
 
-var Locales = GetLocaleList()
-
 // page model
 type Page struct {
        IsTor       bool
@@ -27,7 +25,7 @@ type Page struct {
        Locales     map[string]string
 }
 
-func RootHandler(Layout *template.Template, Exits *Exits, domain 
*gettext.Domain, Phttp *http.ServeMux) http.HandlerFunc {
+func RootHandler(Layout *template.Template, Exits *Exits, domain 
*gettext.Domain, Phttp *http.ServeMux, Locales map[string]string) 
http.HandlerFunc {
 
        return func(w http.ResponseWriter, r *http.Request) {
 
diff --git a/utils.go b/utils.go
index ef7be1d..8b0bda2 100644
--- a/utils.go
+++ b/utils.go
@@ -11,6 +11,7 @@ import (
        "net/http"
        "net/url"
        "os"
+       "path"
        "strconv"
 )
 
@@ -73,20 +74,20 @@ func FuncMap(domain *gettext.Domain) template.FuncMap {
 
 var Layout *template.Template
 
-func CompileTemplate(domain *gettext.Domain, templateName string) 
*template.Template {
+func CompileTemplate(base string, domain *gettext.Domain, templateName string) 
*template.Template {
        if Layout == nil {
                Layout = template.New("")
                Layout = Layout.Funcs(FuncMap(domain))
                Layout = template.Must(Layout.ParseFiles(
-                       "public/base.html",
-                       "public/torbutton.html",
+                       path.Join(base, "public/base.html"),
+                       path.Join(base, "public/torbutton.html"),
                ))
        }
        l, err := Layout.Clone()
        if err != nil {
                log.Fatal(err)
        }
-       return template.Must(l.ParseFiles("public/" + templateName))
+       return template.Must(l.ParseFiles(path.Join(base, "public/", 
templateName)))
 }
 
 type locale struct {
@@ -94,7 +95,7 @@ type locale struct {
        Name string
 }
 
-func GetLocaleList() map[string]string {
+func GetLocaleList(base string) map[string]string {
        // TODO: This should be it's own translation file
        haveTranslatedNames := map[string]string{
                "ar":    "&#1593;&#1585;&#1576;&#1610;&#1577;&nbsp;(Arabiya)",
@@ -127,17 +128,17 @@ func GetLocaleList() map[string]string {
 
        // for all folders in locale which match a locale from 
https://www.transifex.com/api/2/languages/
        // use the language name unless we have an override
-       webLocales, err := FetchTranslationLocales()
+       webLocales, err := FetchTranslationLocales(base)
        if err != nil {
                log.Printf("Failed to get up to date language list, using 
fallback.")
                return haveTranslatedNames
        }
 
-       return GetInstalledLocales(webLocales, haveTranslatedNames)
+       return GetInstalledLocales(base, webLocales, haveTranslatedNames)
 }
 
-func FetchTranslationLocales() (map[string]locale, error) {
-       file, err := os.Open(os.ExpandEnv("${TORCHECKBASE}data/langs"))
+func FetchTranslationLocales(base string) (map[string]locale, error) {
+       file, err := os.Open(path.Join(base, "data/langs"))
        if err != nil {
                return nil, err
        }
@@ -164,8 +165,8 @@ func FetchTranslationLocales() (map[string]locale, error) {
 }
 
 // Get a list of all languages installed in our locale folder with 
translations if available
-func GetInstalledLocales(webLocales map[string]locale, nameTranslations 
map[string]string) map[string]string {
-       localFiles, err := ioutil.ReadDir(os.ExpandEnv("${TORCHECKBASE}locale"))
+func GetInstalledLocales(base string, webLocales map[string]locale, 
nameTranslations map[string]string) map[string]string {
+       localFiles, err := ioutil.ReadDir(path.Join(base, "locale"))
 
        if err != nil {
                log.Print("No locales found in 'locale'. Try running 'make 
i18n'.")



_______________________________________________
tor-commits mailing list
[email protected]
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to