Initialize blog scaffold
Add the CLI, site, and sample content so the project can run locally. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9d2628b318
commit
b78f4b39c9
40 changed files with 9140 additions and 0 deletions
45
backend/internal/staticserver/server.go
Normal file
45
backend/internal/staticserver/server.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package staticserver
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Serve(dir string, host string, port string) error {
|
||||
info, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("static path %s is not a directory", dir)
|
||||
}
|
||||
|
||||
addr := net.JoinHostPort(host, port)
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := filepath.Clean("/" + r.URL.Path)
|
||||
if path == "/" {
|
||||
path = "/index.html"
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(dir, filepath.FromSlash(path))
|
||||
if info, err := os.Stat(fullPath); err == nil && info.IsDir() {
|
||||
fullPath = filepath.Join(fullPath, "index.html")
|
||||
} else if errors.Is(err, os.ErrNotExist) && filepath.Ext(fullPath) == "" {
|
||||
fullPath = filepath.Join(dir, filepath.FromSlash(path), "index.html")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(fullPath); err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.ServeFile(w, r, fullPath)
|
||||
})
|
||||
|
||||
fmt.Printf("serving %s at http://%s\n", dir, addr)
|
||||
return http.ListenAndServe(addr, handler)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue