
Cookies in Golf and HAProxy: A Practical Guide to Web App State Management
Cookies are vital for modern web applications. They enable developers to store small pieces of information on a user's device, improving user experience by remembering preferences and session data. This guide explores how to implement cookies in Golf and integrate it with HAProxy for robust web serving.
What are Cookies and Why Should You Use Them?
Cookies store application state on the client side. This avoids repeatedly asking users for the same information.
Here's how they're used in web development:
- Remembering Usernames: Automatically populate login fields.
- Preserving Session State: Maintain user login status across multiple pages.
- Tracking Preferences: Store user-specific settings like language or theme.
Setting and Retrieving Cookies in Golf: A Step-by-Step Example
Let's create a simple Golf application called "yum" to demonstrate cookie handling. This example sets a cookie containing the user's name.
-
Create the application directory:
-
Create the
biscuit.golf
source file: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="<<p-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 <<p-web name>> @<hr/> else-if @Unrecognized action<hr/> end-if end-handler
The code handles three actions: "enter-cookie" displays a form, "save-cookie" saves the cookie, and "query-cookie" retrieves and displays the cookie value.
-
Compile the application:
-
Start the Golf server:
This runs the application on port 3000.
Integrating Golf with HAProxy for Enhanced Performance
HAProxy acts as a reverse proxy and load balancer, improving the performance and reliability of your Golf application.
HAProxy Configuration
Edit your HAProxy configuration file (typically /etc/haproxy/haproxy.cfg
) with the following settings:
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 /var/lib/gg/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
This configuration directs requests to /yum/
to your Golf application running on port 3000 using FastCGI. This setup allows HAProxy to handle incoming traffic efficiently.
Key HAProxy Configuration Points:
frontend
: Defines the public-facing server (port 90 in this case).backend
: Specifies the backend servers (your Golf application).fcgi-app
: Enables FastCGI protocol for communication.cookies with HAProxy
: Properly configured cookies and HAProxy ensures session persistence and optimal user experience.
Restart HAProxy:
Testing Your Cookies and HAProxy Setup
-
Open your browser and access the following URL:
http://127.0.0.1:90/yum/biscuit/action=enter-cookie
-
Enter your name and submit the form. You should see confirmation that the cookie was saved.
-
Query the cookie:
http://127.0.0.1:90/yum/biscuit/action=query-cookie
Your name should be displayed, retrieved from the cookie stored in your browser. This confirms that using Golf with HAProxy is functioning correctly.
Alternatives to HAProxy
While HAProxy is excellent, you can also use other web servers like Apache or Nginx with Golf. The key is to configure them to use FastCGI to communicate with your Golf application.
Conclusion
This guide demonstrated how to implement cookies in your Golf applications and integrate them with HAProxy for enhanced performance and scalability. By understanding cookie handling and utilizing reverse proxies, you can build robust and user-friendly web services.