diff --git a/assets/assets.go b/assets/assets.go index cfb3e9b9..78ba09b5 100644 --- a/assets/assets.go +++ b/assets/assets.go @@ -2,6 +2,7 @@ // sources: // static/css/main.css // static/css/normalize.css +// static/js/app.js // templates/edit.tmpl // templates/footer.tmpl // templates/header.tmpl @@ -67,6 +68,24 @@ func staticCssNormalizeCss() (*asset, error) { return a, err } +// staticJsAppJs reads file data from disk. It returns an error on failure. +func staticJsAppJs() (*asset, error) { + path := "/home/henrique/Code/Go/src/github.com/hacdias/caddy-hugo/static/js/app.js" + name := "static/js/app.js" + bytes, err := bindataRead(path, name) + if err != nil { + return nil, err + } + + fi, err := os.Stat(path) + if err != nil { + err = fmt.Errorf("Error reading asset info %s at %s: %v", name, path, err) + } + + a := &asset{bytes: bytes, info: fi} + return a, err +} + // templatesEditTmpl reads file data from disk. It returns an error on failure. func templatesEditTmpl() (*asset, error) { path := "/home/henrique/Code/Go/src/github.com/hacdias/caddy-hugo/templates/edit.tmpl" @@ -175,6 +194,7 @@ func AssetNames() []string { var _bindata = map[string]func() (*asset, error){ "static/css/main.css": staticCssMainCss, "static/css/normalize.css": staticCssNormalizeCss, + "static/js/app.js": staticJsAppJs, "templates/edit.tmpl": templatesEditTmpl, "templates/footer.tmpl": templatesFooterTmpl, "templates/header.tmpl": templatesHeaderTmpl, @@ -227,6 +247,10 @@ var _bintree = &bintree{nil, map[string]*bintree{ "normalize.css": &bintree{staticCssNormalizeCss, map[string]*bintree{ }}, }}, + "js": &bintree{nil, map[string]*bintree{ + "app.js": &bintree{staticJsAppJs, map[string]*bintree{ + }}, + }}, }}, "templates": &bintree{nil, map[string]*bintree{ "edit.tmpl": &bintree{templatesEditTmpl, map[string]*bintree{ diff --git a/edit/edit.go b/edit/edit.go index 48dfc014..92ff90d2 100644 --- a/edit/edit.go +++ b/edit/edit.go @@ -6,29 +6,39 @@ import ( "os" "github.com/hacdias/caddy-hugo/page" + "github.com/spf13/hugo/commands" ) -type info struct { - File string +type fileInfo struct { + Content string + Name string } // Execute sth -func Execute(w http.ResponseWriter, r *http.Request, file string) (int, error) { +func Execute(w http.ResponseWriter, r *http.Request, filename string) (int, error) { if r.Method == "POST" { - // it's saving the post - } else { - if _, err := os.Stat(file); os.IsNotExist(err) { - return 404, nil - } - - file, err := ioutil.ReadFile(file) + r.ParseForm() + err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666) if err != nil { return 500, err } - inf := new(info) - inf.File = string(file) + commands.Execute() + } else { + if _, err := os.Stat(filename); os.IsNotExist(err) { + return 404, nil + } + + file, err := ioutil.ReadFile(filename) + + if err != nil { + return 500, err + } + + inf := new(fileInfo) + inf.Content = string(file) + inf.Name = filename page := new(page.Page) page.Title = "Edit" diff --git a/hugo.go b/hugo.go index 7d4554ca..98b07bc7 100644 --- a/hugo.go +++ b/hugo.go @@ -1,9 +1,11 @@ -//go:generate go-bindata -pkg assets -o assets/assets.go static/css/ templates/ +//go:generate go-bindata -pkg assets -o assets/assets.go static/css/ static/js/ templates/ package hugo import ( + "mime" "net/http" + "path/filepath" "strings" "github.com/hacdias/caddy-hugo/assets" @@ -57,13 +59,19 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) { } else if urlMatch(r, assetsURL) { // assets like css, javascript and images - fileName := strings.Replace(r.URL.Path, assetsURL, "static", 1) - file, err := assets.Asset(fileName) + filename := strings.Replace(r.URL.Path, assetsURL, "static", 1) + file, err := assets.Asset(filename) if err != nil { return 404, nil } + extension := filepath.Ext(filename) + mime := mime.TypeByExtension(extension) + + header := w.Header() + header.Set("Content-Type", mime) + w.Write(file) } else if r.URL.Path == mainURL || r.URL.Path == mainURL+"/" { // dashboard diff --git a/static/css/main.css b/static/css/main.css index e69de29b..14652374 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -0,0 +1,32 @@ +body { + font-family: 'Roboto', sans-serif; + padding-top: 3em; +} + +/* SITE HEADER */ + +.site-header { + height: 3em; + width: 100%; + background-color: #263238; + color: #fff; + padding: 0 2em; + box-sizing: border-box; + position: fixed; + top: 0; + left: 0; +} + +.content { + margin: 1.5em auto; + width: 80%; + max-width: 960px; +} + +textarea { + width: 100%; + min-height: 50em; + resize: vertical; + border: 0; + font-family: inherit; +} diff --git a/static/js/app.js b/static/js/app.js new file mode 100644 index 00000000..ddce029d --- /dev/null +++ b/static/js/app.js @@ -0,0 +1 @@ +$("textarea").resizable(); diff --git a/templates/edit.tmpl b/templates/edit.tmpl index d95a6160..6acf4bcd 100644 --- a/templates/edit.tmpl +++ b/templates/edit.tmpl @@ -1,12 +1,16 @@ {{#HEADER#}} - {{ with .Body }} -
+