models_pages and controllers_pages

posts
serr 2025-04-10 21:50:10 +03:00
parent cbce343e51
commit 5a34d18ed6
10 changed files with 22 additions and 22 deletions

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"log" "log"
"main/mvc/controllers" "main/mvc/controllers"
controllers_pages "main/mvc/controllers/pages" "main/mvc/controllers/controllers_pages"
"main/mvc/models" "main/mvc/models"
models_pages "main/mvc/models/pages" "main/mvc/models/models_pages"
"main/tools" "main/tools"
"net/http" "net/http"
) )

View File

@ -1,10 +1,10 @@
package controllers package controllers_pages
import ( import (
"log" "log"
"main/mvc/models" "main/mvc/models"
models_pages "main/mvc/models/pages" "main/mvc/models/models_pages"
"main/tools" "main/tools"
"net/http" "net/http"
) )

View File

@ -1,8 +1,8 @@
package controllers package controllers_pages
import ( import (
"main/mvc/models" "main/mvc/models"
models_pages "main/mvc/models/pages" "main/mvc/models/models_pages"
"net/http" "net/http"
) )

View File

@ -1,8 +1,8 @@
package controllers package controllers_pages
import ( import (
"main/mvc/models" "main/mvc/models"
models_pages "main/mvc/models/pages" "main/mvc/models/models_pages"
"net/http" "net/http"
) )

View File

@ -1,8 +1,8 @@
package controllers package controllers_pages
import ( import (
"main/mvc/models" "main/mvc/models"
models_pages "main/mvc/models/pages" "main/mvc/models/models_pages"
"net/http" "net/http"
) )

View File

@ -6,7 +6,7 @@ import (
"os" "os"
) )
type Config struct { type config struct {
PostsDir string PostsDir string
AssetsDir string AssetsDir string
TemplatesDir string TemplatesDir string
@ -20,16 +20,16 @@ type Config struct {
} }
var ( var (
Cfg = Config{} Cfg = config{}
) )
func init() { func init() {
if err := Cfg.Load("config.json"); err != nil { if err := Cfg.load("config.json"); err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
} }
func (c *Config) Load(configPath string) error { func (c *config) load(configPath string) error {
configFile, err := os.ReadFile(configPath) configFile, err := os.ReadFile(configPath)
if err != nil { if err != nil {
return err return err

View File

@ -1,4 +1,4 @@
package models package models_pages
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package models package models_pages
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package models package models_pages
import ( import (
"bytes" "bytes"
@ -36,7 +36,7 @@ func RenderPostPage(templates *template.Template, version int64, data template.H
return pageData.Bytes(), nil return pageData.Bytes(), nil
} }
func NewPost(link string, data []byte) *Post { func newPost(link string, data []byte) *Post {
previewBuf := make([]byte, 0, 503) previewBuf := make([]byte, 0, 503)
if len(data) > 500 { if len(data) > 500 {

View File

@ -1,4 +1,4 @@
package models package models_pages
import ( import (
"bytes" "bytes"
@ -26,7 +26,7 @@ var (
) )
func init() { func init() {
if err := LoadPosts(models.Cfg.PostsDir); err != nil { if err := loadPosts(models.Cfg.PostsDir); err != nil {
log.Fatalf("%v", err) log.Fatalf("%v", err)
} }
} }
@ -35,7 +35,7 @@ func GetPosts() Posts {
return posts return posts
} }
func LoadPosts(dir string) error { func loadPosts(dir string) error {
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -54,7 +54,7 @@ func LoadPosts(dir string) error {
html := tools.MdToHTML(md) html := tools.MdToHTML(md)
link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".md")) link := fmt.Sprintf("/%s/", strings.TrimSuffix(filepath.Base(path), ".md"))
posts[PostLink(link)] = NewPost(link, html) posts[PostLink(link)] = newPost(link, html)
} }
return nil return nil
}) })