Thanks for the response to a rather general question. I'll definitely have a look at your code for Racket stories, which is live now if I see correctly. Nice!
I guess one concrete thing that I find surprisingly hard in my code is to get rid of the number of times I repeat the structure of the data: 1. write the SQL code to create the database 2. write the interface in Racket that deals with the database directly (transform into Racket values, but it's still in the same groups) 3. write functions that need to know about this structure, because they extract the information from part 2, but different fields for different uses 4. write the view that - quite often - looks like a subset of the database and in my case calls functions from part 3, which again repeats huge parts of what is in part 1 So if I have a user with 5 fields I am interested in, I find that if I change part of my database schema, I usually update 3 (or more) places, which tells me that I didn't get the abstractions right. (And I haven't added tests, or else I'm sure I'd have another layer or 2 of repetition.) The continuation passing style actually suits my use cases much better than storing state in the database -- except that I need to be able later on to recreate the path a user took, in order to figure out what happened, so I have to store it in an easy-to-interpret way in the database. My guess is that I don't grok MVC, and therefore end up with all these repetitions, so I'll have a look at your web site -- although part of the reason I don't grok it may be that most beginner resources have websites that are less stateful than what I do, which is much more like a game with many action nodes and randomization. I should probably have a look at Realm of Racket and see if I can't port the big-bang ideas to the web server. Thanks for the reply and the tutorial, I'll have a look. Cheers, Marc On Mon, Sep 30, 2019 at 2:48 PM Jens Axel Søgaard <jensa...@soegaard.net> wrote: > Hi Marc, > > Den tir. 24. sep. 2019 kl. 21.28 skrev Marc Kaufmann < > marc.kaufman...@gmail.com>: > >> TL;DR: What are some patterns/approaches for web sites for which Racket >> is particularly suited *and* for which you can point at decently written up >> examples in Racket or other languages that share such features? >> > > A big question on which I will punt. > > >> Much longer me: >> >> I spent much of summer implementing online web sites with quite a bit of >> state (do step 1; if answer a do step 2, if answer b do step 3; keep >> forking, randomize some things in between). The first website I did in >> Django, because 2 years ago when I rolled my own in Racket, I ended up with >> an unmaintainable mess of code that had sql and sexpr sprinkled across all >> types of files, and I didn't dare touch anything anymore. Now I learned >> that I am perfectly capable of writing an unmaintainable mess of code in >> Python too. Hence the second such web site (all are used for economic >> experiments, think surveys with a lot of randomization and if/then logic) >> is again back to Racket. >> >> The thing is that, while I know about MVC and grok it better after having >> spent time with Django, I am often at a loss to figure out where to draw >> boundaries in Racket. This is true for Django too, but there are so many >> examples out there, that it is easier to figure out what belongs where, and >> Django forces you to put some things in the models (more or less) and >> others in the views. And all the standard stuff - forms, simple SQL - has >> obvious ways of doing it. There are some things I dislike about it, but >> these are some positives. >> >> Now that I am back in Racket land, and at least partially get what >> continuations do (thanks to primarily Philip, George, Jesse, Bogdan and >> Matthew B's writings and answers on this list over the years), my code is >> vastly better than 2 years ago. Yet, I still feel that it is pretty bad, >> and I see loads of duplications that I do, sometimes due to lack of time to >> refactor, but just as often because I have no clue how to refactor it in a >> way that will not make it ridiculously fine-tuned to my current purpose. >> >> So, my question is this: what approches (if you like big ponderous words, >> what paradigms) and patterns are there to write web sites, and are you >> aware of examples that highlight them *concretely* in code in a way that >> the send/suspend/dispatch is described in the "Continue: ..." tutorial? >> Here are approaches that I know of: >> >> - MVC: Django and Rails for instance, easy to find examples. Usually >> object oriented it seems, but it's sort of what I do in Racket as well. >> - Single-Page Apps: I never implemented one, but my sense was that you >> can do MVC with it too, and it's more about whether you use JS to do things >> asynchronously. I may be wrong and there might be more to it. >> - Continuation style: What "Continue" does, and again it's not really an >> alternative to MVC, but more how you implement it (or other things). I >> still struggle a lot with doing this well, as there are few descriptions of >> it that go beyond "Continue". When I google, it brings up essentially the >> Racket crowd and something called Seaside (I had never heard of it) >> - Microservices (I do not get what it is, if it even is one thing) >> >> So what are examples in Racket or Racket-like languages (semantics, not >> syntax, so it doesn't have to look Lispy if it is functional or uses >> continuations or whatever features of Racket) that I could use to guide how >> I *could* structure my web site in a given way? Django isn't that great a >> fit since it has a pretty heavy ORM, and does lots of magic behind the >> scenes on objects, which is not trivial to map to the functional style of >> Racket. >> >> I don't know whether these are related to it, but half (all?) of my >> struggles come from trying to manage the state people are in and ending up >> passing around biggish objects, or adding 2 or more boolean fields per page >> to the database and calling the database on every new page. >> >> I understand that this is not the most well-formed question on this email >> list and it's likely I am asking the wrong question, so I am very open to >> being told "What you should be asking is ... " (such as, this is not about >> web stuff, but ...). >> > > I am still a beginner in the art of crafting web sites, but here is my > perspective. > There are many ways to organise web apps. Most patterns that work in other > languages will also work in Racket. > > A popular approach is to make the web app "stateless" meaning that all > state is stored on a database. > This makes scaling easy, just add more web-servers and let the load > balancer direct the traffic > to the web server with minimal load. > > The MVC approach fits well here. > > The model takes care of representing and storing data in the database. > All "business logic" is handled in the model as well. > > The view produces html pages based on data to be presented. > The view never calls functions provided by the model directly. > > The control receives a request, determines which model operation to call, > get any additional data and passes the the results on to the view. > > There are choices to be made at all levels. This is what makes it > confusing to begin > developing web apps. > > Should the model use plain lists/vectors, structs or objects when > representing data? > For the first choice the package `db` will do. For structs `deta` is a > nice choice and for objects `racquel`. > > How is the view representing html pages? > Some of the choices: strings, s-exprs, x-expres or html-structures? > (by html-structures I mean: define a struct for each html tag) > > Should the control use `continuations`? > > For single page apps you need basically write a javascript program that > generate requests and your control can them handle them in the same > way you would in a server side web app. > > It's worth experimenting with different approaches to see what fits your > style. > One of the goals of (soon to be released) racket-stories.com (previously > known as web-tutorial/listit) > was to experiment with different tools and at the same time have a > "production web-site" > in Racket with source available. What the site actually does is less > important. > > The source is available here and contains more comments than I usually > write: > > https://github.com/soegaard/web-tutorial > > The listit1-listit3 are the initial versions, which might become parts in > a tutorial at some point. > The almost-ready-to-release version is in version4. > > The choices I made: > - the model uses structs to represent data (and `deta` to handle the > struct to db work) > - the view uses html-structs > - the control doesn't use continuations (in order to make the web-app > stateless) > > Next up on my list of things to experiment with is "single page apps". > > /Jens Axel > > > > > -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAD7_NO57cLHL6UHDiNa0T1xN%3DsctZtBrNWvTDLRiijvEGThesg%40mail.gmail.com.