Add index
This commit is contained in:
parent
a17874d5d3
commit
57f807e29f
30
main.go
30
main.go
@ -3,9 +3,11 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
@ -54,9 +56,37 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
|
||||
t.Execute(w, page)
|
||||
}
|
||||
|
||||
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
title := "index"
|
||||
//template, err := template.New("index").ParseFiles("templates/index.html")
|
||||
template, err := template.New("index").Parse("<!DOCTYPE html><ul>{{range $p := .}}<li><a href=view/{{$p.Title}}>{{$p.Title}}</a></li>{{end}}</ul>")
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, "<h1>ERROR</h1><p>%s not found!</p>", title)
|
||||
} else {
|
||||
files, err := ioutil.ReadDir(".")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
var pages []Page
|
||||
for _, file := range files {
|
||||
if strings.Contains(file.Name(), ".txt") {
|
||||
page, _ := loadPage(strings.Split(file.Name(), ".")[0])
|
||||
pages = append(pages, *page)
|
||||
}
|
||||
}
|
||||
err = template.Execute(w, pages)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/view/", viewHandler)
|
||||
http.HandleFunc("/save/", saveHandler)
|
||||
http.HandleFunc("/edit/", editHandler)
|
||||
http.HandleFunc("/", indexHandler)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user