Skip to content

leyuskckiran1510/peanut-butter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

peanut-butter

Simple miniframework for web-backend Aim of this project to build a miniframework to get started with the web with speed out of the box.
Explore the docs »

View Demo · Report Bug · Request Feature

Features

  1. Static HTML server with costume route [completed]
  2. Server static files [2024-Aug-04] [completed]
  3. Extended route features
    1. In route variables [2024-Aug-06] [completed]
    2. Regex route [2024-Aug-05] [cancled for now]
  4. HTML templating [2024-Aug-06] [started-working]
    • [2024-Aug-06], template copying works, now just swaping the template varibale is left
    • [completed][2024-Aug-07:23-11-33 PM]
  5. Redirect, [completed] [2024-Aug-07:23-11-17 PM]
  6. urlquery_parse [completed] [2024-Aug-08]
  7. A good logo
  8. Parse Post forms/urls, and fix the civetweb and make PR, [2024-Aug-13:23-11-03 PM] [ I didnot know about the standard of file upload ] completed [2024-Aug-21:22-10-21 PM]
  9. Database support i. starting with sqlite the-OG a. support for basic sqlite completed
  10. Add Costume DDL, support to define tables and relations

Features To add

  1. Middlewares/Authentications/Authorizations
  2. Persistent datas like Cookies and Sessions,
  3. RateLimiting
  4. HTTPS/test/support

Technology and Software in Use

  1. civetweb
    1. Building Civetweb

Got starting point from

  1. https://medium.com/@justup1080/tutorial-creating-a-minimalist-http-server-in-c-2303d140c725

How To use

  1. Clone the repo
  2. Open src/main.c
  3. Write your backend logic, example shown below
#define LOG_LEVEL 4
#include <string.h>
#include "peanut_butter.h"
#include "logger.h"
ROUTED(home){
    if(is_method("GET")){
        return render_html("/htmls/index.html");
    }
    return render_html("/htmls/method_not_allowed.html");
}

URL_VAR_ROUTED(user_home){    
    TEMPLATE_INIT();
    if(URL_VAR_INDEX(0,i)==123){
        QUERY_INIT();
        for (int i = 0; i < QUERY_LENGTH(); ++i){
            TEMPLATE_ASSIGN(QUERY_INDEX(i).name,QUERY_INDEX(i).value,s);
        }
        return render_template("htmls/template.html");;
    }
    if(URL_VAR_INDEX(0,i) < 123){
        return render_html("htmls/non_admin.html");
    }
    return redirect("/",302);
}


ROUTED(about){
    render_html("htmls/about.html");
}

URL_VAR_ROUTED(contact){
    TEMPLATE_INIT();
    if(!strcmp(URL_VAR_INDEX(0,s),"admin")){
        TEMPLATE_ASSIGN("name","Admin",s);
        TEMPLATE_ASSIGN("phone","+977-98000000000",s);
        TEMPLATE_ASSIGN("address","somewhere on earth",s);

    }else if(!strcmp(URL_VAR_INDEX(0,s),"manager")){
            TEMPLATE_ASSIGN("name","Manager",s);
            TEMPLATE_ASSIGN("phone","+977-98000000000",s);
            TEMPLATE_ASSIGN("address","somewhere on hell",s);
    }else{
        TEMPLATE_ASSIGN("name",URL_VAR_INDEX(0,s),s);
        TEMPLATE_ASSIGN("phone","---",s);
        TEMPLATE_ASSIGN("address","unknown user",s);
    }
    return render_template("htmls/contact.html");
}



ROUTED(forms){
    if(is_method("GET")){
        return render_html("htmls/forms.html");
    }
    else if (is_method("POST")){
        FORM_INIT(false);
        log_debug("1. { text, %s} ", FORM_GET("text-input1","default_value1"));
        log_debug("2. { filename, %s} ", FORM_GET_FILE("resume"));
    }
    return redirect("/forms",302);
}

int server(){
    URL("/",home);
    URL("/about",about);
    URL("/forms",forms);
    VAR_URL("/%d/home",user_home);
    VAR_URL("/%s/contact",contact);
    return server_run("1234");
}

int main(void){
    return server();
}
  1. now compile and run
make
  1. And You are good to go.

FAQ

Releases

No releases published

Packages

No packages published

Languages