1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-20 03:17:04 +00:00

simplify some examples

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-11-07 12:49:14 +02:00
parent 7a19cfb211
commit 7b6a8f1e26
17 changed files with 55 additions and 172 deletions

View File

@@ -1,27 +1,15 @@
package main
import (
"github.com/kataras/iris/v12"
)
// same as embedded-single-page-application but without go-bindata, the files are "physical" stored in the
// current system directory.
var page = struct {
Title string
}{"Welcome"}
import "github.com/kataras/iris/v12"
func newApp() *iris.Application {
app := iris.New()
app.RegisterView(iris.HTML("./public", ".html"))
app.Get("/", func(ctx iris.Context) {
ctx.ViewData("Page", page)
ctx.View("index.html")
app.HandleDir("/", iris.Dir("./public"), iris.DirOptions{
IndexName: "index.html",
SPA: true,
})
app.HandleDir("/", iris.Dir("./public"))
return app
}
@@ -29,8 +17,7 @@ func main() {
app := newApp()
// http://localhost:8080
// http://localhost:8080/index.html
// http://localhost:8080/app.js
// http://localhost:8080/css/main.css
// http://localhost:8080/about
// http://localhost:8080/a_notfound
app.Listen(":8080")
}

View File

@@ -1,62 +0,0 @@
package main
import (
"io/ioutil"
"path/filepath"
"strings"
"testing"
"github.com/kataras/iris/v12/httptest"
)
type resource string
func (r resource) String() string {
return string(r)
}
func (r resource) strip(strip string) string {
s := r.String()
return strings.TrimPrefix(s, strip)
}
func (r resource) loadFromBase(dir string) string {
filename := r.String()
if filename == "/" {
filename = "/index.html"
}
fullpath := filepath.Join(dir, filename)
b, err := ioutil.ReadFile(fullpath)
if err != nil {
panic(fullpath + " failed with error: " + err.Error())
}
result := string(b)
return result
}
var urls = []resource{
"/",
"/index.html",
"/app.js",
"/css/main.css",
}
func TestSPA(t *testing.T) {
app := newApp()
e := httptest.New(t, app, httptest.Debug(false))
for _, u := range urls {
url := u.String()
contents := u.loadFromBase("./public")
contents = strings.Replace(contents, "{{ .Page.Title }}", page.Title, 1)
e.GET(url).Expect().
Status(httptest.StatusOK).
Body().Equal(contents)
}
}

View File

@@ -1 +0,0 @@
window.alert("app.js loaded from \"/");

View File

@@ -1,3 +0,0 @@
body {
background-color: black;
}

View File

@@ -1,14 +1,19 @@
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ .Page.Title }}</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iris SPA Router Example</title>
</head>
<body>
<h1> Hello from index.html </h1>
<div id="app">
</div>
<script src="/app.js"> </script>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<script src="./index.js"></script>
</body>
</html>

View File

@@ -0,0 +1,21 @@
const NotFound = { template: '<p>Page not found</p>' }
const Home = { template: '<p>home page</p>' }
const About = { template: '<p>about page</p>' }
const routes = {
'/': Home,
'/about': About
}
const app = new Vue({
el: '#app',
data: {
currentRoute: window.location.pathname
},
computed: {
ViewComponent () {
return routes[this.currentRoute] || NotFound
}
},
render (h) { return h(this.ViewComponent) }
})