Skip to content

0xzrf/zig-http

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

INTRO

This is an HTTP server written in ZIG from scratch, that allows you to do CRUD operation on a contact server, serving just the backend Ideally, this should be able to handle request from both browser and curl.

DESIGN

We typically get a HTTP request like so:

METHOD /path?query HTTP/1.1\r\n
Header-Name: Header-Value\r\n
Another-Header: value\r\n
\r\n
[optional body]

The main information is stored before the optional body. A parser needs to reliably parse this information, and respond to it appropriately.

A response would look something like this:

HTTP/1.1 200 OK\r\n
Content-Type: text/html\r\n
Content-Length: 42\r\n
\r\n
[body]

What's supported in this project

This is just a learning project, so it'll only support a subset of features provided by HTTP:

Methods

  1. GET
  2. POST
  3. DELETE
  4. PUT

Status Code

  1. 2xx - Success: 200 OK, 201 Created
  2. 4xx - Client error: 400 Bad Request, 401 Unauthorized, 404 Not Found, 405 Method Not Allowed
  3. 5xx - Server Error: 500 Internal Server Error, 501 Not Implemented

Supported routes

  1. /: Gives you a good Programming introductory Hello World! screen
  2. /get-contact?contact=$: Gives you the contact's phone saved on db
  3. /del-contact: Deletes a contact from db
  4. /update-contact: updates an existing contact
  5. /upload-contact: Uploads a new contact. Errors if it already exists

How to run

Pre-requisite: Download zig from ther official website

Also, you'd probably wanna change the postgres db address in the following to make it fully work(env is a pain in the neck in zig rn):

var db = DB.new("postgresql://zerefdegnl@localhost:5432/myapp", gpa.*, io.*) catch {

Then, run to start the server:

make build_and_run

After setting up the db and running the command, you should see this:

TCP listening on http://0.0.0.0:3636

Go to the website, and have fun!

About

A lightweight http server written in zig to save and retrieve contacts, serving both frontend and backend

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors