Gisle Aune
7 years ago
9 changed files with 133 additions and 27 deletions
-
11controllers/listcontroller.go
-
BINdebug
-
11main.go
-
9server/server.go
-
59view/renderer.go
-
23view/templates/base/default.tmpl
-
13view/templates/index.tmpl
-
9viewmodel/base.go
-
21viewmodel/pagelist.go
@ -1,5 +1,16 @@ |
|||
package main |
|||
|
|||
import "git.aiterp.net/AiteRP/aitestory/server" |
|||
import "git.aiterp.net/AiteRP/aitestory/controllers" |
|||
import "git.aiterp.net/gisle/wrouter/auth" |
|||
|
|||
func main() { |
|||
router := &server.Main.Router |
|||
|
|||
auth.Register(&controllers.WikiAthenticator{}) |
|||
router.Mount("/", &controllers.ListController) |
|||
|
|||
router.Static("/ui/", server.Main.Config.Server.UI) |
|||
|
|||
server.Main.Start() |
|||
} |
@ -0,0 +1,59 @@ |
|||
package view |
|||
|
|||
import ( |
|||
"errors" |
|||
"html/template" |
|||
"io" |
|||
"log" |
|||
"net/http" |
|||
"os" |
|||
"path" |
|||
|
|||
"git.aiterp.net/gisle/wrouter/response" |
|||
) |
|||
|
|||
var wd, _ = os.Getwd() |
|||
var rootPath = path.Join(wd, "./view/templates/") |
|||
var cache = make(map[string]*template.Template) |
|||
|
|||
// Register registers a template and compiles it for rendering. This should be done
|
|||
// in the beginning since an error will terminate the server
|
|||
func Register(name string, base string) { |
|||
tmpl, err := template.New(name).ParseFiles(path.Join(rootPath, name+".tmpl"), path.Join(rootPath, base+".tmpl")) |
|||
if err != nil { |
|||
log.Fatalf("Failed to register %s: %s", name, err) |
|||
} |
|||
|
|||
cache[name] = tmpl |
|||
} |
|||
|
|||
// Render renders a template with the name it was registered with, and with the
|
|||
// view model. The view model is expected to be the correct model from the viewmodel
|
|||
// package
|
|||
func Render(w http.ResponseWriter, name string, status int, viewModel interface{}) { |
|||
tmpl, ok := cache[name] |
|||
if !ok { |
|||
response.Text(w, 500, "Template not found: "+name) |
|||
return |
|||
} |
|||
|
|||
w.WriteHeader(status) |
|||
err := tmpl.ExecuteTemplate(w, "base", viewModel) |
|||
if err != nil { |
|||
log.Println("Template error:", err.Error()) |
|||
} |
|||
} |
|||
|
|||
// Run runs the template to a simple io.Writer. It's made for testing
|
|||
func Run(w io.Writer, name string, viewModel interface{}) error { |
|||
tmpl, ok := cache[name] |
|||
if !ok { |
|||
return errors.New("template not found") |
|||
} |
|||
|
|||
return tmpl.ExecuteTemplate(w, "base", viewModel) |
|||
} |
|||
|
|||
func init() { |
|||
Register("index", "base/default") |
|||
} |
@ -0,0 +1,13 @@ |
|||
{{ define "content" }} |
|||
<article> |
|||
<h1>Hello, World</h1> |
|||
</article> |
|||
{{ end }} |
|||
|
|||
{{ define "menu" }} |
|||
<h1>Aite RP</h1> |
|||
{{ end }} |
|||
|
|||
{{ define "head" }} |
|||
|
|||
{{ end }} |
@ -0,0 +1,21 @@ |
|||
package viewmodel |
|||
|
|||
import ( |
|||
"git.aiterp.net/AiteRP/aitestory/model" |
|||
"git.aiterp.net/gisle/wrouter/auth" |
|||
) |
|||
|
|||
// PageList is a view model for rendering the front page
|
|||
type PageList struct { |
|||
Base |
|||
Headers []model.Header |
|||
Category string |
|||
ActiveTags []model.Tag |
|||
FavoriteTags []model.Tag |
|||
} |
|||
|
|||
// Setup sets up the page model and the base, and should
|
|||
// be run after the details have been filled in.
|
|||
func (pl *PageList) Setup(user *auth.User) { |
|||
pl.setupBase(user, "Index") |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue