Lädt...

🔧 Web framework for C programming language


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Golf has an "extended" mode, which allows for C code to be used directly. Normally, this is not allowed, but if you use extended-mode statement in your .golf file, then you can call C code from it.

In extended mode, Golf is effectively a web framework for C programming language; read this for more about implications on memory safety.

Here's the example of using Golf as a web framework for C. In this case it's a simple C function to calculate the factorial of a a number. Then we'll expose this functionality in an equally simple web application.

Create the Golf application

mkdir -p c-for-web
cd c-for-web

Create "fact" application ("-k"):

gg -k fact

Save the following into a file "calc-fact.golf". This is the web service which will interact with web clients (such as browsers); it will also call the C function we'll create a bit further below:

extended-mode // extended mode in order to call C code

%% /calc-fact public
    get-param par // get input parameter from URL
    string-number par to num // convert to number
    set-number result 
    call-extended factorial(num, &result) // call C function
    // Output result
    @Factorial of <<p-out par>> is <<p-num result>>
%%

Add C code to Golf application

Next, create C file "factorial.c". This is the C function we're calling from Golf code above:

#include "golf.h"

// Compute factorial of f, and store result into res
void factorial(gg_num f, gg_num *res)
{
    *res = 1;
    gg_num i;
    for (i = 2; i <= f; i++) {
        *res *= i;
    }
}

And an include file "factorial.h" to declare any function(s) we will use in Golf code:

void factorial(gg_num f, gg_num *res);

Compile and start the server

Golf will pick up all .golf as well as all .c and .h file and make them into an application:

gg -q 

Start the server:

mgrg fact

Test the server from command line

You can test this web application from command line (note --service flag!). This is useful because you can develop web applications just from command line:

gg -r --req=/calc-fact/par=12 --exec --service

The result is:

Content-Type: text/html;charset=utf-8
Cache-Control: max-age=0, no-cache
Pragma: no-cache
Status: 200 OK

Factorial of 12 is 479001600

This is the exact data that would be sent to an actual web client.

Test the server with web browser

Let's test on an actual web server. In this case we'll use Nginx. Also, we'll do it locally on your computer, since not everyone has a server on the web.

First, edit Nginx config file, which for Ubuntu/Debian is at:

/etc/nginx/sites-enabled/default

and on Fedora/RedHat is at:

/etc/nginx/nginx.conf

Note you might have to use "sudo" to edit these files as they're generally not writable by ordinary users.

Find the "server" section, and add this inside it:

location /fact/ { include /etc/nginx/fastcgi_params; fastcgi_pass  unix:///var/lib/gg/fact/sock/sock; }

This will connect Nginx with the "fact" server you started above. Any requests with a URL path that start with "/fact/" will be delivered to your server, and the reply will go in reverse, ultimately being delivered to the client (such as web browser).

Restart Nginx server for this to take effect:

sudo systemctl restart nginx

Try it from web browser with this URL:

http://127.0.0.1/fact/calc-fact/par=8

If your Nginx runs on another port, specify it, for instance if it runs on port 81, use "127.0.0.1:81" instead.

The result is:

Factorial of 8 is 40320

Of course, if you have a server on the web, just substitute "127.0.0.1" for your server's web address.

...

🐧 Framework announce the Framework Desktop, Framework Laptop 12 and upgraded Framework Laptop 13


📈 23.5 Punkte
🐧 Linux Tipps

🔧 Web framework for C programming language


📈 22.71 Punkte
🔧 Programmierung

🔧 SQL vs Programming Languages: Can SQL be Considered a Programming Language?


📈 21.03 Punkte
🔧 Programmierung

🔧 Learn Programming: What Programming Language Should I Learn First?


📈 21.03 Punkte
🔧 Programmierung

🔧 Presenting Assistant Language. A programming language created with GPT3


📈 20.05 Punkte
🔧 Programmierung

🔧 The Programming Language Showdown: Why Passion for Solutions Matters More Than Language Debates


📈 20.05 Punkte
🔧 Programmierung

🔧 The Next Big Language: How to Choose the Perfect Programming Language to Learn.


📈 20.05 Punkte
🔧 Programmierung

🎥 Advanced Language Understanding &amp; Language Generation with Bot Framework Composer


📈 18.58 Punkte
🎥 Video | Youtube

🎥 Advanced Language Understanding &amp; Language Generation with Bot Framework Composer


📈 18.58 Punkte
🎥 Video | Youtube

🔧 Which Tool and Programming Language to Choose for Web Scraping


📈 16.84 Punkte
🔧 Programmierung

📰 Which Web Programming Language Is The Most Secure? | UpGuard


📈 16.84 Punkte
📰 IT Security Nachrichten

🔧 I Created a Programming Language to Build Front-End Web Apps Much Faster


📈 16.84 Punkte
🔧 Programmierung

🐧 CBLT — A Safe, Fast, and Minimalistic Web Server in Rust Programming Language


📈 16.84 Punkte
🐧 Linux Tipps

🔧 How I Built a Mini Programming Language for Automatic Web Page Generation – Step by Step


📈 16.84 Punkte
🔧 Programmierung

🔧 JavaScript – A programming language used for web development.


📈 16.84 Punkte
🔧 Programmierung

🕵️ Tishna: An Automated Pentest Framework for Web Servers, Web Applications to Web Security


📈 15.31 Punkte
🕵️ Hacking

💾 TIDoS Framework- The Offensive Manual Web Application Penetration Testing Framework


📈 14.9 Punkte
💾 IT Security Tools

💾 TIDoS Framework- The Offensive Manual Web Application Penetration Testing Framework


📈 14.9 Punkte
💾 IT Security Tools

matomo