diff --git a/cmd/inn.go b/cmd/inn.go index d76c612..612cd40 100644 --- a/cmd/inn.go +++ b/cmd/inn.go @@ -12,8 +12,8 @@ import ( func main() { variables := options.GetVar() - resPath := variables.Get("res-path").(string) - dataPath := variables.Get("data-path").(string) + resPath := variables.GetString("res-path") + dataPath := variables.GetString("data-path") var resources fs.FS if resPath == "embed" { resources = res.GetEmbedFS() diff --git a/server/auth.go b/server/auth.go index 8b22c3e..d487967 100644 --- a/server/auth.go +++ b/server/auth.go @@ -20,7 +20,7 @@ func login(c *gin.Context) { c.JSON(http.StatusBadRequest, gin.H{"error": "Parameters can't be empty"}) return } - if username != config.Get("username").(string) || password != config.Get("password").(string) { + if username != config.Get("username").(string) || password != config.GetString("password") { c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication failed"}) return } diff --git a/server/handlers.go b/server/handlers.go index 4e885e9..b74f434 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -18,7 +18,7 @@ import ( func showIndex(c *gin.Context) { dbConn := c.MustGet("dbConn").(*sql.DB) variables := c.MustGet("variables").(*viper.Viper) - pageLength := variables.Get("page-length").(int) + pageLength := variables.GetInt("page-length") posts := db.RetrievePage(dbConn, 1, pageLength) c.HTML(http.StatusOK, "index.tmpl", gin.H{ "Posts": posts, @@ -33,9 +33,8 @@ func showPosts(c *gin.Context) { } dbConn := c.MustGet("dbConn").(*sql.DB) variables := c.MustGet("variables").(*viper.Viper) - pageLength := variables.Get("page-length").(int) + pageLength := variables.GetInt("page-length") posts := db.RetrievePage(dbConn, pageNumber, pageLength) - c.HTML(http.StatusOK, "posts.tmpl", gin.H{ "Posts": posts, }) @@ -65,7 +64,7 @@ func getPosts(c *gin.Context) { } dbConn := c.MustGet("dbConn").(*sql.DB) variables := c.MustGet("variables").(*viper.Viper) - pageLength := variables.Get("page-length").(int) + pageLength := variables.GetInt("page-length") posts := db.RetrievePage(dbConn, pageNumber, pageLength) c.JSON(http.StatusOK, posts) } @@ -91,7 +90,7 @@ func createPost(c *gin.Context) { if err != nil { log.Println(err) } - dataPath := variables.Get("data-path").(string) + dataPath := variables.GetString("data-path") files := form.File["files"] var filename string if len(files) != 0 { diff --git a/server/server.go b/server/server.go index 791ab7d..64102db 100644 --- a/server/server.go +++ b/server/server.go @@ -16,7 +16,7 @@ import ( ) func Run(resources fs.FS, variables *viper.Viper, config *viper.Viper) { - dbConn := db.Open(variables.Get("data-path").(string)) + dbConn := db.Open(variables.GetString("data-path")) defer dbConn.Close() db.CreateTable(dbConn) r := gin.New() @@ -27,13 +27,13 @@ func Run(resources fs.FS, variables *viper.Viper, config *viper.Viper) { log.Fatalln(err) } r.StaticFS("/static", http.FS(staticRoot)) - imagesRoot, err := fs.Sub(os.DirFS(variables.Get("data-path").(string)), "images") + imagesRoot, err := fs.Sub(os.DirFS(variables.GetString("data-path")), "images") if err != nil { log.Fatalln(err) } r.StaticFS("/images", http.FS(imagesRoot)) r.Use(Middleware(dbConn, variables, config)) - r.Use(sessions.Sessions("mysession", cookie.NewStore([]byte((config.Get("cookiekey").(string)))))) + r.Use(sessions.Sessions("mysession", cookie.NewStore([]byte((config.GetString("cookiekey")))))) api := r.Group("/api") api.GET("/posts/:page", getPosts) api.GET("post/:id", getPost)