Cookies in Golf applications, plus HAProxy


Cookies are used in web development to remember the state of your application on a client device. This way, your end-user on this client device doesn't have to provide the same (presumably fairly constant) information all the time. A "client device" can be just a plain web browser, or it can be anything else, such as any internet connected device.

In practicality, cookies are used to remember user name, session state, preferences and any other information that a web application wishes to keep on a client device.

So cookies are quite important for any web application or service. In this example, we'll set a single cookie (name of the user), and then we will retrieve it later. Very simple, but it's the foundation of what you'd need to do in pretty much any application.

First, in a separate directory, create an application "yum" (as in cookies are delicious):
mkdir cookies
cd cookies
gg -k yum

Next, create this source code file "biscuit.golf" (for everyone outside the US who feels it should have been "biscuit" instead of "cookie"):
 begin-handler /biscuit
     get-param action

     if-true action equal "enter-cookie"
         // Display a form to get cookie value
         @<h2>Enter your name</h2>
         @<form action="<<print-path "/biscuit">>" method="POST">
         @    <input type="hidden" name="action" value="save-cookie">
         @    <label for="cookie-value">Your name:</label><br/>
         @    <input type="text" name="cookie-value" value=""><br/>
         @    <br/>
         @    <input type="submit" value="Submit">
         @</form>

     else-if action equal "save-cookie"
         // Submittal of form: save the cookie through response to the browser
         get-param cookie_value
         get-time to cookie_expiration year 1 timezone "GMT"
         set-cookie "customer-name" = cookie_value  expires cookie_expiration  path "/"
         @Cookie sent to browser!
         @<hr/>

     else-if action equal "query-cookie"
         // Web request that delivers cookie value back here (to server); display it.
         get-cookie name="customer-name"
         @Customer name is <<print-out name web-encode>>
         @<hr/>

     else-if
         @Unrecognized action<hr/>
     end-if
 end-handler

The code is pretty easy: if "action" URL parameter is "enter-cookie", you can enter your name. Then, when you submit this web form (passing back "action" parameter with value "save-cookie"), the cookie named "customer-name" is saved with the value you entered, and the cookie expiration is set 1 year into the future. Then you'll query the cookie value in another request ("query-cookie" as "action" parameter), and you'll get your name back because it was saved on the client.

Okay, so compile the application (with all request handlers public):
gg -q --public

Now, start the Golf server to serve up your yummy application:
mgrg -p 3000 yum

"-p 3000" means it will run on socket port 3000, and the reason for that will become apparent momentarily.

We'll use HAProxy as a web server in this instance to demostrate that you can use Golf with pretty much any web server or load balancer. To set it up, here is the simple config file (typically in "/etc/haproxy/haproxy.cfg"). Note that "frontend", "fcgi-app" and "backend" sections are relevant to us here; the rest can be as it is by default in whatever configuration file you have (substitute "your-user" for your OS user name):
frontend front_server
    mode http
    bind *:90
    use_backend backend_servers if { path_reg -i ^.*\/yum\/.*$ }
    option forwardfor


fcgi-app golf-fcgi
    log-stderr global
    docroot /home/your-user/.golf/apps/yum/app
    path-info ^.+(/yum)(/.+)$

backend backend_servers
    mode http
    filter fcgi-app golf-fcgi
    use-fcgi-app golf-fcgi
    server s1 127.0.0.1:3000 proto fcgi

So, the HAProxy server will respond on port 90 from the web. We specify that it will redirect any URL starting with "/yum/" to your Golf application. Obviously, if you change your application name from "yum" to something else, change "yum" here too. Then we specify we'd like to use FastCGI protocol, which Golf uses because it's very fast and capable. And finally, we tell HAProxy we'd like to communicate with our server on port 3000. So that's why we used "-p 3000" above when starting the server!

Restart HAProxy for this to take effect:
sudo systemctl restart haproxy

Now you can test your application server. Open a browser, and point it to your server where all the above code took place. If you're testing this on your own local machine, then just use "127.0.0.1" - and that's what we'll do here:
http://127.0.0.1:90/yum/biscuit/action=enter-cookie

You'd get something like this:

Golf

Enter the name, and click Submit, and you'll get the message that the cookie is saved:

Golf

Now, enter this URL to query the cookie you set:
http://127.0.0.1:90/yum/biscuit/action=query-cookie

And you'll see:

Golf

So now you've learned how to use cookies in Golf, and how to use HAProxy too. Of course, you could have used Apache, Nginx or some other web server as well.
See also
Articles
article-capi  
article-cookies  
article-debug  
article-distributed  
article-encryption  
article-fetch-web-page  
article-fifo  
article-file-manager  
article-hello-server  
article-hello-world  
article-hello-world-service  
article-hello-world-service-web  
article-how-to-create-golf-application  
article-json  
article-language  
article-mariadb  
article-memory-safety  
article-memory-safety-web  
article-notes-postgres  
article-random  
article-regex  
article-remote-call  
article-request-function  
article-security  
article-sendmail  
article-server  
article-shopping  
article-sqlite  
article-statements  
article-status-check  
article-tree  
article-tree-web  
article-vim-coloring  
article-web-framework-for-c-programming-language  
article-what-is-golf  
article-what-is-web-service  
See all
documentation


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.