Git Product home page Git Product logo

salt's Introduction

salt icon

A Human Friendly Interface for HTTP webservers written in Crystal.

Project Status Langugea License

"Salt" icon by Creative Stall from Noun Project.

Installation

Add this to your application's shard.yml:

dependencies:
  salt:
    github: icyleaf/salt
    branch: master

Usage

require "salt"
require "salt/middlewares/session/cookie"
require "salt/middlewares/logger"

class Talk < Salt::App
  def call(env)
    env.session.set("username", "icyleaf")
    env.logger.info("Start Talking!")
    {400, { "Content-Type" => "text/plain" }, ["Can I talk to salt?"]}
  end
end

class Shout < Salt::App
  def call(env)
    call_app(env)

    env.logger.debug("Shout class")
    {status_code, headers, body.map &.upcase }
  end
end

class Speaking < Salt::App
  def call(env)
    call_app(env)

    env.logger.debug("Speaking class")
    {200, headers, ["This is Slat speaking! #{env.session.get("username")}"]}
  end
end

Salt.use Salt::Session::Cookie, secret: "<change me>"
Salt.use Salt::Logger, level: Logger::DEBUG, progname: "app"
Salt.use Shout
Salt.use Speaking

Salt.run Talk.new

Available middleware

  • ShowExceptions
  • CommonLogger
  • Logger
  • Runtime
  • Session (Cookie/Redis)
  • Head
  • File
  • Directory
  • Static
  • SendFile
  • ETag
  • BasicAuth
  • Router (lightweight)

All these components use the same interface, which is described in detail in the Salt::App specification. These optional components can be used in any way you wish.

How to Contribute

Your contributions are always welcome! Please submit a pull request or create an issue to add a new question, bug or feature to the list.

All Contributors are on the wall.

You may also like

  • halite - HTTP Requests Client with a chainable REST API, built-in sessions and loggers.
  • totem - Load and parse a configuration file or string in JSON, YAML, dotenv formats.
  • markd - Yet another markdown parser built for speed, Compliant to CommonMark specification.
  • poncho - A .env parser/loader improved for performance.
  • popcorn - Easy and Safe casting from one type to another.
  • fast-crystal - ๐Ÿ’จ Writing Fast Crystal ๐Ÿ˜ -- Collect Common Crystal idioms.

Resouces

Heavily inspired from Ruby's rack gem.

License

MIT License ยฉ icyleaf

salt's People

Contributors

icyleaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

salt's Issues

return Tuple for App::Response

In rack, it use array as response, and make it simple and less code to use:

class Talk < Salt::App
  def call(env)
    [200, { "Content-Type" => "text/plain" }, ["Can I talk to salt?"]]
  end
end

in PL #275 by @RX14 suggested use tuples instead of array, but more code to use.

class Talk < Salt::App
  def call(env)
    {
      status_code: 400,
      headers: { "Content-Type" => "text/plain" },
      body: ["Can I talk to salt?"]
    }
  end
end

Which is better?

env.session raises NotFoundMiddleware

Setup:

salt-bug $ crystal -v
Crystal 0.27.0 (2018-11-04)

LLVM: 6.0.1
Default target: x86_64-apple-macosx

salt-bug $ cat salt-bug.cr
require "salt"
require "salt/middlewares/session/cookie"

class App < Salt::App
  def call(env)
    {200, {"Content-Type" => "text/plain"}, ["OK"]}
  end
end

Salt.use(Salt::Session::Cookie, secret: "...")

Salt.run(App.new)

salt-bug $ crystal salt-bug.cr 
[11724] Salt server starting ...
[11724] * Version 0.4.2 (Crystal 0.27.0)
[11724] * Environment: development
[11724] * Listening on http://localhost:9898/
[11724] Use Ctrl-C to stop

Visit localhost:9898 in browser...

Salt::Exceptions::NotFoundMiddleware at /
Missing Session middleware, use Salt::run add it.

Server output:

Salt::Exceptions::NotFoundMiddleware: Missing Session middleware, use Salt::run add it.
    lib/salt/src/salt/environment.cr:0:9 in 'session'
    lib/salt/src/salt/middlewares/session/abstract/persisted.cr:35:7 in 'call'
    ...

I modified persisted.cr#call like:

def call(env)
  prepare_session(env)

  # Force exception here:
  env.session # raises Exceptions::NotFoundMiddleware.new("...")

  call_app(env)
  commit_session(env)

  {status_code, headers, body}
end

Inside prepare_session, calling session and even env.session is okay; seems only after exiting prepare_session does calling env.session raise the above exception.

This is my first bug-report ever, so please lemme know if you need anything else.

Also, thank-you for writing Salt. I've spent many hours learning Rack, and it's cool to see some of that experience be of use learning Salt.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.