Comments (5)
First of all, Static
routes goes first, if you put it first it, correctly, gives you a panic: Error: Router: Path's part: '/api/all' conflicts with wildcard '/*filepath' in the route path: '/api/all' !
Yes, because iris.StaticWeb
will override the /api/all it will think it as /api/all/index.html
because you're using the StaticWeb
. You could use the context.ServeFile
on a wildcard /*path
route, and inside this handler do a if path:= context.Param("path") ; path == "/api/all" { allVendorBrandLabel(context)} else { ctx.ServeFile(path) }
try this and tell me the results. Or we could discuss ways to make a ready-to-use function for SPA with paths (html5)
from examples.
Thank you very much for your very helpful response.
`func main() {
iris.Get("/*path", func(c *iris.Context) {
path := c.Param("path")
if path == "/api/all" {
allVendorBrandLabel(c)
} else {
c.ServeFile("./www/index.html", true)
}
})
iris.Listen(":8080")
}
`
That's the final code to get it to work.
It would be nice to have a more elegant way to approach this though, as the api portion of the app matures, it would be nice to handle it in a more similar way to other routes.
from examples.
I just think it simpler,
package main
import (
"github.com/kataras/iris"
)
// hosts: add these lines: (use tabs to separate them, if that doesn't works for you)
// 127.0.0.1 mydomain.com
// 127.0.0.1 api.mydomain.com
func main() {
// OPTIONAL if you want diffent domain for static files
// (I do this way but you are not forced to do the same)
//
// create the subdomain s.mydomain.com to serve our resources files
// http://s.mydomain.com/resources/css/bootstrap.min.css ...
// iris.Party("s.").StaticWeb("/", "./www/resources")
//
//
// REGISTER YOUR REST API
//
// http://api.mydomain.com/ ...
// brackers are optional, it's just a visual declaration.
api := iris.Party("api.")
{
// http://api.mydomain.com/users/42
api.Get("/users/:userid", func(ctx *iris.Context) {
ctx.Writef("user with id: %s", ctx.Param("userid"))
})
}
//
// REGISTER THE PAGE AND ALL OTHER STATIC FILES
// INCLUDING A FAVICON, CSS, JS and so on
//
// http://mydomain.com , here should be your index.html
// which is the SPA frontend page
iris.StaticWeb("/", "./www")
//
// START THE SERVER
//
iris.Listen("mydomain.com:80")
}
- Or you could just use other
iris station and server
for static files and other for SPA,
statics := iris.New()
statics.StaticWeb("/","./www")
go statics.Listen(":9090")
iris.Get("/api/all",...)
iris.Listen(":8080")
from examples.
You rock man, iris is the best Go web framework out there in my opinion, which is why when I came to this team at Nordstrom I used it for my first project. I have used it in the past for several microservices on other projects and it worked splendidly. Will keep checking back! Right now there's just one endpoint so I can get started.
from examples.
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")
// third parameter is optional, if not empty then these routes
// will have priority over the static handler when the request path is one of their route's path
iris.StaticWeb("/", "./www", usersAPI)
iris.Listen("localhost:8080")
}
@mikepc Surprise :D
Discussion: https://github.com/kataras/iris/issues/585
from examples.
Related Issues (20)
- Any example of this code??? HOT 1
- read form example cannot run HOT 1
- Gzip doesn't work HOT 2
- How to print Session value in Template HOT 1
- Flash Session [ Help Needed ] HOT 1
- get with params HOT 2
- how to read data from client like JMeter HOT 4
- why static file can't be packaged HOT 5
- Can we use mvc service in another service? HOT 3
- How can I get test coverage? HOT 1
- "WithoutVersionChecker" is undefined! HOT 2
- "Hello, webAssembly" link in the table of contents is broken HOT 1
- Error with ctx.URLParams()
- write tcp [::1]:8099->[::1]:64820: wsasend: An established connection was aborted by the software in your host machine.
- WebSocket to REST handlers Adapter
- /create and /decode has no function at the example "Glimpse" HOT 1
- html/template: "movie/index.html" is undefined HOT 1
- Potential import collision: import path should be "go.etcd.io/bbolt", not "github.com/etcd-io/bbolt" HOT 4
- httpexpect doesn't work
- How do I set up a header? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.