GithubHelp home page GithubHelp logo

perfect-sessionstorer's Introduction

Perfect-SessionStorer

Perfect Session Storer is a really easy to use data storage solution for your Perfect based Swift web app. Session Storer is type safe and can be indexed just like a dictionary.

Overview

Session Storer uses Swift Generics and standard Cocoa programming patterns. There is the main SessionStorer class and it has a required Data Source and an optional delegate.

  • SessionStorer<T>

    • This is the primary class. Instantiate this to hold data of any type.
  • SessionStorerDataSource<T> (required)

    • Subclass this and use it to hook the SessionStorer up to some sort of Database or other storage solution.
    • SessionStorerInMemoryDataSource - A built in Data Source that stores the data in memory.
  • SessionStorerDelegate<T> (optional)

    • Subclass this to get in-between saving and retrieving data.
    • One reason to do this is to encrypt / decrypt data between your server code and the data being stored in the data source.
  • The last two items should really be protocols. But because of the complexities of Generics when used with protocols, it was much simpler to implement and to use with abstract super classes. When swift supports generic protocols, I will switch over to that.

Getting Started - The Easy Way

  1. Add the dependency to your Package.swift file
    • .Package(url: "https://github.com/jeffreybergier/Perfect-SessionStorer.git", majorVersion: 0, minor: 0)
  2. Choose whether you want to use the String or Any Singletons (you can use both if you want as well)
  3. Add the correct filter to the Perfect HTTP Server in the same file that you add your routes
    • server.setRequestFilters([(SessionInMemoryStringStorer.shared.filter, .low)])
  4. Store data in the Storer in the function handler for any route.
    • SessionInMemoryStringStorer.shared[request, "Username"] = "ToyFanBoi99"
  5. Retrieve data from another request from the same browser
    • let username = SessionInMemoryStringStorer.shared[request, "Username"] //optional("ToyFanBoi99")

The singletons provided store data in memory. There is no persistence of this data. So if the server gets killed or crashes, all the data is lost. I hope to be able to provide a SQLite Data Store soon. Also, the default cookie length is 1 year. Data is automatically cleared out by the SessionStorer after it expires.

import PerfectSessionStorer

// Register the SessionStorer Filter with the servers request filters
server.setRequestFilters([(SessionInMemoryStringStorer.shared.filter, .low)])

routes.add(method: .get, uri: "/") { request, response in
    // Read out of the SessionStorer
    let username = SessionInMemoryStringStorer.shared[request, "username"] ?? "USER_NOT_LOGGED_IN"
    //...
}
routes.add(method: .get, uri: "/login") { request, response in
    // Save into the SessionStorer
    SessionInMemoryStringStorer.shared[request, "username"] = request.param(name: "username")
    //...
}

perfect-sessionstorer's People

Contributors

jeffreybergier avatar

Watchers

James Cloos avatar  avatar

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.