yatwiki/WikiServer.go

60 lines
1.1 KiB
Go
Raw Normal View History

2017-07-08 23:13:36 +00:00
package yatwiki3
import (
"html/template"
"log"
"net/http"
)
type WikiServer struct {
db *WikiDB
opts *ServerOptions
pageTmp *template.Template
}
func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
wdb, err := NewWikiDB(opts.DBFilePath)
if err != nil {
return nil, err
}
tmpl, err := template.New("yatwiki/page").Parse(pageTemplate)
if err != nil {
panic(err)
}
ws := WikiServer{
db: wdb,
opts: opts,
pageTmp: tmpl,
}
return &ws, nil
}
func (this *WikiServer) Close() {
this.db.Close()
}
func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "Yatwiki3")
if r.URL.Path == this.opts.ExpectBaseURL+"wiki.css" {
w.Header().Set("Content-Type", "text/css")
w.Write(tmplWikiCss)
return
}
pto := DefaultPageTemplateOptions(this.opts)
pto.SessionMessage = `Invalid request.`
//pto.CurrentPageIsArticle = true
//pto.CurrentPageName = "quotes/\"2017"
this.servePageResponse(w, pto)
}
func (this *WikiServer) servePageResponse(w http.ResponseWriter, pto *pageTemplateOptions) {
err := this.pageTmp.Execute(w, pto)
if err != nil {
log.Println(err.Error())
}
}