-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: define page structs and template names in sub-package; clea…
…ned up some code.
- Loading branch information
1 parent
45863f7
commit d5262d5
Showing
7 changed files
with
234 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package page | ||
|
||
const ( | ||
TnameError = `error.html` | ||
TnameNotFound = `notfound.html` | ||
TnameClasslist = `classlist.html` | ||
TnameBbsIndex = `bbsindex.html` | ||
TnameBbsArticle = `bbsarticle.html` | ||
TnameAskOver18 = `askover18.html` | ||
|
||
TnameLayout = `layout.html` | ||
TnameCommon = `common.html` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package page | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ptt/pttweb/pttbbs" | ||
) | ||
|
||
type Page interface { | ||
TemplateName() string | ||
} | ||
|
||
type NoContent struct{} | ||
|
||
func (NoContent) TemplateName() string { return "" } | ||
|
||
type Redirect struct { | ||
NoContent | ||
To string | ||
} | ||
|
||
func (p *Redirect) WriteHeaders(w http.ResponseWriter) error { | ||
w.Header().Set("Location", p.To) | ||
w.WriteHeader(http.StatusFound) | ||
return nil | ||
} | ||
|
||
func NewRedirect(to string) *Redirect { | ||
return &Redirect{ | ||
To: to, | ||
} | ||
} | ||
|
||
type NotFound struct{} | ||
|
||
func (NotFound) TemplateName() string { return TnameNotFound } | ||
|
||
func (p *NotFound) WriteHeaders(w http.ResponseWriter) error { | ||
w.WriteHeader(http.StatusNotFound) | ||
return nil | ||
} | ||
|
||
type Error struct { | ||
Title string | ||
ContentHtml string | ||
} | ||
|
||
func (Error) TemplateName() string { return TnameError } | ||
|
||
type AskOver18 struct { | ||
From string | ||
} | ||
|
||
func (AskOver18) TemplateName() string { return TnameAskOver18 } | ||
|
||
type Classlist struct { | ||
Boards []pttbbs.Board | ||
} | ||
|
||
func (Classlist) TemplateName() string { return TnameClasslist } | ||
|
||
type BbsIndex struct { | ||
Board pttbbs.Board | ||
|
||
HasPrevPage bool | ||
HasNextPage bool | ||
PrevPage int | ||
NextPage int | ||
TotalPage int | ||
|
||
Articles []pttbbs.Article | ||
|
||
IsValid bool | ||
} | ||
|
||
func (BbsIndex) TemplateName() string { return TnameBbsIndex } | ||
|
||
type BbsArticle struct { | ||
Title string | ||
Description string | ||
Board *pttbbs.Board | ||
FileName string | ||
ContentHtml string | ||
ContentTailHtml string | ||
ContentTruncated bool | ||
} | ||
|
||
func (BbsArticle) TemplateName() string { return TnameBbsArticle } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package page | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
type TemplateMap map[string]*template.Template | ||
|
||
var ( | ||
templateFiles = [][]string{ | ||
{TnameError, TnameLayout, TnameCommon}, | ||
{TnameNotFound, TnameLayout, TnameCommon}, | ||
{TnameClasslist, TnameLayout, TnameCommon}, | ||
{TnameBbsIndex, TnameLayout, TnameCommon}, | ||
{TnameBbsArticle, TnameLayout, TnameCommon}, | ||
{TnameAskOver18, TnameLayout, TnameCommon}, | ||
} | ||
|
||
tmpl TemplateMap | ||
) | ||
|
||
func loadTemplates(dir string, filenames [][]string, funcMap template.FuncMap) (TemplateMap, error) { | ||
new_tmpl := make(TemplateMap) | ||
|
||
for _, fns := range filenames { | ||
t := template.New("") | ||
t.Funcs(funcMap) | ||
|
||
paths := make([]string, len(fns), len(fns)) | ||
for i, fn := range fns { | ||
paths[i] = filepath.Join(dir, fn) | ||
} | ||
|
||
if _, err := t.ParseFiles(paths...); err != nil { | ||
return nil, err | ||
} | ||
|
||
name := fns[0] | ||
root := t.Lookup("ROOT") | ||
if root == nil { | ||
return nil, errors.New("No ROOT template defined") | ||
} | ||
new_tmpl[name] = root | ||
} | ||
|
||
return new_tmpl, nil | ||
} | ||
|
||
func LoadTemplates(dir string, funcMap template.FuncMap) error { | ||
new_tmpl, err := loadTemplates(dir, templateFiles, funcMap) | ||
if err != nil { | ||
return err | ||
} | ||
tmpl = new_tmpl | ||
return nil | ||
} | ||
|
||
type NeedToWriteHeaders interface { | ||
WriteHeaders(w http.ResponseWriter) error | ||
} | ||
|
||
func ExecuteTemplate(w http.ResponseWriter, name string, arg interface{}) error { | ||
if p, ok := arg.(NeedToWriteHeaders); ok { | ||
if err := p.WriteHeaders(w); err != nil { | ||
return err | ||
} | ||
} | ||
if name == "" { | ||
return nil | ||
} | ||
return tmpl[name].Execute(w, arg) | ||
} | ||
|
||
func ExecutePage(w http.ResponseWriter, p Page) error { | ||
return ExecuteTemplate(w, p.TemplateName(), p) | ||
} |
Oops, something went wrong.