1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 08:08:59 +01:00

Server serves static files again

This commit is contained in:
Oliver Steele 2017-07-12 21:17:11 -04:00
parent 1d4c637965
commit 0c70a295c2
4 changed files with 33 additions and 31 deletions

View File

@ -51,13 +51,11 @@ func (s *Server) Run(open bool, logger func(label, value string)) error {
func (s *Server) handler(rw http.ResponseWriter, r *http.Request) {
s.mu.Lock()
defer s.mu.Unlock()
var (
site = s.Site
urlpath = r.URL.Path
site = s.Site
urlpath = r.URL.Path
p, found = site.URLPage(urlpath)
)
p, found := site.URLPage(urlpath)
if !found {
rw.WriteHeader(http.StatusNotFound)
log.Println("Not found:", urlpath)
@ -67,7 +65,6 @@ func (s *Server) handler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "404 page not found: %s", urlpath) // nolint: gas
return
}
mimeType := mime.TypeByExtension(p.OutputExt())
if mimeType != "" {
rw.Header().Set("Content-Type", mimeType)

View File

@ -52,5 +52,5 @@ func (s *Site) Build(options BuildOptions) (int, error) {
if err := s.Clean(options); err != nil {
return 0, err
}
return s.WritePages(options)
return s.WriteFiles(options)
}

View File

@ -42,8 +42,8 @@ func (s *Site) DestDir() string {
return filepath.Join(s.config.Source, s.config.Destination)
}
// OutputPages returns a list of output pages.
func (s *Site) OutputPages() []pages.Document {
// OutputDocs returns a list of output pages.
func (s *Site) OutputDocs() []pages.Document {
out := make([]pages.Document, 0, len(s.Routes))
for _, p := range s.Routes {
out = append(out, p)

View File

@ -13,14 +13,14 @@ import (
"github.com/osteele/gojekyll/utils"
)
// WritePages writes output files.
// WriteFiles writes output files.
// It attends to options.dry_run.
func (s *Site) WritePages(options BuildOptions) (count int, err error) {
func (s *Site) WriteFiles(options BuildOptions) (count int, err error) {
errs := make(chan error)
for _, p := range s.OutputPages() {
for _, p := range s.OutputDocs() {
count++
go func(p pages.Document) {
errs <- s.SavePage(p, options)
go func(d pages.Document) {
errs <- s.WriteDoc(d, options)
}(p)
}
var errList []error
@ -32,16 +32,16 @@ func (s *Site) WritePages(options BuildOptions) (count int, err error) {
return count, combineErrors(errList)
}
// SavePage writes a document to the destination directory.
// WriteDoc writes a document to the destination directory.
// It attends to options.dry_run.
func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
from := p.SourcePath()
to := filepath.Join(s.DestDir(), p.Permalink())
if !p.Static() && filepath.Ext(to) == "" {
func (s *Site) WriteDoc(d pages.Document, options BuildOptions) error {
from := d.SourcePath()
to := filepath.Join(s.DestDir(), d.Permalink())
if !d.Static() && filepath.Ext(to) == "" {
to = filepath.Join(to, "index.html")
}
if options.Verbose {
fmt.Println("create", to, "from", p.SourcePath())
fmt.Println("create", to, "from", d.SourcePath())
}
if options.DryRun {
// FIXME render the page, just don't write it
@ -52,29 +52,34 @@ func (s *Site) SavePage(p pages.Document, options BuildOptions) error {
return err
}
switch {
case p.Static() && options.UseHardLinks:
case d.Static() && options.UseHardLinks:
return os.Link(from, to)
case p.Static():
case d.Static():
return utils.CopyFileContents(to, from, 0644)
default:
return s.SaveDocumentToFile(p, to)
return utils.VisitCreatedFile(to, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
}
}
// SaveDocumentToFile writes a page to filename.
func (s *Site) SaveDocumentToFile(d pages.Document, filename string) error {
return utils.VisitCreatedFile(filename, func(w io.Writer) error {
return s.WriteDocument(w, d)
})
}
// WriteDocument writes the document to a writer.
func (s *Site) WriteDocument(w io.Writer, d pages.Document) error {
switch p := d.(type) {
case pages.Page:
return s.WritePage(w, p)
default:
return d.Write(w)
}
}
// WritePage writes the page to a writer.
func (s *Site) WritePage(w io.Writer, p pages.Page) error {
if err := s.prepareRendering(); err != nil {
return err
}
buf := new(bytes.Buffer)
if err := d.Write(buf); err != nil {
if err := p.Write(buf); err != nil {
return err
}
b := buf.Bytes()