From e73c0065508e4d630c8f9b7b51d61dd6cdb8794f Mon Sep 17 00:00:00 2001 From: max Date: Thu, 20 Oct 2022 11:59:33 -0500 Subject: [PATCH] Routing, templating, and database handling --- controllers/getController.go | 28 +++++++++++++++++++++++ controllers/postController.go | 42 ++++++++++++++++++++++++++++++++++ database/databaseConnection.go | 33 ++++++++++++++++++++++++++ routes/getRoutes.go | 24 +++++++++++++++++++ routes/postRoutes.go | 17 ++++++++++++++ templates/base.html | 13 +++++++++++ templates/pages/home.html | 5 ++++ templates/pages/login.html | 11 +++++++++ templates/pages/register.html | 11 +++++++++ templating/templateHelper.go | 20 ++++++++++++++++ 10 files changed, 204 insertions(+) create mode 100644 controllers/getController.go create mode 100644 controllers/postController.go create mode 100644 database/databaseConnection.go create mode 100644 routes/getRoutes.go create mode 100644 routes/postRoutes.go create mode 100644 templates/base.html create mode 100644 templates/pages/home.html create mode 100644 templates/pages/login.html create mode 100644 templates/pages/register.html create mode 100644 templating/templateHelper.go diff --git a/controllers/getController.go b/controllers/getController.go new file mode 100644 index 0000000..5a2aeed --- /dev/null +++ b/controllers/getController.go @@ -0,0 +1,28 @@ +package controllers + +import ( + "GoWeb/app" + "GoWeb/templating" + "net/http" +) + +// GetController is a wrapper struct for the App struct +type GetController struct { + App *app.App +} + +func (getController *GetController) ShowHome(w http.ResponseWriter, r *http.Request) { + type dataStruct struct { + Test string + } + + data := dataStruct{ + Test: "Hello World!", + } + + templating.RenderTemplate(getController.App, w, "templates/pages/home.html", data) +} + +func (getController *GetController) ShowRegister(w http.ResponseWriter, r *http.Request) { + templating.RenderTemplate(getController.App, w, "templates/pages/register.html", nil) +} diff --git a/controllers/postController.go b/controllers/postController.go new file mode 100644 index 0000000..7031d07 --- /dev/null +++ b/controllers/postController.go @@ -0,0 +1,42 @@ +package controllers + +import ( + "GoWeb/app" + "GoWeb/database/models" + "log" + "net/http" + "time" +) + +// PostController is a wrapper struct for the App struct +type PostController struct { + App *app.App +} + +func (postController *PostController) Register(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + createdAt := time.Now() + updatedAt := time.Now() + + if username == "" || password == "" { + log.Println("Tried to create user with empty username or password") + http.Redirect(w, r, "/register", http.StatusFound) + } + + models.CreateUser(postController.App, username, password, createdAt, updatedAt) + + http.Redirect(w, r, "/login", http.StatusFound) +} + +func (postController *PostController) Login(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + + if username == "" || password == "" { + log.Println("Tried to create user with empty username or password") + http.Redirect(w, r, "/register", http.StatusFound) + } + + http.Redirect(w, r, "/login", http.StatusFound) +} diff --git a/database/databaseConnection.go b/database/databaseConnection.go new file mode 100644 index 0000000..7802000 --- /dev/null +++ b/database/databaseConnection.go @@ -0,0 +1,33 @@ +package database + +import ( + "GoWeb/app" + "database/sql" + "fmt" + _ "github.com/lib/pq" + "log" +) + +// ConnectDB returns a new database connection +func ConnectDB(app *app.App) *sql.DB { + // Set connection parameters from config + postgresConfig := fmt.Sprintf("host=%s port=%s user=%s "+ + "password=%s dbname=%s sslmode=disable", + app.Config.Db.Ip, app.Config.Db.Port, app.Config.Db.User, app.Config.Db.Password, app.Config.Db.Name) + + // Create connection + db, err := sql.Open("postgres", postgresConfig) + if err != nil { + panic(err) + } + + // Test connection + err = db.Ping() + if err != nil { + panic(err) + } + + log.Println("Connected to database successfully on " + app.Config.Db.Ip + ":" + app.Config.Db.Port + " using database " + app.Config.Db.Name) + + return db +} diff --git a/routes/getRoutes.go b/routes/getRoutes.go new file mode 100644 index 0000000..c7a946f --- /dev/null +++ b/routes/getRoutes.go @@ -0,0 +1,24 @@ +package routes + +import ( + "GoWeb/app" + "GoWeb/controllers" + "log" + "net/http" +) + +// GetRoutes defines all project get routes +func GetRoutes(app *app.App) { + // Get controller struct initialize + getController := controllers.GetController{ + App: app, + } + + // Serve static files + http.Handle("/file/", http.FileServer(http.Dir("./static"))) + log.Println("Serving static files from: ./static") + + // Pages + http.HandleFunc("/", getController.ShowHome) + http.HandleFunc("/register", getController.ShowRegister) +} diff --git a/routes/postRoutes.go b/routes/postRoutes.go new file mode 100644 index 0000000..b082b47 --- /dev/null +++ b/routes/postRoutes.go @@ -0,0 +1,17 @@ +package routes + +import ( + "GoWeb/app" + "GoWeb/controllers" + "net/http" +) + +func PostRoutes(app *app.App) { + // Get controller struct initialize + postController := controllers.PostController{ + App: app, + } + + http.HandleFunc("/register-handle", postController.Register) + http.HandleFunc("/login-handle", postController.Login) +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..30b537a --- /dev/null +++ b/templates/base.html @@ -0,0 +1,13 @@ + + + + + SiteName - {{ template "pageTitle" }} + + +{{ template "content" . }} + + + \ No newline at end of file diff --git a/templates/pages/home.html b/templates/pages/home.html new file mode 100644 index 0000000..4236a8f --- /dev/null +++ b/templates/pages/home.html @@ -0,0 +1,5 @@ +{{ define "pageTitle" }}Home{{ end }} + +{{ define "content" }} +{{ .Test }} +{{ end }} \ No newline at end of file diff --git a/templates/pages/login.html b/templates/pages/login.html new file mode 100644 index 0000000..d2aadad --- /dev/null +++ b/templates/pages/login.html @@ -0,0 +1,11 @@ +{{ define "pageTitle" }}Login{{ end }} + +{{ define "content" }} +
+
+

+
+

+ +
+{{ end }} diff --git a/templates/pages/register.html b/templates/pages/register.html new file mode 100644 index 0000000..d54e0c4 --- /dev/null +++ b/templates/pages/register.html @@ -0,0 +1,11 @@ +{{ define "pageTitle" }}Register{{ end }} + +{{ define "content" }} +
+
+

+
+

+ +
+{{ end }} diff --git a/templating/templateHelper.go b/templating/templateHelper.go new file mode 100644 index 0000000..8ca0039 --- /dev/null +++ b/templating/templateHelper.go @@ -0,0 +1,20 @@ +package templating + +import ( + "GoWeb/app" + "html/template" + "log" + "net/http" +) + +func RenderTemplate(app *app.App, w http.ResponseWriter, contentPath string, data any) { + templatePath := app.Config.Template.BaseName + + t, _ := template.ParseFiles(templatePath, contentPath) + err := t.Execute(w, data) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), 500) + return + } +}