Skip to content

Commit

Permalink
refactor: server routing
Browse files Browse the repository at this point in the history
taking advantage of improvement of server routing from go 1.22
https://go.dev/blog/routing-enhancements
  • Loading branch information
drawbu committed Jun 10, 2024
1 parent 44f69e1 commit e88fe04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ var (
func main() {
serv := app.Server{Port: 8080, AssetsDir: assetsDir}

serv.AddRoute("/", homepage.Handler)
serv.AddRoute("GET /", homepage.Handler)
blog := blog.Handler{}
go blog.FetchArticles()
serv.AddRoute("/blog/{article...}", blog.Render)
serv.AddRoute("/contact", contact.Handler)
serv.AddRoute("/resume", resume.Handler)
serv.ServeRoute("/assets/", "/assets/")
serv.AddRoute("GET /blog/{article...}", blog.Render)
serv.AddRoute("GET /contact", contact.Handler)
serv.AddRoute("GET /resume", resume.Handler)
serv.ServeRoute("GET /assets/", "/assets/")

serv.Run()
}
28 changes: 16 additions & 12 deletions pkg/routes/blog/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
Expand All @@ -27,16 +28,16 @@ type article struct {
Title string
Date string
Path string
URI string
Content []byte
}

func (h *Handler) Render(serv *app.Server, w http.ResponseWriter, r *http.Request) error {
if r.URL.Path == "/blog" || r.URL.Path == "/blog/" {
article_name, err := url.PathUnescape(r.PathValue("article"))
if err != nil || article_name == "" {
return components.Template(blog(h.Articles)).Render(context.Background(), w)
}

a, err := findArticle(h.Articles, r.URL.Path)
a, err := findArticle(h.Articles, "/" + article_name)
if err != nil {
return err
}
Expand All @@ -60,7 +61,7 @@ func (h *Handler) fetch() {
return
}
h.RepoPath = path
h.Articles = getArticles(h.RepoPath, h.RepoPath)
h.Articles = getArticles("", h.RepoPath)
return
}

Expand All @@ -76,7 +77,7 @@ func (h *Handler) fetch() {

func findArticle(articles []article, path string) (article, error) {
for _, a := range articles {
if a.URI == path {
if a.Path == path {
return a, nil
}
}
Expand Down Expand Up @@ -114,8 +115,9 @@ func cloneRepo(repo string) (string, error) {
return dirname, nil
}

func getArticles(path string, basepath string) []article {
entries, err := os.ReadDir(path)
func getArticles(path string, repo_path string) []article {
fullpath := repo_path + "/" + path
entries, err := os.ReadDir(fullpath)
if err != nil {
return []article{}
}
Expand All @@ -127,20 +129,22 @@ func getArticles(path string, basepath string) []article {
}

if entry.IsDir() {
articles = append(articles, getArticles(path+"/"+entry.Name(), basepath)...)
articles = append(articles, getArticles(path+"/"+entry.Name(), repo_path)...)
continue
}
if strings.HasSuffix(entry.Name(), ".md") {
name := strings.TrimSuffix(entry.Name(), ".md")
uri := "/blog" + strings.TrimPrefix(path, basepath) + "/" + name
filepath := path + "/" + entry.Name()
articles = append(articles, article{Title: name, Path: filepath, URI: uri, Content: getContent(filepath)})
articles = append(articles, article{
Title: name,
Path: path + "/" + name,
Content: parseMarkdownArticle(fullpath + "/" + entry.Name()),
})
}
}
return articles
}

func getContent(path string) []byte {
func parseMarkdownArticle(path string) []byte {
file, err := os.ReadFile(path)
if err != nil {
return []byte{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/routes/blog/blog.templ
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ templ blog(articles []article) {
<ul>
for _, article := range articles {
<li>
<a href={ templ.URL(article.URI) }>{ article.Title }</a>
<a href={ "/blog/" + templ.URL(article.Path) }>{ article.Title }</a>
</li>
}
</ul>
Expand Down

0 comments on commit e88fe04

Please sign in to comment.