https://kotlinlang.org logo
#ktor
Title
# ktor
a

Abhigya Krishna

03/31/2024, 7:37 PM
Hello there, I am working on a project with KTOR for quiet some time. I got stuck on a issue where some of my routes are not getting registered. Can someone assist me with the issue?
Copy code
routing {
        pmsRoute(database)
        inventoryRoute(database)
        locationRoute(database)

        val allRoutes = allRoutes(this)
        val allRoutesWithMethod = allRoutes.filter { it.selector is HttpMethodRouteSelector }
        allRoutesWithMethod.forEach {
            println("route: $it")
        }
}

fun allRoutes(root: Route): List<Route> {
    return listOf(root) + root.children.flatMap { allRoutes(it) }
}
List of routes registered are:
Copy code
route: /location/(method:PUT)
route: /location/(method:POST)
route: /location/get_rooms/{floor}/(method:GET)
route: /location/get_rooms/all/(method:GET)
route: /location/get_floors/(method:GET)
route: /location/get_packets/{location}/(method:GET)
route: /location/get_packets/full/[floor]/[room]/[rack?]/[shelf?]/(method:GET)
Code:
Copy code
route("/location") {
        put<Resources.LocationRoute.Add.Unlinked> {
            // body
        }

        put<Resources.LocationRoute.Add.Linked> {
            // body
        }

        put<Resources.LocationRoute.Add.Floor> {
            // body
        }

        put<Resources.LocationRoute.Add.Room> {
            // body
        }

        post<Resources.LocationRoute.Update.All> {
            // body
        }

        post<Resources.LocationRoute.Update.LinkMachine> {
            // body
        }

        get<Resources.LocationRoute.GetRooms.ByFloor> {
            // body
        }

        get<Resources.LocationRoute.GetRooms.All> {
            // body
        }

        get<Resources.LocationRoute.GetFloors> {
            // body
        }

        get<Resources.LocationRoute.GetPackets.ByLocation> {
            // body
        }

        get<Resources.LocationRoute.GetPackets.Full> {
            // body
        }
}
Objects:
Copy code
class Resources {
    class LocationRoute {
        @Resource("/add")
        class Add {
            @Resource("unlinked")
            class Unlinked(val parent: Add = Add())

            @Resource("linked")
            class Linked(val parent: Add = Add())

            @Resource("floor")
            class Floor(val parent: Add = Add())

            @Resource("room")
            class Room(val parent: Add = Add())
        }

        @Resource("/update")
        class Update {
            @Resource("all")
            class All(val parent: Update = Update())

            @Resource("link_machine")
            class LinkMachine(val parent: Update = Update())
        }

        @Resource("/get_rooms")
        class GetRooms {
            @Resource("{floor}")
            class ByFloor(val parent: GetRooms = GetRooms(), val floor: Int)

            @Resource("all")
            class All(val parent: GetRooms = GetRooms())
        }

        @Resource("/get_floors")
        class GetFloors

        @Resource("/get_packets")
        class GetPackets {
            @Resource("{location}")
            class ByLocation(val parent: GetPackets = GetPackets(), val location: Int)

            @Resource("full")
            class Full(val parent: GetPackets = GetPackets(), val floor: Int, val room: Int, val rack: Int? = null, val shelf: Int? = null)
        }
    }
}
🧵 3
c

Chrimaeon

03/31/2024, 7:44 PM
1. Long code snippets should go to the thread. 2. Check your imports. You might face the same issue discussed some days before. https://kotlinlang.slack.com/archives/C0A974TJ9/p1711450966840419