feat: complete go rewrite
This commit is contained in:
49
internal/handlers/auth.go
Normal file
49
internal/handlers/auth.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rideaware/admin-panel/internal/database"
|
||||
"github.com/rideaware/admin-panel/internal/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func LoginGet(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "login.html", gin.H{})
|
||||
}
|
||||
|
||||
func LoginPost(c *gin.Context) {
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
|
||||
admin, err := database.GetAdmin(username)
|
||||
if err != nil || !database.VerifyPassword(admin.Password, password) {
|
||||
c.HTML(http.StatusUnauthorized, "login.html",
|
||||
gin.H{"error": "Invalid username or password"})
|
||||
return
|
||||
}
|
||||
|
||||
session, err := middleware.GetStore().Get(c.Request, "session")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
session.Values["username"] = username
|
||||
if err := session.Save(c.Request, c.Writer); err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, "/")
|
||||
}
|
||||
|
||||
func Logout(c *gin.Context) {
|
||||
session, err := middleware.GetStore().Get(c.Request, "session")
|
||||
if err == nil {
|
||||
session.Options.MaxAge = -1
|
||||
session.Save(c.Request, c.Writer)
|
||||
}
|
||||
c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
28
internal/handlers/newsletter.go
Normal file
28
internal/handlers/newsletter.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rideaware/admin-panel/internal/email"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SendUpdateGet(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "send_update.html", gin.H{})
|
||||
}
|
||||
|
||||
func SendUpdatePost(c *gin.Context) {
|
||||
subject := c.PostForm("subject")
|
||||
body := c.PostForm("body")
|
||||
|
||||
message, err := email.SendUpdate(subject, body)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusOK, "send_update.html",
|
||||
gin.H{"error": message})
|
||||
return
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "send_update.html",
|
||||
gin.H{"success": message})
|
||||
}
|
||||
19
internal/handlers/subscribers.go
Normal file
19
internal/handlers/subscribers.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rideaware/admin-panel/internal/database"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func IndexGet(c *gin.Context) {
|
||||
emails, err := database.GetAllEmails()
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
c.HTML(http.StatusOK, "admin_index.html",
|
||||
gin.H{"emails": emails})
|
||||
}
|
||||
Reference in New Issue
Block a user