Routing, templating, and database handling
This commit is contained in:
parent
38352de02f
commit
e73c006550
28
controllers/getController.go
Normal file
28
controllers/getController.go
Normal 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)
|
||||||
|
}
|
42
controllers/postController.go
Normal file
42
controllers/postController.go
Normal 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)
|
||||||
|
}
|
33
database/databaseConnection.go
Normal file
33
database/databaseConnection.go
Normal 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
24
routes/getRoutes.go
Normal 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
17
routes/postRoutes.go
Normal 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
13
templates/base.html
Normal 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>
|
5
templates/pages/home.html
Normal file
5
templates/pages/home.html
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{{ define "pageTitle" }}Home{{ end }}
|
||||||
|
|
||||||
|
{{ define "content" }}
|
||||||
|
{{ .Test }}
|
||||||
|
{{ end }}
|
11
templates/pages/login.html
Normal file
11
templates/pages/login.html
Normal 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 }}
|
11
templates/pages/register.html
Normal file
11
templates/pages/register.html
Normal 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 }}
|
20
templating/templateHelper.go
Normal file
20
templating/templateHelper.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user