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
|
2017-07-09 00:14:28 +00:00
|
|
|
} else if r.URL.Path == this.opts.ExpectBaseURL+"favicon.ico" && len(this.opts.FaviconFilePath) > 0 {
|
|
|
|
w.Header().Set("Content-Type", "image/x-icon")
|
|
|
|
http.ServeFile(w, r, this.opts.FaviconFilePath)
|
|
|
|
return
|
2017-07-08 23:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|