Giter Club home page Giter Club logo

Comments (5)

 avatar commented on July 21, 2024

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.

mikepc avatar mikepc commented on July 21, 2024

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.

 avatar commented on July 21, 2024

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.

mikepc avatar mikepc commented on July 21, 2024

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.

 avatar commented on July 21, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.