Making controller class from scratch

So I have made this web server here’s some UML for the design.

UML for HTTP web server

I have the parts of my URI saved to a string[] so that the URI "/school/students" would be decoded to {"school","students"}. Then the URI is mapped onto a collection of resrouces and sub resrouces; which links to the appropriate method route (which defines the HTTP response sent back when a specific URI and HTTP method is entered).

Here’s an example of setting up some basic routing:

Resource pupils = new Resource("pupils");          HTTPResponse pupilsResponse = new HTTPResponse(                     HTTPResponse.StatusCodes.OK,                     HTTPMessage.MIMETypes.PLAIN_TEXT,                     "pupils"                     );          pupils.AddMethodRoute(new MethodRoute(                 HTTPRequest.RequestMethodType.GET,                 pupilsResponse                 )             );          Resource year9 = new Resource("year9");          HTTPResponse year9Response = new HTTPResponse(                     HTTPResponse.StatusCodes.OK,                     HTTPMessage.MIMETypes.PLAIN_TEXT,                     "year 9"                     );          year9.AddMethodRoute(new MethodRoute(                 HTTPRequest.RequestMethodType.GET,                 year9Response                 )             );          Resource year10 = new Resource("year10");          HTTPResponse year10Response = new HTTPResponse(                     HTTPResponse.StatusCodes.OK,                     HTTPMessage.MIMETypes.PLAIN_TEXT,                     "year 10"                     );          year10.AddMethodRoute(new MethodRoute(                 HTTPRequest.RequestMethodType.GET,                 year10Response                 )             );          pupils.AddSubResource(year10);         pupils.AddSubResource(year9);          Resource school = new Resource("school");          HTTPResponse schoolResponse = new HTTPResponse(                     HTTPResponse.StatusCodes.OK,                     HTTPMessage.MIMETypes.PLAIN_TEXT,                     "school"                     );          school.AddMethodRoute(new MethodRoute(                 HTTPRequest.RequestMethodType.GET,                 schoolResponse                 )             );          HTTPServer Server = new HTTPServer(3000, school);          Server.AddSubRoute(pupils);          Server.Listen(); 

This seems a bit of a clunky way to set up routing. How would I go about adding some kind of controller? I was thinking of using callback methods but I want some kind of controller class like in ASP.NET MVC… Any ideas of how to make this neater and cleaner?? Just to point me in the right direction would be awesome!

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.