Skip to content

Commit

Permalink
Add serving spa - the easy way #9 https://github.com/kataras/iris/iss…
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jan 12, 2017
1 parent e460994 commit 712b49c
Show file tree
Hide file tree
Showing 5 changed files with 16,474 additions and 21 deletions.
35 changes: 14 additions & 21 deletions route_state/main.go
Expand Up @@ -6,38 +6,31 @@ import (

func main() {

iris.None("/api/user/:userid", func(ctx *iris.Context) {
// You can find the Route by iris.Lookup("theRouteName")
// you can set a route name as: myRoute := iris.Get("/mypath", handler)("theRouteName")
// that will set a name to the route and returns its iris.Route instance for further usage.
api := iris.None("/api/users/:userid", func(ctx *iris.Context) {
userid := ctx.Param("userid")
ctx.Writef("user with id: %s", userid)
})("user.api")
})("users.api")

// change the "user.api" state from offline to online and online to offline
// change the "users.api" state from offline to online and online to offline
iris.Get("/change", func(ctx *iris.Context) {
routeName := "user.api"
if iris.Lookup(routeName).IsOnline() {
if api.IsOnline() {
// set to offline
iris.SetRouteOffline(routeName)
iris.SetRouteOffline(api)
} else {
// set to online if it was not online(so it was offline)
iris.SetRouteOnline(routeName, iris.MethodGet)
iris.SetRouteOnline(api, iris.MethodGet)
}
})

// iris.Get("/execute/:routename", func(ctx *iris.Context) {
// routeName := ctx.Param("routename")
// userAPICtx := ctx.ExecuteRoute(routeName)
// if userAPICtx == nil {
// ctx.Writef("Route with name: %s didnt' found or couldn't be validate with this request path!", routeName)
// }
// })

iris.Get("/execute", func(ctx *iris.Context) {
routeName := "user.api"
// change the path in order to be catcable from the ExecuteRoute
// ctx.Request.URL.Path = "/api/user/42"
// ctx.ExecRoute(routeName)
// ctx.Request.URL.Path = "/api/users/42"
// ctx.ExecRoute(iris.Route)
// or:
ctx.ExecRouteAgainst(routeName, "/api/user/42")
ctx.ExecRouteAgainst(api, "/api/users/42")
})

iris.Get("/", func(ctx *iris.Context) {
Expand All @@ -48,11 +41,11 @@ func main() {
// START THE SERVER
//
// STEPS:
// 1. navigate to http://localhost:8080/user/api/42
// 1. navigate to http://localhost:8080/api/users/42
// you should get 404 error
// 2. now, navigate to http://localhost:8080/change
// you should see a blank page
// 3. now, navigate to http://localhost:8080/user/api/42
// 3. now, navigate to http://localhost:8080/api/users/42
// you should see the page working, NO 404 error
// go back to the http://localhost:8080/change
// you should get 404 error again
Expand Down
33 changes: 33 additions & 0 deletions spa_2_using_offline_routing/main.go
@@ -0,0 +1,33 @@
package main

import (
"github.com/kataras/iris"
)

func main() {

// usersAPI := iris.None("/api/users/:userid", func(ctx *iris.Context) {
// ctx.Writef("user with id: %s", ctx.Param("userid"))
// })("api.users.id") // we need to call empty ("") in order to get its iris.Route instance
// // or ("the name of the route")
// // which later on can be found with iris.Lookup("the name of the route")

// static := iris.StaticHandler("/", "./www", false, false)
// iris.Get("/*file", iris.Prioritize(usersAPI), static)
// ___OR___
// static := iris.NewStaticHandlerBuilder("./www").Path("/").Except(usersAPI).Build()
// iris.Get("/*file", static)

// ___OR___SIMPLY:

usersAPI := iris.None("/api/users/:userid", func(ctx *iris.Context) {
ctx.Writef("user with id: %s", ctx.Param("userid"))
})("api.users.id")

iris.StaticWeb("/", "./www", usersAPI)

//
// START THE SERVER
//
iris.Listen("localhost:8080")
}
12 changes: 12 additions & 0 deletions spa_2_using_offline_routing/www/index.html
@@ -0,0 +1,12 @@
<html>
<head>
<title>SPA Index Title</title>
<link rel="stylesheet" href="resources/css/bootstrap.min.css">
</head>
<body>
<h1>Home</h1>
<button class="btn btn-primary">Hi, Iris</button>

<script src="resources/js/jquery-2.1.1.js"> </script>
</body>
</html>

0 comments on commit 712b49c

Please sign in to comment.