2019-09-02 00:23:03 -04:00
|
|
|
package eephttpd
|
2018-08-24 01:06:39 -04:00
|
|
|
|
|
|
|
import (
|
2019-09-02 01:06:57 -04:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-09-02 00:23:03 -04:00
|
|
|
"net/http"
|
2019-09-02 01:06:57 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/d5/tengo/script"
|
2018-08-24 01:06:39 -04:00
|
|
|
)
|
|
|
|
|
2019-09-02 00:23:03 -04:00
|
|
|
func (f *EepHttpd) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
|
2019-09-02 01:06:57 -04:00
|
|
|
if strings.HasSuffix(rq.URL.Path, ".md") {
|
|
|
|
f.HandleMarkdown(rw, rq)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Eventually I'll give this some simple scriptable capabilities.
|
|
|
|
if strings.HasSuffix(rq.URL.Path, ".tengo") {
|
|
|
|
r.HandleScript(rw, rq)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
f.HandleFile(rw, rq)
|
2018-08-24 01:06:39 -04:00
|
|
|
}
|
|
|
|
|
2019-09-02 00:23:03 -04:00
|
|
|
func (f *EepHttpd) HandleMarkdown(rw http.ResponseWriter, rq *http.Request) {
|
2019-09-02 01:06:57 -04:00
|
|
|
path := filepath.Join(f.ServeDir, rq.URL.Path)
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.mark.Render(rw, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EepHttpd) HandleFile(rw http.ResponseWriter, rq *http.Request) {
|
|
|
|
path := filepath.Join(f.ServeDir, rq.URL.Path)
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
f.HandleMissing(rw, rq)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(rw, string(bytes))
|
|
|
|
}
|
2018-08-24 01:06:39 -04:00
|
|
|
|
2019-09-02 01:06:57 -04:00
|
|
|
func (f *EepHttpd) HandleMissing(rw http.ResponseWriter, rq *http.Request) {
|
|
|
|
path := filepath.Join(f.ServeDir, rq.URL.Path)
|
|
|
|
fmt.Fprintf(rw, "ERROR %s NOT FOUND", path)
|
2018-08-24 01:06:39 -04:00
|
|
|
}
|