Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/

.idea
.vscode
45 changes: 45 additions & 0 deletions form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<html>
<head>
<title>Create page</title>
<style>
html, body {
height: 100%;
width: 100%;
}

#inputbox {
width: 80%;
height: 100px;
}

</style>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>

<h1>Page "<span id="page-name"></span>" not found</h1>
<p>feel free to make one</p>

<form action="" method="post" id="in-form">
<h2><label>author</label></h2>
<input name="author" type="text">
<br/>

<h2><label>post</label></h2>
<textarea name="post"></textarea>

<br/>
<input type="submit">
</form>

<script>
let title = document.querySelector("#page-name")
title.innerText = window.location.href

</script>

</body>

</html>
Binary file added markdownpages/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions markdownpages/foo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
15 changes: 15 additions & 0 deletions page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head>

</head>
<body>

<a href="?edit=true">edit</a>
<br/>


<!-- TODO: not a fmt string-->
%s

</body>
</html>
121 changes: 121 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"os"
"path"

"gopkg.in/russross/blackfriday.v2"
"log"
"fmt"
"strings"
)

const MarkDownPages = "markdownpages"
var PageFormat string

func init() {

byt, err := ioutil.ReadFile("page.html")
if err != nil {
panic(err)
}

PageFormat = string(byt)
}

type WikiHandler struct {
root string
}

func (h *WikiHandler) absPath(p string) string {
return path.Join(h.root, p)
}

func (h *WikiHandler) get(path string) (io.Reader, error) {
abs := h.absPath(path)

data, err := ioutil.ReadFile(abs)
if err != nil {
// handle error
return nil, err
}

reader := strings.NewReader(
fmt.Sprintf(PageFormat, blackfriday.Run(data)))

return reader, nil

}

func (h *WikiHandler) post(r *http.Request) (io.Reader, error) {

r.ParseForm()

abs := h.absPath(r.URL.Path)
file, err := os.Create(abs)
if err != nil {
// handle error
return nil, err
}

fmt.Fprint(file, r.PostFormValue("post"))

file.Close()

data, _ := ioutil.ReadFile(abs)

reader := bytes.NewReader(
blackfriday.Run(data))

return reader, nil
}

func (h *WikiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

var data io.Reader
var err error

log.Printf("%s %s\n", r.Method, r.URL.Path)

switch r.Method {
case http.MethodGet: // handle get request
data, err = h.get(r.URL.Path)
if err != nil {
data, err = os.Open("form.html")
}

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
io.Copy(w, data)

case http.MethodPost:
data, err = h.post(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
io.Copy(w, data)

default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}

}

func main() {
handler := &WikiHandler{
root: MarkDownPages,
}

http.ListenAndServe(":8080", handler)
}