From ade1fada32a4c9cc14af29050bc15528f319efdb Mon Sep 17 00:00:00 2001 From: Chris Mann Date: Fri, 21 Jul 2023 06:37:18 +0200 Subject: [PATCH] Refactoring into view --- Makefile | 2 +- http-utils.go | 37 +++++++++++++++++++++++++++++++++++ main.go | 53 +-------------------------------------------------- view/home.go | 30 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 53 deletions(-) create mode 100644 http-utils.go create mode 100644 view/home.go diff --git a/Makefile b/Makefile index a00d9ff..c2733ce 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BIN=guichet -SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go +SRC=main.go ssha.go profile.go admin.go invite.go directory.go utils.go picture.go login.go config.go http-utils.go view/home.go DOCKER=lxpz/guichet_amd64 all: $(BIN) diff --git a/http-utils.go b/http-utils.go new file mode 100644 index 0000000..eb62026 --- /dev/null +++ b/http-utils.go @@ -0,0 +1,37 @@ +/* +http-utils provide utility functions that interact with http +*/ + +package main + +import ( + "crypto/tls" + "net/http" + + "github.com/go-ldap/ldap/v3" +) + +func logRequest(handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) + handler.ServeHTTP(w, r) + }) +} + +func ldapOpen(w http.ResponseWriter) *ldap.Conn { + l, err := ldap.DialURL(config.LdapServerAddr) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return nil + } + + if config.LdapTLS { + err = l.StartTLS(&tls.Config{InsecureSkipVerify: true}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return nil + } + } + + return l +} diff --git a/main.go b/main.go index ddd0587..b9294d5 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,7 @@ package main import ( "crypto/rand" - "crypto/tls" + // "crypto/tls" // "encoding/json" "flag" @@ -20,7 +20,6 @@ import ( // "os" "strings" - "github.com/go-ldap/ldap/v3" "github.com/gorilla/mux" "github.com/gorilla/sessions" ) @@ -90,53 +89,3 @@ func main() { log.Fatal("Cannot start http server: ", err) } } - -func logRequest(handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL) - handler.ServeHTTP(w, r) - }) -} - -func ldapOpen(w http.ResponseWriter) *ldap.Conn { - l, err := ldap.DialURL(config.LdapServerAddr) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return nil - } - - if config.LdapTLS { - err = l.StartTLS(&tls.Config{InsecureSkipVerify: true}) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return nil - } - } - - return l -} - -// Page handlers ---- - -type HomePageData struct { - Login *LoginStatus - BaseDN string - Org string -} - -func handleHome(w http.ResponseWriter, r *http.Request) { - templateHome := getTemplate("home.html") - - login := checkLogin(w, r) - if login == nil { - return - } - - data := &HomePageData{ - Login: login, - BaseDN: config.BaseDN, - Org: config.Org, - } - - templateHome.Execute(w, data) -} diff --git a/view/home.go b/view/home.go new file mode 100644 index 0000000..76ce672 --- /dev/null +++ b/view/home.go @@ -0,0 +1,30 @@ +/* +home show the home page +*/ + +package main + +import "net/http" + +type HomePageData struct { + Login *LoginStatus + BaseDN string + Org string +} + +func handleHome(w http.ResponseWriter, r *http.Request) { + templateHome := getTemplate("home.html") + + login := checkLogin(w, r) + if login == nil { + return + } + + data := &HomePageData{ + Login: login, + BaseDN: config.BaseDN, + Org: config.Org, + } + + templateHome.Execute(w, data) +}