Commits

Yann Malet  committed 3069b38

Use env variable to specify the path to the templates and the statc files

  • Participants
  • Parent commits 17fae6d

Comments (0)

Files changed (2)

File run_http_server.go

 
 import (
 	"bitbucket.org/yml/gowebexp/web"
-	"fmt"
+	"log"
 	"net/http"
 )
 
+const (
+	ADDRESS = ":8080"
+)
+
 func main() {
+
 	web.App.SetRoute()
 	// Start serving
-	fmt.Println("Starting server on : http://127.0.0.1:8080")
+	log.Println("Starting server on: ", ADDRESS)
 	http.Handle("/", web.App)
 
 	// Serving static file
-	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(web.App.Path+"/web/static/"))))
+	http.Handle("/static/",
+		http.StripPrefix("/static/", http.FileServer(http.Dir(web.App.StaticDir))))
 
-	http.ListenAndServe(":8080", nil)
+	err := http.ListenAndServe(ADDRESS, nil)
+	if err != nil {
+		log.Fatal("An error occured when trying to server gowebexp: \n", err)
+	}
 }
 package web
 
 import (
+	"bitbucket.org/yml/gowebexp/pages"
+	"crypto/rand"
+	"encoding/base64"
 	"fmt"
 	"github.com/gorilla/context"
 	"github.com/gorilla/mux"
 	"github.com/gorilla/sessions"
 	"html/template"
+	"log"
 	"net/http"
-	//"os"
-	"bitbucket.org/yml/gowebexp/pages"
-	"crypto/rand"
-	"encoding/base64"
+	"os"
 	"path/filepath"
 )
 
-// TODO: Use flag to set parameter: IP, PORT, path
+// Helper function to check the existence of a file or a directory
+func Exists(name string) bool {
+	if _, err := os.Stat(name); err != nil {
+		if os.IsNotExist(err) {
+			log.Fatal(name, " does not exist.")
+		}
+		return false
+	}
+	return true
+}
 
 // Implement a sigleton Pattern
 type WebApp struct {
 	Router      *mux.Router
 	Storage     Storage
-	Path        string
+	StaticDir   string
+	TemplateDir string
 	CookieStore *sessions.CookieStore
 }
 
 	if err != nil {
 		fmt.Println("An error occured while creating the storage : ", err)
 	}
-	//path, err := os.Getwd()
-	//if err != nil {
-	//	fmt.Println("An error occured while trying to retireve the current path", err)
-	//}
-	path := "/home/yml/Dropbox/Devs/golang/gorilla_on_web/src/gowebexp/"
+
+	// Read the configuration from the environ variable
+	StaticDir := os.Getenv("GOWEBEXP_STATIC")
+	if StaticDir == "" || !Exists(StaticDir) {
+		log.Println("StaticDir: ", StaticDir)
+		log.Fatal("`export GOWEBEXP_STATIC=<path>` must be set in the environ")
+	}
+	TemplateDir := os.Getenv("GOWEBEXP_TEMPLATES")
+	if TemplateDir == "" || !Exists(TemplateDir) {
+		log.Println("TemplateDir: ", TemplateDir)
+		log.Fatal("`export GOWEBEXP_TEMPLATE=<path>` must be set in the environ")
+	}
+
 	app := &WebApp{
 		Router:      mux.NewRouter(),
 		Storage:     storage,
-		Path:        path,
+		StaticDir:   StaticDir,
+		TemplateDir: TemplateDir,
 		CookieStore: sessions.NewCookieStore([]byte("something-very-secret"))}
 	return app
 }
 
 func (app *WebApp) Template(name string) string {
-	return filepath.Join(app.Path, "web", "templates", name)
+	return filepath.Join(app.TemplateDir, name)
 }
 
 func (app *WebApp) GenerateCsrfToken() string {