Routing, templating, and database handling

This commit is contained in:
max 2022-10-20 11:59:33 -05:00
parent 38352de02f
commit e73c006550
10 changed files with 204 additions and 0 deletions

View File

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

View File

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

View File

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

24
routes/getRoutes.go Normal file
View File

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

17
routes/postRoutes.go Normal file
View File

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

13
templates/base.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SiteName - {{ template "pageTitle" }}</title>
</head>
<body>
{{ template "content" . }}
</body>
<footer>
<p>SiteName - Powered by Go!</p>
</footer>
</html>

View File

@ -0,0 +1,5 @@
{{ define "pageTitle" }}Home{{ end }}
{{ define "content" }}
{{ .Test }}
{{ end }}

View File

@ -0,0 +1,11 @@
{{ define "pageTitle" }}Login{{ end }}
{{ define "content" }}
<form action="/login-handle" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" value="John"><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
{{ end }}

View File

@ -0,0 +1,11 @@
{{ define "pageTitle" }}Register{{ end }}
{{ define "content" }}
<form action="/register-handle" method="post">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username" value="John"><br><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
{{ end }}

View File

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