Ease packaging by making resource path configurable
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Quentin 2022-07-19 15:39:49 +02:00
parent b41630e941
commit a84ac778ad
Signed by: quentin
GPG Key ID: E9602264D639FF68
6 changed files with 49 additions and 19 deletions

View File

@ -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 {

View File

@ -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 = ./.;

View File

@ -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)

View File

@ -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,

36
main.go
View File

@ -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{})

View File

@ -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 {