Files
eephttpd/serve.go

99 lines
1.9 KiB
Go
Raw Normal View History

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-13 00:48:30 -04:00
"log"
2019-09-02 00:23:03 -04:00
"net/http"
2019-10-13 15:51:22 -04:00
"os"
2019-09-02 01:06:57 -04:00
"path/filepath"
"strings"
2019-09-13 00:48:30 -04:00
"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
}
2019-09-13 00:48:30 -04:00
if strings.HasSuffix(rq.URL.Path, ".tengo") {
2019-09-02 20:04:20 -04:00
f.HandleScript(rw, rq)
2019-09-13 00:48:30 -04:00
return
2019-09-02 20:04:20 -04:00
}
2019-09-02 01:06:57 -04:00
f.HandleFile(rw, rq)
2018-08-24 01:06:39 -04:00
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
if info != nil {
return !info.IsDir()
}
return false
}
2019-09-13 00:48:30 -04:00
func (f *EepHttpd) checkURL(rq *http.Request) string {
p := rq.URL.Path
if rq.URL.Path == "/" {
p = "/index.html"
}
2019-10-13 15:51:22 -04:00
if !FileExists(filepath.Join(f.ServeDir, p)) {
p = "/README.md"
2019-10-13 15:51:22 -04:00
}
if !FileExists(filepath.Join(f.ServeDir, p)) {
if FileExists(filepath.Join(f.ServeDir, "/index.tengo")) {
p = "/index.tengo"
}
}
2019-09-02 20:04:20 -04:00
log.Println(p)
2019-09-13 00:48:30 -04:00
return filepath.Join(f.ServeDir, p)
2019-09-02 20:04:20 -04:00
}
func (f *EepHttpd) HandleScript(rw http.ResponseWriter, rq *http.Request) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 20:04:20 -04:00
bytes, err := ioutil.ReadFile(path)
if err != nil {
2019-09-13 00:48:30 -04:00
log.Println(err)
2019-09-02 20:04:20 -04:00
return
}
2019-09-13 00:48:30 -04:00
scr := script.New(bytes)
com, err := scr.Compile()
2019-09-02 20:04:20 -04:00
if err != nil {
2019-09-13 00:48:30 -04:00
log.Println(err)
2019-09-02 20:04:20 -04:00
panic(err)
}
2019-09-13 00:48:30 -04:00
if err := com.Run(); err != nil {
log.Println(err)
2019-09-02 20:04:20 -04:00
panic(err)
}
2019-09-13 00:48:30 -04:00
response := com.Get("response")
fmt.Fprintf(rw, response.String())
2019-09-02 20:04:20 -04:00
}
2019-09-02 00:23:03 -04:00
func (f *EepHttpd) HandleMarkdown(rw http.ResponseWriter, rq *http.Request) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 01:06:57 -04:00
bytes, err := ioutil.ReadFile(path)
if err != nil {
return
}
f.mark.Render(rw, bytes)
}
func (f *EepHttpd) HandleFile(rw http.ResponseWriter, rq *http.Request) {
2019-09-02 20:04:20 -04:00
path := f.checkURL(rq)
2019-09-02 01:06:57 -04:00
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) {
2019-09-13 00:48:30 -04:00
path := f.checkURL(rq)
2019-09-02 20:04:20 -04:00
fmt.Fprintf(rw, "ERROR %s NOT FOUND", strings.Replace(path, f.ServeDir, "", -1))
2018-08-24 01:06:39 -04:00
}