Compare commits
5 Commits
v1.5.1
...
dc450e26dd
Author | SHA1 | Date | |
---|---|---|---|
dc450e26dd | |||
de4a217c5f | |||
c4e83d06b9 | |||
51da24be9b | |||
e497f4d2f0 |
@ -2,7 +2,6 @@ package controllers
|
||||
|
||||
import (
|
||||
"GoWeb/app"
|
||||
"GoWeb/models"
|
||||
"GoWeb/security"
|
||||
"GoWeb/templating"
|
||||
"net/http"
|
||||
@ -15,7 +14,8 @@ type Get struct {
|
||||
|
||||
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
|
||||
type dataStruct struct {
|
||||
Test string
|
||||
CsrfToken string
|
||||
Test string
|
||||
}
|
||||
|
||||
data := dataStruct{
|
||||
@ -58,8 +58,3 @@ func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
templating.RenderTemplate(w, "templates/pages/login.html", data)
|
||||
}
|
||||
|
||||
func (g *Get) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
models.LogoutUser(g.App, w, r)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
|
@ -50,3 +50,8 @@ func (p *Post) Register(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
http.Redirect(w, r, "/login", http.StatusFound)
|
||||
}
|
||||
|
||||
func (p *Post) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
models.LogoutUser(p.App, w, r)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ import (
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Migrate given a dummy object of any type, it will create a table with the same name as the type and create columns with the same name as the fields of the object
|
||||
// Migrate given a dummy object of any type, it will create a table with the same name
|
||||
// as the type and create columns with the same name as the fields of the object
|
||||
func Migrate(app *app.App, anyStruct interface{}) error {
|
||||
valueOfStruct := reflect.ValueOf(anyStruct)
|
||||
typeOfStruct := valueOfStruct.Type()
|
||||
@ -23,10 +24,15 @@ func Migrate(app *app.App, anyStruct interface{}) error {
|
||||
for i := 0; i < valueOfStruct.NumField(); i++ {
|
||||
fieldType := typeOfStruct.Field(i)
|
||||
fieldName := fieldType.Name
|
||||
if fieldName != "Id" && fieldName != "id" {
|
||||
err := createColumn(app, tableName, fieldName, fieldType.Type.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
// Create column if dummy for migration is NOT zero value
|
||||
fieldValue := valueOfStruct.Field(i).Interface()
|
||||
if !reflect.ValueOf(fieldValue).IsZero() {
|
||||
if fieldName != "Id" && fieldName != "id" {
|
||||
err := createColumn(app, tableName, fieldName, fieldType.Type.Name())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -1,8 +1,8 @@
|
||||
module GoWeb
|
||||
|
||||
go 1.21
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/lib/pq v1.10.9
|
||||
golang.org/x/crypto v0.17.0
|
||||
golang.org/x/crypto v0.19.0
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -1,4 +1,4 @@
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
||||
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
|
@ -17,7 +17,7 @@ type Session struct {
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
const sessionColumnsNoId = "\"UserId\", \"AuthToken\",\"RememberMe\", \"CreatedAt\""
|
||||
const sessionColumnsNoId = "\"UserId\", \"AuthToken\", \"RememberMe\", \"CreatedAt\""
|
||||
const sessionColumns = "\"Id\", " + sessionColumnsNoId
|
||||
const sessionTable = "public.\"Session\""
|
||||
|
||||
|
@ -26,8 +26,7 @@ func Get(app *app.App) {
|
||||
slog.Info("serving static files from embedded file system /static")
|
||||
|
||||
// Pages
|
||||
http.HandleFunc("/", getController.ShowHome)
|
||||
http.HandleFunc("/login", getController.ShowLogin)
|
||||
http.HandleFunc("/register", getController.ShowRegister)
|
||||
http.HandleFunc("/logout", getController.Logout)
|
||||
http.HandleFunc("GET /", getController.ShowHome)
|
||||
http.HandleFunc("GET /login", getController.ShowLogin)
|
||||
http.HandleFunc("GET /register", getController.ShowRegister)
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ func Post(app *app.App) {
|
||||
}
|
||||
|
||||
// User authentication
|
||||
http.HandleFunc("/register-handle", middleware.Csrf(postController.Register))
|
||||
http.HandleFunc("/login-handle", middleware.Csrf(postController.Login))
|
||||
http.HandleFunc("POST /register-handle", middleware.Csrf(postController.Register))
|
||||
http.HandleFunc("POST /login-handle", middleware.Csrf(postController.Login))
|
||||
http.HandleFunc("POST /logout", middleware.Csrf(postController.Logout))
|
||||
}
|
||||
|
Reference in New Issue
Block a user