fix: trim root directory prefix

This commit is contained in:
Hoang Nguyen 2023-11-20 00:00:00 +07:00
parent 2ace4b61bf
commit f493874e4b
Signed by: folliehiyuki
GPG Key ID: B0567C20730E9B11
2 changed files with 14 additions and 18 deletions

25
main.go
View File

@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"time"
"golang.org/x/sync/errgroup"
@ -17,13 +18,10 @@ import (
var (
ctx = context.Background()
previewPort = "8080"
// Specify the starting directory, for consistency
_, f, _, _ = runtime.Caller(0)
)
// genListingPages generates listing pages recursively inside provided directory
func genListingPages(path string) error {
func genListingPages(path, prefix string) error {
entries, err := os.ReadDir(path)
if err != nil {
return err
@ -33,7 +31,9 @@ func genListingPages(path string) error {
if err != nil {
return err
}
if err := listingPage(path, entries).Render(ctx, indexFile); err != nil {
trimedPath := strings.TrimPrefix(path, prefix)
if err := listingPage(strings.TrimPrefix(trimedPath, "/"), entries).Render(ctx, indexFile); err != nil {
return err
}
@ -43,7 +43,7 @@ func genListingPages(path string) error {
entry := entry
if entry.IsDir() {
errGroup.Go(func() error {
return genListingPages(filepath.Join(path, entry.Name()))
return genListingPages(filepath.Join(path, entry.Name()), prefix)
})
}
}
@ -52,11 +52,7 @@ func genListingPages(path string) error {
// preview Previews the website at specified root directory on localhost:<port>
func preview(root, port string) error {
http.Handle("/", http.FileServer(
http.Dir(
filepath.Join(filepath.Dir(f), root),
),
))
http.Handle("/", http.FileServer(http.Dir(root)))
fmt.Println("Serving the preview webpage at http://localhost:" + port)
srv := &http.Server{
Addr: ":" + port,
@ -69,7 +65,7 @@ func preview(root, port string) error {
// generate generates index.html files for the root directory
func generate(root string) error {
// NOTE: it also generate one for the root directory. We'll override this file later
if err := genListingPages(root); err != nil {
if err := genListingPages(root, root); err != nil {
return err
}
@ -101,7 +97,10 @@ Options:
if len(flag.Args()) != 1 {
log.Fatal("[ERROR] exactly 1 argument DIR is required")
}
rootDir := flag.Arg(0)
// Specify the starting directory, for consistency
_, f, _, _ := runtime.Caller(0)
rootDir := filepath.Join(filepath.Dir(f), flag.Arg(0))
if *genFlag {
if err := generate(rootDir); err != nil {

View File

@ -2,10 +2,7 @@ package main
import "io/fs"
var (
siteName = "FollieCDN"
siteURL = "https://cdn.folliehiyuki.com/"
)
var siteName = "FollieCDN"
templ pageTemplate(path string) {
<html lang="en-us">
@ -24,7 +21,7 @@ templ pageTemplate(path string) {
<meta name="description" content={ siteName }/>
}
<link rel="icon" type="image/x-icon" href="/favicon.ico"/>
<link rel="canonical" href={ siteURL + path }/>
<link rel="canonical" href={ "https://cdn.folliehiyuki.com/" + path }/>
<link rel="stylesheet" href="/styles/normalize.css" integrity="sha256-Atknw9eu6T9FV6v//wFp8skKdJ5JcAqANQ1bPykR4go=" crossorigin="anonymous"/>
<link rel="stylesheet" href="/styles/self.css" integrity="sha256-KReR+W3bFJ+Eu2X+F0lR+bULlbmaLS8SXUhIQJ/p/p4=" crossorigin="anonymous"/>
<link rel="stylesheet" href="/fonts/iosevka/iosevka-aile.css" integrity="sha256-20HRMpRlRW2+dk9R7asoOl5/z8Xyc2BbjeLZsButUww=" crossorigin="anonymous"/>