Templates are working, just empty
This commit is contained in:
@@ -2,6 +2,10 @@ package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.aiterp.net/AiteRP/aitestory/view"
|
||||
"git.aiterp.net/AiteRP/aitestory/viewmodel"
|
||||
|
||||
"git.aiterp.net/gisle/wrouter"
|
||||
"git.aiterp.net/gisle/wrouter/auth"
|
||||
@@ -11,11 +15,14 @@ import (
|
||||
var ListController = wrouter.Router{}
|
||||
|
||||
func listIndex(path string, w http.ResponseWriter, req *http.Request, user *auth.User) bool {
|
||||
if req.Method != "GET" {
|
||||
if req.Method != "GET" || strings.LastIndex(req.URL.Path, "/") >= len(path) {
|
||||
return false
|
||||
}
|
||||
|
||||
// tags := strings.Split(path, ",")
|
||||
vm := viewmodel.PageList{}
|
||||
vm.Setup(user)
|
||||
|
||||
view.Render(w, "index", 200, vm)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
package main
|
||||
|
||||
func 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()
|
||||
}
|
||||
@@ -25,6 +25,15 @@ type server struct {
|
||||
Config Config
|
||||
}
|
||||
|
||||
func (srv *server) Start() {
|
||||
log.Println(fmt.Sprintf("Running: http://%s:%d", srv.Config.Server.Host, srv.Config.Server.Port))
|
||||
|
||||
_, err := srv.Router.Listen(srv.Config.Server.Host, srv.Config.Server.Port)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to init listen: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Main is the main instance
|
||||
var Main = server{}
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
{{define "layout"}}
|
||||
{{define "base"}}
|
||||
<!DOCTYPE html>
|
||||
<!-- For preview and template creation only -->
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>{ .ViewTitle } - { .SiteTitle }</title>
|
||||
<title>{{ .ViewTitle }}</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=no" />
|
||||
@@ -32,27 +32,10 @@
|
||||
</main>
|
||||
|
||||
<nav class="main-menu">
|
||||
|
||||
<h1>{ .ViewTitle }</h1>
|
||||
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">O</div><div class="mg-label">OoC</div></a></li>
|
||||
<li><a><div class="mg-icon">S</div><div class="mg-label">Stories</div></a></li>
|
||||
<li><a><div class="mg-icon">N</div><div class="mg-label">News</div></a></li>
|
||||
<li><a><div class="mg-icon">D</div><div class="mg-label">Documents</div></a></li>
|
||||
<li><a><div class="mg-icon">I</div><div class="mg-label">Items</div></a></li>
|
||||
<li><a><div class="mg-icon">i</div><div class="mg-label">Info</div></a></li>
|
||||
<li><a><div class="mg-icon">B</div><div class="mg-label">Background</div></a></li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">+</div><div class="mg-label">Post</div></a></li>
|
||||
</ul>
|
||||
|
||||
{{ template "menu" }}
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">A</div><div class="mg-label">Login</div></a></li>
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
</div>
|
||||
</body>
|
||||
@@ -0,0 +1,13 @@
|
||||
{{ define "content" }}
|
||||
<article>
|
||||
<h1>Hello, World</h1>
|
||||
</article>
|
||||
{{ end }}
|
||||
|
||||
{{ define "menu" }}
|
||||
<h1>Aite RP</h1>
|
||||
{{ end }}
|
||||
|
||||
{{ define "head" }}
|
||||
|
||||
{{ end }}
|
||||
+6
-3
@@ -2,6 +2,7 @@ package viewmodel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.aiterp.net/AiteRP/aitestory/server"
|
||||
"git.aiterp.net/gisle/wrouter/auth"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
|
||||
// Base is the basic information used to render the page
|
||||
type Base struct {
|
||||
UserID string
|
||||
UserName string
|
||||
UserRole string
|
||||
UserLoggedIn bool
|
||||
@@ -16,11 +18,12 @@ type Base struct {
|
||||
}
|
||||
|
||||
// InitBase initializes the base of the viewmodel
|
||||
func (base *Base) InitBase(user *auth.User, viewTitle string) {
|
||||
func (base *Base) setupBase(user *auth.User, viewTitle string) {
|
||||
if user != nil {
|
||||
base.UserName = user.ID
|
||||
base.UserID = user.ID
|
||||
base.UserName = strings.SplitN(user.ID, ":", 2)[1]
|
||||
base.UserRole = user.Data["role"]
|
||||
base.UserLoggedIn = false
|
||||
base.UserLoggedIn = true
|
||||
}
|
||||
|
||||
base.ViewTitle = fmt.Sprintf("%s - %s", viewTitle, server.Main.Config.View.Title)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user