218 lines
5.7 KiB
Go
218 lines
5.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"html/template"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
var config = readConfig("")
|
|
|
|
type Configuration struct {
|
|
Host string
|
|
Port string
|
|
DataPath string
|
|
}
|
|
|
|
// our main function
|
|
func main() {
|
|
|
|
// Ausgeben der Config-Optionen
|
|
fmt.Println("Host: " + config.Host)
|
|
fmt.Println("Post: " + config.Port)
|
|
fmt.Println("DataPath: " + config.DataPath)
|
|
|
|
// DataPath-Verzeichnis anlegen, wenn es noch nicht existiert
|
|
if !directoryExists(config.DataPath) {
|
|
os.MkdirAll(config.DataPath, os.ModePerm)
|
|
|
|
// Default Seiten anlegen
|
|
bStartmd, _ := Asset("web/pages/start.md")
|
|
bHilfemd, _ := Asset("web/pages/hilfe.md")
|
|
bPlaygroundmd, _ := Asset("web/pages/playground.md")
|
|
bSidebarmd, _ := Asset("web/pages/sidebar.md")
|
|
|
|
err := ioutil.WriteFile(path.Join(config.DataPath, "start.md"), bStartmd, 0644)
|
|
check(err)
|
|
err = ioutil.WriteFile(path.Join(config.DataPath, "sidebar.md"), bSidebarmd, 0644)
|
|
check(err)
|
|
err = ioutil.WriteFile(path.Join(config.DataPath, "hilfe.md"), bHilfemd, 0644)
|
|
check(err)
|
|
err = ioutil.WriteFile(path.Join(config.DataPath, "playground.md"), bPlaygroundmd, 0644)
|
|
check(err)
|
|
}
|
|
|
|
router := mux.NewRouter()
|
|
router.HandleFunc("/p/{pagename:.*}", getRawPage).Methods("GET")
|
|
router.HandleFunc("/{pagename:.*}", getHTMLPage).Methods("GET")
|
|
router.HandleFunc("/{pagename:.*}", postHTMLPage).Methods("POST")
|
|
|
|
log.Fatal(http.ListenAndServe(":"+config.Port, router))
|
|
|
|
}
|
|
|
|
func getHTMLPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := mux.Vars(r)
|
|
|
|
if strings.HasSuffix(params["pagename"], ".css") {
|
|
//fmt.Println("CSS: " + params["pagename"])
|
|
b, _ := Asset("web/" + params["pagename"])
|
|
w.Header().Set("Content-Type", "text/css")
|
|
w.Write(b)
|
|
} else if strings.HasSuffix(params["pagename"], ".js") {
|
|
//fmt.Println("JS: " + params["pagename"])
|
|
b, _ := Asset("web/" + params["pagename"])
|
|
w.Header().Set("Content-Type", "application/javascript")
|
|
w.Write(b)
|
|
} else {
|
|
// Directory Listing für die Sidebar
|
|
sidebarAusgabe := " \r\n \r\n**Inhaltsverzeichnis** \r\n\r\n"
|
|
filepath.Walk(config.DataPath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
tmpPath := strings.Replace(path, "\\", "/", -1)
|
|
tmpPath = strings.Replace(tmpPath, "data", "", -1)
|
|
tmpPathSplit := strings.Split(tmpPath, "/")
|
|
|
|
sidebarTABs := ""
|
|
for i, _ := range tmpPathSplit {
|
|
//fmt.Println(i, " => ", string(c))
|
|
if i > 1 {
|
|
sidebarTABs = sidebarTABs + " "
|
|
}
|
|
}
|
|
|
|
if info.Name() != "data" && info.Name() != "start.md" && info.Name() != "sidebar.md" && info.Name() != "hilfe.md" {
|
|
if !strings.HasSuffix(info.Name(), ".md") {
|
|
sidebarAusgabe = sidebarAusgabe + sidebarTABs + "* [" + strings.TrimSuffix(info.Name(), ".md") + "](" + strings.TrimSuffix(tmpPath, ".md") + "/start) \r\n"
|
|
} else {
|
|
sidebarAusgabe = sidebarAusgabe + sidebarTABs + "* [" + strings.TrimSuffix(info.Name(), ".md") + "](" + strings.TrimSuffix(tmpPath, ".md") + ") \r\n"
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
//tmpl := template.Must(template.ParseFiles("./web/index.html"))
|
|
bTmpl, _ := Asset("web/index.html")
|
|
tmpl := template.Must(template.New("tml").Parse(string(bTmpl)))
|
|
data := MarkDownData{
|
|
PageTitle: " ",
|
|
MDText: "",
|
|
SideBar: sidebarAusgabe,
|
|
}
|
|
tmpl.Execute(w, data)
|
|
}
|
|
}
|
|
|
|
func getRawPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := mux.Vars(r)
|
|
pPageName := params["pagename"] + ".md"
|
|
// Hack für anzeige der start.md wenn / aufgerufen wird
|
|
if pPageName == ".md" {
|
|
pPageName = "start.md"
|
|
}
|
|
pageContent := "Empty"
|
|
|
|
//fmt.Println(path.Join(config.DataPath, pPageName))
|
|
b, err := ioutil.ReadFile(path.Join(config.DataPath, pPageName))
|
|
check(err)
|
|
|
|
pageContent = string(b)
|
|
|
|
json.NewEncoder(w).Encode(pageContent)
|
|
}
|
|
|
|
func postHTMLPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := mux.Vars(r)
|
|
pPageName := params["pagename"] + ".md"
|
|
|
|
// Verzeichnis erstellen
|
|
dir := path.Dir(pPageName)
|
|
if !directoryExists(path.Join(config.DataPath, dir)) {
|
|
os.MkdirAll(path.Join(config.DataPath, dir), os.ModePerm)
|
|
}
|
|
|
|
//{ "MDText":"**test3**" }
|
|
var oBody TPostBody
|
|
json.NewDecoder(r.Body).Decode(&oBody)
|
|
//fmt.Println(oBody)
|
|
|
|
f, err := os.Create(path.Join(config.DataPath, pPageName))
|
|
check(err)
|
|
defer f.Close()
|
|
fw := bufio.NewWriter(f)
|
|
_, err = fw.WriteString(oBody.MDText)
|
|
check(err)
|
|
fw.Flush()
|
|
|
|
json.NewEncoder(w).Encode("OK")
|
|
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
// Typen
|
|
//--------------------------------------------------------------------------
|
|
|
|
type MarkDownData struct {
|
|
PageTitle string
|
|
MDText string
|
|
SideBar string
|
|
}
|
|
|
|
type TPostBody struct {
|
|
MDText string
|
|
}
|
|
|
|
//--------------------------------------------------------------------------
|
|
// Hilfsfunktionen
|
|
//--------------------------------------------------------------------------
|
|
|
|
func check(e error) {
|
|
if e != nil {
|
|
fmt.Println(e)
|
|
}
|
|
}
|
|
|
|
func fileExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
func directoryExists(filename string) bool {
|
|
info, err := os.Stat(filename)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return info.IsDir()
|
|
}
|
|
|
|
func readConfig(filename string) *Configuration {
|
|
// initialize conf with default values.
|
|
conf := &Configuration{Host: "http://127.0.0.1", Port: "8000", DataPath: "./data"}
|
|
|
|
b, err := ioutil.ReadFile("./config.json")
|
|
if err != nil {
|
|
return conf
|
|
}
|
|
if err = json.Unmarshal(b, conf); err != nil {
|
|
return conf
|
|
}
|
|
return conf
|
|
}
|