Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1580

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T09:29:08+00:00 2024-11-25T09:29:08+00:00

How to build a basic REST API in Lua – Milua micro framework

  • 62k

Prelude

I recently started a new project, for which I wanted to build a REST API. Here are the alternatives I considered and why I rejected them:

  • Using a framework I'm more familiar with, in a different language, like Flask for Python. My project already required Lua and used a bunch of different technologies, so I'd prefer to keep it simple and not add a whole new language and framework to the list.
  • Using an existing framework in Lua, like Lapis. The problem here was that Lapis relies on third-party software, like Nginx, so the dependencies list would keep growing. This wouldn't be a problem in a more heavy project, but the API I need here is really small, so I would consider overkill anything more than a single technology for it.

Cover image by aloiswohlfahrt from Pixabay

Milua

I felt clearly that what I really wanted was a pure Lua solution. After a bit of research I didn't find any de-facto standard (I might be wrong), but I did find a http library by duarnimator that provided an excellent set of functionalities to launch a simple server in a few lines.

So inspired by the experience of using Flask I made Milua

GitHub logo MiguelMJ / Milua

Lua micro framework for web development

Installation

I learned how the luarocks, a lua package manager, works and published the first version, so you can install it with it's dependencies only with:

  luarocks install milua   
Enter fullscreen mode Exit fullscreen mode

Examples

Right after, you can try the example that comes in the repository.

  local app = require "milua"  -- Basic example app.add_handler(     "GET",     "/",     function()         return "<h1>Welcome to the handsome server!</h1>"     end )  -- Example capturing a path variable app.add_handler(     "GET",     "/user/...",      function (captures, query, headers)          local username = captures[1]         local times = query.times or 1         return "The user " .. username .. " is" ..                (" very"):rep(times) .. " handsome"      end )  app.start()   
Enter fullscreen mode Exit fullscreen mode

Launching this would just require you to execute the script after installing Milua. Then, you can test it with curl.

  $ curl localhost:8800/ <h1>Welcome to the handsome server!</h1>   $ curl localhost:8800/user/foo The user foo is very handsome  $ curl localhost:8800/user/foo?times=3 The user foo is very very very handsome   
Enter fullscreen mode Exit fullscreen mode

Features

For now, version 0.1 as I write, the milua module only offers two functions:

  • add_handler(method, pattern, handler) to associate a method and a path to a handler.
    • The handler function must accept the following arguments:
      • captures: An array with the variables fields of the path, specified with three dots (...) in the pattern string. In fact, this pattern is just a regular Lua pattern with some syntactic sugar, so you can capture anything you like in your path and be as specific as you want: /([0-9]+) would capture a path to a numeric value; /admin_... would capture the destination without the prefix admin_.
      • query: A table with the key-value pairs of the query in the URL (the options that come after the ?).
      • headers: The headers of the HTTP request.
      • body: The body of the HTTP request.
    • and must return the following values:
      • The body of the repsonse.
      • (Optional) A table with the headers of the response.
  • start(config) where config contains the host and the port to run the application.

Conclusion

There is still room for a couple of modifications that would keep the complexity at the same level, like adding error handlers (to allow a 404.html page, for example) or specifying directories for static files. For example, it has no templating functionalities (maybe in a future), but you can require any of your preference and use it in your app. But for now it servers its purpose and it's compatible with anything you want to throw at it, thanks to its minimal nature.


I'm still learning about Lua and its tools and might write more about this language in the future. What's your experience with Lua? Have you used before for web development? I'll be happy to read any comment you have!

Recommended reading

miguelmj

Data structures in Prolog – Where to start

MiguelMJ ・ Jun 15 '21

#prolog #datastructures #resources

miguelmj

How to get answers on StackOverflow

MiguelMJ ・ Feb 6 '22

#beginners #codenewbie #productivity

You can follow me on Twitter! 🐦

apiluawebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.