From a84ac778adf57d6446eef6577e617a80d4c6bb8e Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 19 Jul 2022 15:39:49 +0200 Subject: Ease packaging by making resource path configurable --- main.go | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index d574f3f..51a23aa 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strings" "github.com/go-ldap/ldap/v3" @@ -19,6 +20,7 @@ import ( ) type ConfigFile struct { + Resources []string `json:"resources"` HttpBindAddr string `json:"http_bind_addr"` LdapServerAddr string `json:"ldap_server_addr"` LdapTLS bool `json:"ldap_tls"` @@ -62,6 +64,7 @@ var store sessions.Store = nil func readConfig() ConfigFile { // Default configuration values for certain fields config_file := ConfigFile{ + Resources: []string{}, HttpBindAddr: ":9991", LdapServerAddr: "ldap://127.0.0.1:389", @@ -91,13 +94,40 @@ func readConfig() ConfigFile { log.Fatal(err) } + // Enrich the Resource entry with default values + config_file.Resources = append(config_file.Resources, ".") + ex, err := os.Executable() + if err == nil { + exPath := filepath.Dir(ex) + config_file.Resources = append(config_file.Resources, exPath) + } + fmt.Println(config_file.Resources) + return config_file } +func selectResource(conf *ConfigFile) { + ResourceLoop: + for _, p := range conf.Resources { + for _, suffix := range []string{"", "/templates", "/static"} { + _, err := os.Stat(p + suffix) + if err != nil { + continue ResourceLoop + } + } + + // Success, this Resource folder is the one! + conf.Resources = []string{p} + return + } + log.Fatalf("Unable to find templates/ and static/ in the following paths: %s", conf.Resources) +} + func main() { flag.Parse() config_file := readConfig() + selectResource(&config_file) config = &config_file session_key := make([]byte, 32) @@ -127,7 +157,7 @@ func main() { r.HandleFunc("/admin/ldap/{dn}", handleAdminLDAP) r.HandleFunc("/admin/create/{template}/{super_dn}", handleAdminCreate) - staticfiles := http.FileServer(http.Dir("static")) + staticfiles := http.FileServer(http.Dir(config.Resources[0]+"/static")) r.Handle("/static/{file:.*}", http.StripPrefix("/static/", staticfiles)) log.Printf("Starting HTTP server on %s", config.HttpBindAddr) @@ -296,7 +326,7 @@ type HomePageData struct { } func handleHome(w http.ResponseWriter, r *http.Request) { - templateHome := template.Must(template.ParseFiles("templates/layout.html", "templates/home.html")) + templateHome := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/home.html")) login := checkLogin(w, r) if login == nil { @@ -338,7 +368,7 @@ type LoginFormData struct { } func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo { - templateLogin := template.Must(template.ParseFiles("templates/layout.html", "templates/login.html")) + templateLogin := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/login.html")) if r.Method == "GET" { templateLogin.Execute(w, LoginFormData{}) -- cgit v1.2.3