Por que e quando um ResponseWriter geraria html bruto?
Não entendo por que o código está gerando os dados view.html e post.html corretamente, mas exibindo tudo como texto bruto. Eu estava seguindo o guiaaqui e enquanto eu o criava, pensei que o html gerado a partir da função Execute seria enviado para o ResponserWriter que cuidaria de exibi-lo, mas o erro que estou recebendo parece indicar que meu entendimento sobre Execute ou o ResponseWriter está errado.
package main
import (
"os"
"fmt"
"time"
"bufio"
"net/http"
"html/template"
)
type UserPost struct {
Name string
About string
PostTime string
}
func check(e error) {
if e != nil {
fmt.Println("Error Recieved...")
panic(e)
}
}
func lineCounter(workingFile *os.File) int {
fileScanner := bufio.NewScanner(workingFile)
lineCount := 0
for fileScanner.Scan() {
lineCount++
}
return lineCount
}
func loadPage(i int) (*UserPost, error) {
Posts,err := os.Open("dataf.txt")
check(err)
var PostArray [512]UserPost = parsePosts(Posts,i)
Name := PostArray[i].Name
About := PostArray[i].About
PostTime := PostArray[i].PostTime
Posts.Close()
return &UserPost{Name: Name[:len(Name)-1], About: About[:len(About)-1], PostTime: PostTime[:len(PostTime)-1]}, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
tmp,err := os.Open("dataf.txt")
check(err)
num := (lineCounter(tmp)/3)
tmp.Close()
for i := 0; i < num; i++ {
p, _ := loadPage(i)
t, _ := template.ParseFiles("view.html")
t.Execute(w, p)
}
p := UserPost{Name: "", About: "", PostTime: ""}
t, _ := template.ParseFiles("post.html")
t.Execute(w, p)
}
func inputHandler(w http.ResponseWriter, r *http.Request) {
Name := r.FormValue("person")
About := r.FormValue("body")
PostTime := time.Now().String()
filePaste,err := os.OpenFile("dataf.txt", os.O_RDWR | os.O_CREATE | os.O_APPEND | os.SEEK_END, 0666)
check(err)
filePaste.WriteString(Name+"~\n")
filePaste.WriteString(About+"~\n")
filePaste.WriteString(PostTime+"~\n")
filePaste.Close()
fmt.Println("Data recieved: ", Name,About,PostTime)
http.Redirect(w, r, "/#bottom", http,.StatusFound) //Use "/#bottom" to go to bottom of html page.
}
//os.File is the file type.
func parsePosts(fileToParse *os.File,num int) [512]UserPost {
var buffer [512]UserPost
reader := bufio.NewReader(fileToParse)
//This For loop reads each "forum post" then saves it to the buffer, then iterates to the next.
for i := 0;i <= num; i++ {
currentPost := new(UserPost)
str, err := reader.ReadString('~')
check(err)
currentPost.Name = str
//I search for '~' because my files save the end of reading line with that, so i can keep formatting saved (\n placement).
str2, err2 := reader.ReadString('~')
check(err2)
currentPost.About = str2
str3, err3 := reader.ReadString('~')
check(err3)
currentPost.PostTime = str3
buffer[i] = *currentPost
}
return buffer
}
func main() {
fmt.Println("Listening...")
http.HandleFunc("/", viewHandler)
http.HandleFunc("/post/", inputHandler)
http.ListenAndServe(":8080", nil)
}
view.html
<h4>{{.Name}}</h4>
<font size="3">
<div>{{printf "%s" .About}}</div>
</font>
<br>
<font size="2" align="right">
<div align="right">{{.PostTime}}</div>
</font>
post.html
<form action="/post/" method="POST">
<div><textarea name="person" rows="1" cols="30">{{printf "%s" .Name}}</textarea></div>
<div><textarea name="body" rows="5" cols="100">{{printf "%s" .About}}</textarea></div>
<div><input type="submit" value="Submit"></div>
<a name="bottom"></a>
</form>
Atualmente, eu tenho lido de um arquivo dataf.txt vazio.