Skip to content

Instantly share code, notes, and snippets.

View kjessup's full-sized avatar

Kyle Jessup kjessup

  • Treefrog
  • Newmarket, Ontario
View GitHub Profile
@kjessup
kjessup / formatted_decimal.lasso
Created April 3, 2014 20:01
This object attempts to work like a decimal, but is able to store its formatting flags, much in the manner of LP8.
define formatted_decimal => type {
    data public value::decimal,
        private flags
     
    public onCreate(flags = (:)) => {
        .value = 0.0
        .flags = #flags
    }
    public onCreate(v::decimal, flags = (:)) => {
        .value = #v
@kjessup
kjessup / map_multifind.lasso
Created April 3, 2014 20:02
This is an additional implimentation of map->find which accepts two or more keys. Each key is sought for in the map and a staticarray is returned containing each result.
define map->find(one, two, ...)::staticarray =>
    (with key in (:#one, #two) + #rest or (:)
    select .find(#key))->asStaticArray
@kjessup
kjessup / string_encodebash.lasso
Created April 3, 2014 20:03
Returns the string encoded such that it can be given to bash as a literal command line argument. Note that you won't need this for arguments by default if you are using sys_process.
define string->encodeBash() => '"'+regexp('([\\$`"\\\n])', `\\$1`, self)->replaceAll+'"'
//
// JSONConvertable.swift
//
// Created by Kyle Jessup on 2016-01-21.
// Copyright © 2016 Treefrog. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version, as supplemented by the
@kjessup
kjessup / gist:605a4aace3d37591d1aba34d8f4c786f
Created June 2, 2016 04:42
Perfect 1.0 routes + mustache
public func PerfectServerModuleInit() {
Routing.Handler.registerGlobally()
Routing.Routes["GET", "/"] = { req in return Example(path: "index.mustache") }
}
struct Example: RequestHandler {
let path: String
init(path: String) {
self.path = path
@kjessup
kjessup / filter.swift
Last active July 8, 2016 17:52
Perfect 404 response filter
struct Filter404: HTTPResponseFilter {
func filterBody(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
callback(.continue)
}
func filterHeaders(response: HTTPResponse, callback: (HTTPResponseFilterResult) -> ()) {
if case .notFound = response.status {
response.bodyBytes.removeAll()
response.appendBody(string: "The file \(response.request.path) was not found.")
response.setHeader(.contentLength, value: "\(response.bodyBytes.count)")
@kjessup
kjessup / gist:b7d1d513e1ea2bec96e2311cfe6c671d
Created September 20, 2016 15:19
Perfect 2.0 -lCOpenSSL link error
-lCOpenSSL link error?
Add to the PROJECT build settings > "Library Search Paths" = $(PROJECT_DIR) as "Recursive".
Add it to the project settings as a whole, not for individual targets.
Alternatively you can generate the project with the DEVELOPMENT-SNAPSHOT-2016-09-07-a+ toolchain.
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import PerfectThread
// Create HTTPs server.
func createHTTPSServer() -> HTTPServer {
let server = HTTPServer()
server.serverPort = 8181
server.ssl = (sslCert: "cert.pem", sslKey: "key.pem")
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
import PerfectThread
import PerfectRequestLogger
Threading.getQueue(name: "nonsecureserver", type: .serial).dispatch {
let server = HTTPServer()
var routes = Routes()
routes.add(method: .get, uri: "/**", handler: {
import PerfectCURL
let urls = ["http://localhost:8080/a", "http://localhost:8080/b", "http://localhost:8080/c", "http://localhost:8080/d"]
func fetchURLS(_ urls: [String], process: (_ url: String, _ headers: String?, _ body: String?) -> ()) {
struct curlTrack {
let url: String
let curl: CURL