Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider modifying the example code #1969

Closed
1 task done
rikonaka opened this issue Apr 28, 2023 · 4 comments
Closed
1 task done

Consider modifying the example code #1969

rikonaka opened this issue Apr 28, 2023 · 4 comments

Comments

@rikonaka
Copy link

  • I have looked for existing issues (including closed) about this

Bug Report

Version

├── axum v0.6.17
│ ├── axum-core v0.3.4

Platform

Linux homelab 5.10.0-21-amd64 #1 SMP Debian 5.10.162-1 (2023-01-21) x86_64 GNU/Linux

Crates

Description

I am try to run my first axum example from readme page.

use axum::{
    routing::{get, post},
    http::StatusCode,
    response::IntoResponse,
    Json, Router,
};
use serde::{Deserialize, Serialize};
use std::net::SocketAddr;

#[tokio::main]
async fn main() {
    // initialize tracing
    tracing_subscriber::fmt::init();

    // build our application with a route
    let app = Router::new()
        // `GET /` goes to `root`
        .route("/", get(root))
        // `POST /users` goes to `create_user`
        .route("/users", post(create_user));

    // run our app with hyper
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

// basic handler that responds with a static string
async fn root() -> &'static str {
    "Hello, World!"
}

async fn create_user(
    // this argument tells axum to parse the request body
    // as JSON into a `CreateUser` type
    Json(payload): Json<CreateUser>,
) -> (StatusCode, Json<User>) {
    // insert your application logic here
    let user = User {
        id: 1337,
        username: payload.username,
    };

    // this will be converted into a JSON response
    // with a status code of `201 Created`
    (StatusCode::CREATED, Json(user))
}

// the input to our `create_user` handler
#[derive(Deserialize)]
struct CreateUser {
    username: String,
}

// the output to our `create_user` handler
#[derive(Serialize)]
struct User {
    id: u64,
    username: String,
}

But I got error.

error[E0425]: cannot find function `serve` in crate `axum`
  --> src/main.rs:25:11
   |
25 |     axum::serve(listener, app).await.unwrap();
   |           ^^^^^ not found in `axum`
@davidpdrsn
Copy link
Member

From the readme

image

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2023
@rikonaka
Copy link
Author

Hi @davidpdrsn , the example you provided in the readme should at least run for test, we are not axum developers, if you can't do that then don't provide example code.

@davidpdrsn
Copy link
Member

The example works against the main branch, as intended. If you want to see what's published on crates.io use the v0.6.x branch.

@jplatte
Copy link
Member

jplatte commented Apr 28, 2023

You can also see the readme of the latest version (not git main) at https://crates.io/crates/axum by the way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants