Code Monkey home page Code Monkey logo

Comments (13)

fafhrd91 avatar fafhrd91 commented on May 8, 2024

technically it is possible to do, but there is no easy way. You can register application handler with ApplicationBuilder::Handler, it accepts prefix, then you can split prefix and get tail.

would you like to implement tail match for actix web?

from actix-web.

krircc avatar krircc commented on May 8, 2024

@fafhrd91 yes. I want do a SPA with actic-web. so when request other paths it will return index.html.
like:

'/router/one'  ---> index.html
'/router/two'  ---> index.html
......

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

for your example you can use following example:

Application::default("/")
    .resource("/router/{section}", |r| {
        match r.params().get("section") {
          Some("one") => { do something },
          Some("two") => { do something else },
           _ => HTTPNotFound 
        }
    }))

is this what you need? you can match only one section at the moment.

from actix-web.

krircc avatar krircc commented on May 8, 2024

@fafhrd91 no , I want match any paths not someone. like '/router/{anyPaths}'
in rocket.rs like :

#[get("/page/<path..>")]
fn path(path: PathBuf) -> io::Result<NamedFile> {
    NamedFile::open("webfront/index.html")
}

Is there a easy way do this current?

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

it is not explicitly implemented at the moment. Would you like to participate in actix-web development and add this functionality?

there is temporary solution:

Application::default("/")
    .handler("/router/", |r| {
        let tail = r.path()[r.prefix_len()..];
        println!("Tail: {:?}", tail);
        HTTPOk
    }))

tail would contain everything after /router/ prefix

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

if you want just serve files from filesystem you can use https://github.com/actix/actix-web/blob/master/src/staticfiles.rs#L26

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

actix-web is missing a lot of staff, any help is welcome!

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

I just added tail pattern https://github.com/actix/actix-web/blob/master/src/recognizer.rs#L140

you can use it like this:

Application::default("/")
    .handler("/router/{tail:*}", |r| {
        let tail = r.match_info().get("tail").unwrap();
        println!("Tail: {:?}", tail);
        HTTPOk
    }))

I didn't test it though

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

I'll close issue. reopen if you think we should add something else

from actix-web.

krircc avatar krircc commented on May 8, 2024

@fafhrd91 please provide support reponse return file.

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

Could you provide pull request?

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

here is example

extern crate actix_web;
use actix_web::*;
use std::path::PathBuf;

fn index(req: HttpRequest) -> Result<fs::NamedFile> {
    let path: PathBuf = req.match_info().query("tail")?;
    Ok(fs::NamedFile::open(path)?)
}

fn main() {
    Application::default("/")
        .resource(r"/a/{tail:*}", |r| r.get(index))
        .finish();
}

from actix-web.

fafhrd91 avatar fafhrd91 commented on May 8, 2024

I dropped tail pattern, but it possible to do the same with custom regex ".*" :

extern crate actix_web;
use actix_web::*;
use std::path::PathBuf;

fn index(req: HttpRequest) -> Result<fs::NamedFile> {
    let path: PathBuf = req.match_info().query("tail")?;
    Ok(fs::NamedFile::open(path)?)
}

fn main() {
    Application::default("/")
        .resource(r"/a/{tail:.*}", |r| r.method(Method::GET).f(index))
        .finish();
}

from actix-web.

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.