From e864807341990f5c72e198d96740983bf7671584 Mon Sep 17 00:00:00 2001 From: thinkpadmaster Date: Thu, 13 Jul 2023 23:34:25 -0500 Subject: Move to multifile system and prepare for new file format --- errors.go | 23 +++++++ handle.go | 66 ++++++++++++++++++ load.go | 131 ++++++++++++++++++++++++++++++++++++ main.go | 49 ++++++++++++++ render.go | 20 ++++++ site.go | 228 -------------------------------------------------------------- 6 files changed, 289 insertions(+), 228 deletions(-) create mode 100644 errors.go create mode 100644 handle.go create mode 100644 load.go create mode 100644 main.go create mode 100644 render.go delete mode 100644 site.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..9406a9a --- /dev/null +++ b/errors.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "net/http" + "runtime/debug" +) + +func (app *application) serverError(w http.ResponseWriter, err error) { + trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack()) + app.errorLog.Output(2, trace) + + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +} + +func (app *application) clientError(w http.ResponseWriter, status int) { + app.errorLog.Printf("Clent error: %d\n", status) + http.Error(w, http.StatusText(status), status) +} + +func (app *application) notFound(w http.ResponseWriter) { + app.clientError(w, http.StatusNotFound) +} diff --git a/handle.go b/handle.go new file mode 100644 index 0000000..20fa259 --- /dev/null +++ b/handle.go @@ -0,0 +1,66 @@ +package main + +import ( + "net/http" + "strings" +) + +func (app *application) home(w http.ResponseWriter, r *http.Request) { + path := strings.Split(r.URL.Path, "/") + if path[1] != "" { + app.notFound(w) + return + } else { + p, err := app.loadPosts("html/projects", 3) + if err != nil { + app.serverError(w, err) + return + } + + err = renderTemplate(w, "main/index", p) + if err != nil { + app.serverError(w, err) + return + } + } +} + +func (app *application) about(w http.ResponseWriter, r *http.Request) { + err := renderTemplate(w, "main/about", nil) + if err != nil { + app.serverError(w, err) + return + } +} + +func (app *application) aggregate(w http.ResponseWriter, r *http.Request) { + p, err := app.loadPosts("html"+strings.TrimSuffix(r.URL.Path, "/"), -1) + if err != nil { + app.notFound(w) + } + + renderTemplate(w, "main/"+strings.TrimPrefix(strings.TrimSuffix(r.URL.Path, "/"), "/"), p) +} + +func (app *application) post(w http.ResponseWriter, r *http.Request) { + path := strings.Split(r.URL.Path, "/") + if path[2] == "" { + app.aggregate(w, r) + } else { + post, err := app.readFile("html" + strings.TrimSuffix(r.URL.Path, "/") + ".tmpl.html") + if err != nil { + app.notFound(w) + return + } + + var posts []*Post + posts = append(posts, post) + p := &Posts{Contents: posts} + + err = renderTemplate(w, path[1]+"/"+path[2], p) + if err != nil { + app.serverError(w, err) + return + } + } +} diff --git a/load.go b/load.go new file mode 100644 index 0000000..b6866e3 --- /dev/null +++ b/load.go @@ -0,0 +1,131 @@ +package main + +import ( + "os" + "regexp" + "sort" + "strings" +) + +type Post struct { + FileName string + Title string + Date string + Tags []string + Image string +} + +type Posts struct { + Contents []*Post +} + +func (p Post) containsTag(filterTag string) bool { + if filterTag == "" { + return true + } + + for _, tag := range p.Tags { + if filterTag == tag { + return true + } + } + + return false +} + +func (app *application) loadPosts(location string, postCount int) (p *Posts, err error) { + if postCount == 0 || postCount < -1 { + return nil, os.ErrInvalid + } + + var posts []*Post + + files, err := os.ReadDir(location) + if err != nil { + return nil, err + } + + for _, file := range files { + if !file.IsDir() && strings.HasSuffix(file.Name(), ".tmpl.html") { + newPost, err := app.readFile(location + "/" + file.Name()) + if err != nil { + return nil, err + } + /* + // filtering by tag + if !newPost.containsTag(filterTag) { + continue + } */ + + posts = append(posts, newPost) + } + } + + sort.Slice(posts, func(i, j int) bool { + return posts[i].Date > posts[j].Date + }) + + if postCount == -1 { + return &Posts{Contents: posts}, nil + } else if postCount < len(posts) { + return &Posts{Contents: posts[:postCount]}, nil + } else { + return &Posts{Contents: posts}, nil + } +} + +func (app *application) readFile(location string) (p *Post, err error) { + fileContent, err := os.ReadFile(location) + if err != nil { + return nil, err + } + + var tmp *Post = new(Post) + + fileName := strings.TrimSuffix(strings.Split(location, "/")[2], ".tmpl.html") + + // title + title := strings.ReplaceAll(fileName, "_", " ") + + // date + datePattern := regexp.MustCompile(`