Compare commits
	
		
			8 Commits
		
	
	
		
			v1.5.1
			...
			6d6aff50b3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					6d6aff50b3 | ||
| 
						 | 
					a6be73765a | ||
| 
						 | 
					ddc9e51831 | ||
| 
						 | 
					dc450e26dd | ||
| 
						 | 
					de4a217c5f | ||
| 
						 | 
					c4e83d06b9 | ||
| 
						 | 
					51da24be9b | ||
| 
						 | 
					e497f4d2f0 | 
@@ -13,13 +13,28 @@ type Get struct {
 | 
				
			|||||||
	App *app.App
 | 
						App *app.App
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
 | 
					func (g *Get) ShowHome(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	type dataStruct struct {
 | 
						type dataStruct struct {
 | 
				
			||||||
 | 
							CsrfToken       string
 | 
				
			||||||
 | 
							IsAuthenticated bool
 | 
				
			||||||
		Test            string
 | 
							Test            string
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						isAuthenticated := true
 | 
				
			||||||
 | 
						user, err := models.CurrentUser(g.App, r)
 | 
				
			||||||
 | 
						if err != nil || user.Id == 0 {
 | 
				
			||||||
 | 
							isAuthenticated = false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data := dataStruct{
 | 
						data := dataStruct{
 | 
				
			||||||
 | 
							CsrfToken:       CsrfToken,
 | 
				
			||||||
		Test:            "Hello World!",
 | 
							Test:            "Hello World!",
 | 
				
			||||||
 | 
							IsAuthenticated: isAuthenticated,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	templating.RenderTemplate(w, "templates/pages/home.html", data)
 | 
						templating.RenderTemplate(w, "templates/pages/home.html", data)
 | 
				
			||||||
@@ -28,6 +43,7 @@ func (g *Get) ShowHome(w http.ResponseWriter, _ *http.Request) {
 | 
				
			|||||||
func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
					func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	type dataStruct struct {
 | 
						type dataStruct struct {
 | 
				
			||||||
		CsrfToken       string
 | 
							CsrfToken       string
 | 
				
			||||||
 | 
							IsAuthenticated bool
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
						CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
				
			||||||
@@ -35,8 +51,15 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						isAuthenticated := true
 | 
				
			||||||
 | 
						user, err := models.CurrentUser(g.App, r)
 | 
				
			||||||
 | 
						if err != nil || user.Id == 0 {
 | 
				
			||||||
 | 
							isAuthenticated = false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data := dataStruct{
 | 
						data := dataStruct{
 | 
				
			||||||
		CsrfToken:       CsrfToken,
 | 
							CsrfToken:       CsrfToken,
 | 
				
			||||||
 | 
							IsAuthenticated: isAuthenticated,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	templating.RenderTemplate(w, "templates/pages/register.html", data)
 | 
						templating.RenderTemplate(w, "templates/pages/register.html", data)
 | 
				
			||||||
@@ -45,6 +68,7 @@ func (g *Get) ShowRegister(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
					func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
	type dataStruct struct {
 | 
						type dataStruct struct {
 | 
				
			||||||
		CsrfToken       string
 | 
							CsrfToken       string
 | 
				
			||||||
 | 
							IsAuthenticated bool
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
						CsrfToken, err := security.GenerateCsrfToken(w, r)
 | 
				
			||||||
@@ -58,8 +82,3 @@ func (g *Get) ShowLogin(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	templating.RenderTemplate(w, "templates/pages/login.html", data)
 | 
						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)
 | 
						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"
 | 
						"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 {
 | 
					func Migrate(app *app.App, anyStruct interface{}) error {
 | 
				
			||||||
	valueOfStruct := reflect.ValueOf(anyStruct)
 | 
						valueOfStruct := reflect.ValueOf(anyStruct)
 | 
				
			||||||
	typeOfStruct := valueOfStruct.Type()
 | 
						typeOfStruct := valueOfStruct.Type()
 | 
				
			||||||
@@ -23,6 +24,10 @@ func Migrate(app *app.App, anyStruct interface{}) error {
 | 
				
			|||||||
	for i := 0; i < valueOfStruct.NumField(); i++ {
 | 
						for i := 0; i < valueOfStruct.NumField(); i++ {
 | 
				
			||||||
		fieldType := typeOfStruct.Field(i)
 | 
							fieldType := typeOfStruct.Field(i)
 | 
				
			||||||
		fieldName := fieldType.Name
 | 
							fieldName := fieldType.Name
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 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" {
 | 
								if fieldName != "Id" && fieldName != "id" {
 | 
				
			||||||
				err := createColumn(app, tableName, fieldName, fieldType.Type.Name())
 | 
									err := createColumn(app, tableName, fieldName, fieldType.Type.Name())
 | 
				
			||||||
				if err != nil {
 | 
									if err != nil {
 | 
				
			||||||
@@ -30,6 +35,7 @@ func Migrate(app *app.App, anyStruct interface{}) error {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,8 +1,8 @@
 | 
				
			|||||||
module GoWeb
 | 
					module GoWeb
 | 
				
			||||||
 | 
					
 | 
				
			||||||
go 1.21
 | 
					go 1.22
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/lib/pq v1.10.9
 | 
						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 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
 | 
				
			||||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
 | 
					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.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
 | 
				
			||||||
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
 | 
					golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,7 @@ func RunAllMigrations(app *app.App) error {
 | 
				
			|||||||
		Id:         1,
 | 
							Id:         1,
 | 
				
			||||||
		UserId:     1,
 | 
							UserId:     1,
 | 
				
			||||||
		AuthToken:  "migrate",
 | 
							AuthToken:  "migrate",
 | 
				
			||||||
		RememberMe: false,
 | 
							RememberMe: true, // Booleans must be true to migrate properly
 | 
				
			||||||
		CreatedAt:  time.Now(),
 | 
							CreatedAt:  time.Now(),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	err = database.Migrate(app, session)
 | 
						err = database.Migrate(app, session)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,12 +22,11 @@ func Get(app *app.App) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	staticHandler := http.FileServer(http.FS(staticFS))
 | 
						staticHandler := http.FileServer(http.FS(staticFS))
 | 
				
			||||||
	http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
 | 
						http.Handle("GET /static/", http.StripPrefix("/static/", staticHandler))
 | 
				
			||||||
	slog.Info("serving static files from embedded file system /static")
 | 
						slog.Info("serving static files from embedded file system /static")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Pages
 | 
						// Pages
 | 
				
			||||||
	http.HandleFunc("/", getController.ShowHome)
 | 
						http.HandleFunc("GET /", getController.ShowHome)
 | 
				
			||||||
	http.HandleFunc("/login", getController.ShowLogin)
 | 
						http.HandleFunc("GET /login", getController.ShowLogin)
 | 
				
			||||||
	http.HandleFunc("/register", getController.ShowRegister)
 | 
						http.HandleFunc("GET /register", getController.ShowRegister)
 | 
				
			||||||
	http.HandleFunc("/logout", getController.Logout)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,6 +15,7 @@ func Post(app *app.App) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// User authentication
 | 
						// User authentication
 | 
				
			||||||
	http.HandleFunc("/register-handle", middleware.Csrf(postController.Register))
 | 
						http.HandleFunc("POST /register-handle", middleware.Csrf(postController.Register))
 | 
				
			||||||
	http.HandleFunc("/login-handle", middleware.Csrf(postController.Login))
 | 
						http.HandleFunc("POST /login-handle", middleware.Csrf(postController.Login))
 | 
				
			||||||
 | 
						http.HandleFunc("POST /logout", middleware.Csrf(postController.Logout))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,21 @@
 | 
				
			|||||||
    <link href="/static/css/style.css" rel="stylesheet">
 | 
					    <link href="/static/css/style.css" rel="stylesheet">
 | 
				
			||||||
</head>
 | 
					</head>
 | 
				
			||||||
<body>
 | 
					<body>
 | 
				
			||||||
 | 
					<div class="navbar">
 | 
				
			||||||
 | 
					    {{ if .IsAuthenticated }}
 | 
				
			||||||
 | 
					    <form action="/logout" method="post">
 | 
				
			||||||
 | 
					        <input name="csrf_token" type="hidden" value="{{ .CsrfToken }}">
 | 
				
			||||||
 | 
					        <input type="submit" value="Logout">
 | 
				
			||||||
 | 
					    </form>
 | 
				
			||||||
 | 
					    {{ else }}
 | 
				
			||||||
 | 
					    <form action="/login" method="get">
 | 
				
			||||||
 | 
					        <input type="submit" value="Login">
 | 
				
			||||||
 | 
					    </form>
 | 
				
			||||||
 | 
					    <form action="/register" method="get">
 | 
				
			||||||
 | 
					        <input type="submit" value="Register">
 | 
				
			||||||
 | 
					    </form>
 | 
				
			||||||
 | 
					    {{ end }}
 | 
				
			||||||
 | 
					</div>
 | 
				
			||||||
{{ template "content" . }}
 | 
					{{ template "content" . }}
 | 
				
			||||||
<div class="footer-container">
 | 
					<div class="footer-container">
 | 
				
			||||||
    <footer>
 | 
					    <footer>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user