package main
import (
"net/http"
"text/template"
"os"
"io/ioutil"
"strings"
"time"
"fmt"
)
type Context struct {
Title string
Items []FullItem
}
type FullItem struct{
Time string
Name string
Area string
}
func main() {
const htmlpage = `
Itemlist:
{{range .Items}}
{{.Time}} |
{{.Name}} |
{{.Area}} |
{{end}}
`
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fileName := "pickit_log.txt"
Items := []FullItem{}
fileBytes, err := ioutil.ReadFile(fileName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
lines := strings.Split(string(fileBytes), "\n")
for i := len(lines)-1; i >= 0; i-- {
line := lines[i]
if(len(line)<20) {
continue
}
timestamp := line[1:20]
parsed, _ := time.Parse("2006-01-02 15:04:05", timestamp)
restofdata:= line[27:len(line)]
importadata:= strings.SplitN(restofdata, "|", 2)
segmenteddata := strings.SplitN(importadata[0], "-", 2)
itemarea := segmenteddata[1]
itemname := segmenteddata[0]
newItem := FullItem{
Time: parsed.Format("15:04"), //24 hour format
//Time: parsed.Format("3:04 PM"), //12 hour AM/PM format
Name: itemname[1:len(itemname)],
Area: itemarea[7:len(itemarea)],
}
Items = append(Items, newItem)
}
w.Header().Add("Content Type", "text/html")
templates, _ := template.New("doc").Parse(htmlpage)
context := Context{
Title: "Item List",
Items: Items,
}
templates.Lookup("doc").Execute(w, context)
})
http.ListenAndServe(":8080", nil)
}