This commit is contained in:
@@ -17,20 +17,20 @@ func LoginGetHandler(c *gin.Context) {
|
||||
logger := slog.Get(c)
|
||||
logger.Info("LoginGetHandler", globals.UserKey, user)
|
||||
if user != nil {
|
||||
c.HTML(http.StatusBadRequest, "login.html", gin.H{
|
||||
c.HTML(http.StatusBadRequest, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "Please logout first",
|
||||
"message": "Please logout first",
|
||||
"user": user,
|
||||
})
|
||||
return
|
||||
}
|
||||
c.HTML(http.StatusOK, "login.html", gin.H{
|
||||
c.HTML(http.StatusOK, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "",
|
||||
"message": "",
|
||||
"user": user,
|
||||
})
|
||||
}
|
||||
@@ -41,11 +41,11 @@ func LoginPostHandler(c *gin.Context) {
|
||||
logger := slog.Get(c)
|
||||
logger.Info("LoginPostHandler", "user", user)
|
||||
if user != nil {
|
||||
c.HTML(http.StatusBadRequest, "login.html", gin.H{
|
||||
c.HTML(http.StatusBadRequest, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "Please logout first",
|
||||
"message": "Please logout first",
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -54,30 +54,30 @@ func LoginPostHandler(c *gin.Context) {
|
||||
password := c.PostForm("password")
|
||||
|
||||
if helpers.EmptyUserPass(username, password) {
|
||||
c.HTML(http.StatusBadRequest, "login.html", gin.H{
|
||||
c.HTML(http.StatusBadRequest, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "Parameters can't be empty",
|
||||
"message": "Parameters can't be empty",
|
||||
})
|
||||
}
|
||||
|
||||
if !helpers.CheckUserPass(username, password) {
|
||||
c.HTML(http.StatusUnauthorized, "login.html", gin.H{
|
||||
c.HTML(http.StatusUnauthorized, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "Incorrect username or password",
|
||||
"message": "Incorrect username or password",
|
||||
})
|
||||
}
|
||||
|
||||
session.Set(globals.UserKey, username)
|
||||
if err := session.Save(); err != nil {
|
||||
c.HTML(http.StatusInternalServerError, "login.html", gin.H{
|
||||
c.HTML(http.StatusInternalServerError, "login", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"login": globals.LoginUrl,
|
||||
"content": "Failed to save session",
|
||||
"message": "Failed to save session",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -18,11 +18,11 @@ func ListComicsGetHandler(c *gin.Context) {
|
||||
var comics []schema.Comic
|
||||
|
||||
result := schema.DB.Find(&comics)
|
||||
logger.Info("found Comics", "count", result.RowsAffected)
|
||||
c.HTML(http.StatusOK, "comics.html", gin.H{
|
||||
logger.Info("ListComicsGetHandler", "count", result.RowsAffected)
|
||||
c.HTML(http.StatusOK, "comics", gin.H{
|
||||
"title": "Kontor Comics",
|
||||
"version": "0.4.0",
|
||||
"content": "Media Files...",
|
||||
"message": "Comics...",
|
||||
"comics": comics,
|
||||
"user": user,
|
||||
})
|
||||
|
||||
@@ -69,7 +69,7 @@ func ListComicStoryArcs(c *gin.Context) {
|
||||
|
||||
logger := slog.Get(c)
|
||||
result := schema.DB.Find(&items)
|
||||
logger.Info("found Comic StoryArcs", "count", result.RowsAffected)
|
||||
logger.Info("ListComicStoryArcs", "count", result.RowsAffected)
|
||||
c.JSON(http.StatusOK, items)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,10 @@ func IndexGetHandler(c *gin.Context) {
|
||||
user := session.Get(globals.UserKey)
|
||||
logger := slog.Get(c)
|
||||
logger.Info("IndexGetHandler", globals.UserKey, user)
|
||||
c.HTML(http.StatusOK, "home.html", gin.H{
|
||||
c.HTML(http.StatusOK, "home", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"content": "This is an index page...",
|
||||
"message": "This is an index page...",
|
||||
"login": globals.LoginUrl,
|
||||
"logout": globals.LogoutUrl,
|
||||
"user": user,
|
||||
|
||||
@@ -3,7 +3,6 @@ package controllers
|
||||
import (
|
||||
"kontor-api-gin/pkg/globals"
|
||||
"kontor-api-gin/pkg/schema"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
@@ -13,11 +12,12 @@ import (
|
||||
|
||||
func ListMediaFiles(c *gin.Context) {
|
||||
var files []schema.MediaFile
|
||||
logger := slog.Get(c)
|
||||
|
||||
// query := schema.DB.Model(&schema.MediaFile{})
|
||||
// result := query.Find(&files)
|
||||
result := schema.DB.Find(&files)
|
||||
log.Printf("found %d MediaFiles", result.RowsAffected)
|
||||
logger.Info("ListMediaFiles", "count", result.RowsAffected)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "All MediaFiles",
|
||||
"files": files,
|
||||
@@ -26,9 +26,10 @@ func ListMediaFiles(c *gin.Context) {
|
||||
|
||||
func ListMediaLoFis(c *gin.Context) {
|
||||
var lofis []schema.MediaLoFi
|
||||
logger := slog.Get(c)
|
||||
|
||||
result := schema.DB.Find(&lofis)
|
||||
log.Printf("found %d MediaLoFis", result.RowsAffected)
|
||||
logger.Info("ListMediaLoFis", "count", result.RowsAffected)
|
||||
c.JSON(http.StatusOK, lofis)
|
||||
}
|
||||
|
||||
@@ -41,10 +42,10 @@ func ListMediaActorsGetHandler(c *gin.Context) {
|
||||
|
||||
result := schema.DB.Find(&actors)
|
||||
logger.Info("ListMediaActorsGetHandler", "count", result.RowsAffected)
|
||||
c.HTML(http.StatusOK, "mediaactors.html", gin.H{
|
||||
c.HTML(http.StatusOK, "mediaactors", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"content": "Media Actors...",
|
||||
"message": "Media Actors...",
|
||||
"actors": actors,
|
||||
"login": globals.LoginUrl,
|
||||
"logout": globals.LogoutUrl,
|
||||
@@ -61,10 +62,10 @@ func ListMediaFilesGetHandler(c *gin.Context) {
|
||||
|
||||
result := schema.DB.Find(&files)
|
||||
logger.Info("ListMediaFilesGetHandler", "count", result.RowsAffected)
|
||||
c.HTML(http.StatusOK, "mediafiles.html", gin.H{
|
||||
c.HTML(http.StatusOK, "mediafiles", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"content": "Media Files...",
|
||||
"message": "Media Files...",
|
||||
"files": files,
|
||||
"login": globals.LoginUrl,
|
||||
"logout": globals.LogoutUrl,
|
||||
@@ -82,10 +83,11 @@ func ListMediaLoFisGetHandler(c *gin.Context) {
|
||||
|
||||
result := schema.DB.Find(&lofis)
|
||||
logger.Info("ListMediaLoFisGetHandler", "count", result.RowsAffected)
|
||||
c.HTML(http.StatusOK, "media_lofi.html", gin.H{
|
||||
logger.Info("ListMediaLoFisGetHandler", "medialofi", lofis[0])
|
||||
c.HTML(http.StatusOK, "lofi", gin.H{
|
||||
"title": "Kontor Gin",
|
||||
"version": "0.4.0",
|
||||
"content": "Media LoFis...",
|
||||
"message": "Media LoFis...",
|
||||
"lofis": lofis,
|
||||
"login": globals.LoginUrl,
|
||||
"logout": globals.LogoutUrl,
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func InitConfig() error {
|
||||
viper.SetConfigName("kontor-gin")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath(".")
|
||||
viper.AddConfigPath("$HOME/.config")
|
||||
viper.AddConfigPath("/etc/kontor")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
// Read the config file
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
// Handle the case where config file is not found
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
slog.Info("No config file found, using defaults")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("error reading config file: %w", err)
|
||||
}
|
||||
|
||||
slog.Info("initConfig", "Using config file: ", viper.ConfigFileUsed())
|
||||
return nil
|
||||
}
|
||||
@@ -6,6 +6,6 @@ const UserKey = "user"
|
||||
|
||||
const Version = "0.4.0"
|
||||
|
||||
const LoginUrl = "/auth/login"
|
||||
const LoginUrl = "/login"
|
||||
|
||||
const LogoutUrl = "/auth/logout"
|
||||
|
||||
@@ -27,7 +27,7 @@ func MediaRoutes(g *gin.Engine) {
|
||||
media.GET("/lofis", controllers.ListMediaLoFisGetHandler)
|
||||
}
|
||||
|
||||
func ProtectedRoutes(g *gin.Engine) {
|
||||
func AuthRoutes(g *gin.Engine) {
|
||||
auth := g.Group("/auth")
|
||||
auth.Use(middleware.AuthRequired)
|
||||
auth.GET("/logout", controllers.LogoutGetHandler)
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package schema
|
||||
|
||||
type MediaLoFi struct {
|
||||
Base
|
||||
import "time"
|
||||
|
||||
WebLink string `gorm:"uniqueIndex" json:"url"`
|
||||
Review bool `json:"review"`
|
||||
ShouldDownload bool `json:"should_download"`
|
||||
Title string `gorm:"size:300" json:"title"`
|
||||
FileName string `json:"file_name"`
|
||||
type MediaLoFi struct {
|
||||
ID string `gorm:"type:string;primary_key;"`
|
||||
Created_Date time.Time `json:"created_date"`
|
||||
Last_Modified_date time.Time `json:"last_modified_date"`
|
||||
Deleted_Date *time.Time `json:"deleted_date"`
|
||||
Version int `gorm:"default:0" json:"version"`
|
||||
Url string `gorm:"uniqueIndex" json:"url"`
|
||||
Review bool `json:"review"`
|
||||
ShouldDownload bool `json:"should_download"`
|
||||
Title string `gorm:"size:300" json:"title"`
|
||||
FileName string `json:"file_name"`
|
||||
}
|
||||
|
||||
func (media_lofi *MediaLoFi) TableName() string {
|
||||
|
||||
Reference in New Issue
Block a user