diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2022-07-19 15:39:49 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2022-07-19 15:39:49 +0200 |
commit | a84ac778adf57d6446eef6577e617a80d4c6bb8e (patch) | |
tree | 5c622349b2784cbe7c3fd7fa901758c2b161b091 | |
parent | b41630e941b44a8fffe09f42d9a7efb728fcbfa1 (diff) | |
download | guichet-a84ac778adf57d6446eef6577e617a80d4c6bb8e.tar.gz guichet-a84ac778adf57d6446eef6577e617a80d4c6bb8e.zip |
Ease packaging by making resource path configurable
-rw-r--r-- | admin.go | 8 | ||||
-rw-r--r-- | default.nix | 8 | ||||
-rw-r--r-- | directory.go | 4 | ||||
-rw-r--r-- | invite.go | 8 | ||||
-rw-r--r-- | main.go | 36 | ||||
-rw-r--r-- | profile.go | 4 |
6 files changed, 49 insertions, 19 deletions
@@ -48,7 +48,7 @@ type AdminUsersTplData struct { } func handleAdminUsers(w http.ResponseWriter, r *http.Request) { - templateAdminUsers := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_users.html")) + templateAdminUsers := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/admin_users.html")) login := checkAdminLogin(w, r) if login == nil { @@ -87,7 +87,7 @@ type AdminGroupsTplData struct { } func handleAdminGroups(w http.ResponseWriter, r *http.Request) { - templateAdminGroups := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_groups.html")) + templateAdminGroups := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/admin_groups.html")) login := checkAdminLogin(w, r) if login == nil { @@ -165,7 +165,7 @@ type PropValues struct { } func handleAdminLDAP(w http.ResponseWriter, r *http.Request) { - templateAdminLDAP := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_ldap.html")) + templateAdminLDAP := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/admin_ldap.html")) login := checkAdminLogin(w, r) if login == nil { @@ -551,7 +551,7 @@ type CreateData struct { } func handleAdminCreate(w http.ResponseWriter, r *http.Request) { - templateAdminCreate := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_create.html")) + templateAdminCreate := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/admin_create.html")) login := checkAdminLogin(w, r) if login == nil { diff --git a/default.nix b/default.nix index 336e58d..24b759c 100644 --- a/default.nix +++ b/default.nix @@ -12,7 +12,7 @@ let ]; }; bin = pkgs.gomod.buildGoApplication { - pname = "bottin-bin"; + pname = "guichet-bin"; version = "0.1.0"; src = ./.; modules = ./gomod2nix.toml; @@ -20,15 +20,15 @@ let CGO_ENABLED=0; meta = with pkgs.lib; { - description = "Bottin is a cloud-native LDAP server backed by a Consul datastore"; - homepage = "https://git.deuxfleurs.fr/Deuxfleurs/bottin"; + description = "Interface web pour gérer le LDAP: changer son mot de passe, ses infos de profil, inviter des gens, administration"; + homepage = "https://git.deuxfleurs.fr/Deuxfleurs/guichet"; license = licenses.gpl3Plus; platforms = platforms.linux; }; }; in pkgs.stdenv.mkDerivation { - pname = "bottin"; + pname = "guichet"; version = "0.1.0"; src = ./.; diff --git a/directory.go b/directory.go index ab5dea3..4f4a5a2 100644 --- a/directory.go +++ b/directory.go @@ -13,7 +13,7 @@ const FIELD_NAME_PROFILE_PICTURE = "profilePicture" const FIELD_NAME_DIRECTORY_VISIBILITY = "directoryVisibility" func handleDirectory(w http.ResponseWriter, r *http.Request) { - templateDirectory := template.Must(template.ParseFiles("templates/layout.html", "templates/directory.html")) + templateDirectory := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/directory.html")) login := checkLogin(w, r) if login == nil { @@ -37,7 +37,7 @@ type SearchResults struct { } func handleDirectorySearch(w http.ResponseWriter, r *http.Request) { - templateDirectoryResults := template.Must(template.ParseFiles("templates/directory_results.html")) + templateDirectoryResults := template.Must(template.ParseFiles(config.Resources[0]+"/templates/directory_results.html")) //Get input value by user r.ParseMultipartForm(1024) @@ -60,7 +60,7 @@ func handleInvitationCode(w http.ResponseWriter, r *http.Request) { inviteDn := config.InvitationNameAttr + "=" + code_id + "," + config.InvitationBaseDN err := l.Bind(inviteDn, code_pw) if err != nil { - templateInviteInvalidCode := template.Must(template.ParseFiles("templates/layout.html", "templates/invite_invalid_code.html")) + templateInviteInvalidCode := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/invite_invalid_code.html")) templateInviteInvalidCode.Execute(w, nil) return } @@ -110,7 +110,7 @@ type NewAccountData struct { } func handleNewAccount(w http.ResponseWriter, r *http.Request, l *ldap.Conn, invitedBy string) bool { - templateInviteNewAccount := template.Must(template.ParseFiles("templates/layout.html", "templates/invite_new_account.html")) + templateInviteNewAccount := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/invite_new_account.html")) data := &NewAccountData{} @@ -239,7 +239,7 @@ type CodeMailFields struct { } func handleInviteSendCode(w http.ResponseWriter, r *http.Request) { - templateInviteSendCode := template.Must(template.ParseFiles("templates/layout.html", "templates/invite_send_code.html")) + templateInviteSendCode := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/invite_send_code.html")) login := checkInviterLogin(w, r) if login == nil { @@ -298,7 +298,7 @@ func trySendCode(login *LoginStatus, choice string, sendto string, data *SendCod return } - templateMail := template.Must(template.ParseFiles("templates/invite_mail.txt")) + templateMail := template.Must(template.ParseFiles(config.Resources[0]+"/templates/invite_mail.txt")) buf := bytes.NewBuffer([]byte{}) templateMail.Execute(buf, &CodeMailFields{ To: sendto, @@ -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{}) @@ -22,7 +22,7 @@ type ProfileTplData struct { } func handleProfile(w http.ResponseWriter, r *http.Request) { - templateProfile := template.Must(template.ParseFiles("templates/layout.html", "templates/profile.html")) + templateProfile := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/profile.html")) login := checkLogin(w, r) if login == nil { @@ -97,7 +97,7 @@ type PasswdTplData struct { } func handlePasswd(w http.ResponseWriter, r *http.Request) { - templatePasswd := template.Must(template.ParseFiles("templates/layout.html", "templates/passwd.html")) + templatePasswd := template.Must(template.ParseFiles(config.Resources[0]+"/templates/layout.html", config.Resources[0]+"/templates/passwd.html")) login := checkLogin(w, r) if login == nil { |