Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Jan 8, 2018
0 parents commit 8a6b6f0
Show file tree
Hide file tree
Showing 44 changed files with 3,767 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .cfignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.build/*
.build-ubuntu/*
Packages/*
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dockerfile
Dockerfile-tools
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.DS_Store
/.build
/.build-ubuntu
/Packages
/config.json
/spec.json
/ssl
./config/github.json
config/github.json
/PlaygroundServer.xcodeproj

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/*
xcuserdata

node_modules
package-lock.json
Package.resolved
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4.0.3
6 changes: 6 additions & 0 deletions BuildVersioningForSSL.xcconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Workaround for SSL https://github.com/IBM-Swift/Kitura/issues/1203

INFOPLIST_FILE = PlaygroundServer.xcodeproj/PlaygroundServer_Info.plist
CREATE_INFOPLIST_SECTION_IN_BINARY = YES
CURRENT_PROJECT_VERSION = 1
VERSIONING_SYSTEM = apple-generic
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
By submitting a pull request, you represent that you have the right to license
your contribution to Marcin Krzyżanowski and the community, and agree by submitting the patch
that your contributions are licensed under the commercial licence.

---

Before submitting the pull request, please make sure you have tested your
changes.
27 changes: 27 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FROM ibmcom/swift-ubuntu-runtime:4.0
MAINTAINER IBM Swift Engineering at IBM Cloud
LABEL Description="Template Dockerfile that extends the ibmcom/swift-ubuntu-runtime image."

# We can replace this port with what the user wants
EXPOSE 8080

# Default user if not provided
ARG bx_dev_user=root
ARG bx_dev_userid=1000

# Install system level packages
# RUN apt-get update && apt-get dist-upgrade -y

# Add utils files
ADD https://raw.githubusercontent.com/IBM-Swift/swift-ubuntu-docker/master/utils/run-utils.sh /swift-utils/run-utils.sh
ADD https://raw.githubusercontent.com/IBM-Swift/swift-ubuntu-docker/master/utils/common-utils.sh /swift-utils/common-utils.sh
RUN chmod -R 555 /swift-utils

# Create user if not root
RUN if [ $bx_dev_user != "root" ]; then useradd -ms /bin/bash -u $bx_dev_userid $bx_dev_user; fi

# Bundle application source & binaries
COPY . /swift-project

# Command to start Swift application
CMD [ "sh", "-c", "cd /swift-project && .build-ubuntu/release/PlaygroundServer" ]
24 changes: 24 additions & 0 deletions Dockerfile-tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ibmcom/swift-ubuntu:4.0
MAINTAINER IBM Swift Engineering at IBM Cloud
LABEL Description="Template Dockerfile that extends the ibmcom/swift-ubuntu image."

# We can replace this port with what the user wants
EXPOSE 8080 1024 1025

# Default user if not provided
ARG bx_dev_user=root
ARG bx_dev_userid=1000

# Install system level packages
# RUN apt-get update && apt-get dist-upgrade -y

# Add utils files
ADD https://raw.githubusercontent.com/IBM-Swift/swift-ubuntu-docker/master/utils/tools-utils.sh /swift-utils/tools-utils.sh
ADD https://raw.githubusercontent.com/IBM-Swift/swift-ubuntu-docker/master/utils/common-utils.sh /swift-utils/common-utils.sh
RUN chmod -R 555 /swift-utils

# Create user if not root
RUN if [ "$bx_dev_user" != root ]; then useradd -ms /bin/bash -u $bx_dev_userid $bx_dev_user; fi

# Bundle application source & binaries
COPY . /swift-project
Empty file added Frameworks/.keep
Empty file.
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright (C) Marcin Krzyżanowski <[email protected]>
This software is provided 'as-is', without any express or implied warranty.

In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

- The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
- Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- This notice may not be removed or altered from any source or binary distribution.
- For non-commercial use only.
23 changes: 23 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "PlaygroundServer",
dependencies: [
.package(url: "https://github.com/IBM-Swift/Kitura.git", from: "2.1.0"),
.package(url: "https://github.com/IBM-Swift/Kitura-WebSocket.git", from: "1.0.0"),
.package(url: "https://github.com/IBM-Swift/HeliumLogger.git", from: "1.7.1"),
.package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", from: "6.0.0"),
.package(url: "https://github.com/IBM-Swift/Configuration.git", from: "3.0.0"),
.package(url: "https://github.com/apple/swift-package-manager.git", from: "0.1.0"),
.package(url: "https://github.com/IBM-Swift/Kitura-StencilTemplateEngine.git", from: "1.8.4"),
.package(url: "https://github.com/krzyzanowskim/Kitura-CredentialsGitHub.git", .branch("master")), // Fix dependency chain
.package(url: "https://github.com/yaslab/CSV.swift", from: "2.1.0"),
],
targets: [
.target(name: "PlaygroundServer", dependencies: [ .target(name: "Application"), "Kitura" , "HeliumLogger"]),
.target(name: "Application", dependencies: [ "Kitura", "Kitura-WebSocket", "Configuration", "CloudEnvironment", "Utility", "CredentialsGitHub", "KituraStencil", "CSV"], exclude: ["DerivedData","public","node_modules"]),

.testTarget(name: "ApplicationTests" , dependencies: [.target(name: "Application"), "Kitura","HeliumLogger" ])
]
)
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SwiftPlayground.run

Online Swift Playground. Implemented in Swift.

TBA. Checkout http://SwiftPlayground.run

## Installation

```
$ git clone https://github.com/krzyzanowskim/OnlineSwiftPlayground.git
$ cd swiftplayground
$ npm install
$ swift run -c release
```

## Config

Third party frameworks should be copied to `Frameworks` directory.

See `config/` for GitHub auth. sample config.

## Author

SwiftPlayground.run is owned and maintained by [Marcin Krzyżanowski](http://www.krzyzanowskim.com)

You can follow me on Twitter at [@krzyzanowskim](http://twitter.com/krzyzanowskim) for project updates and releases.

## License

See LICENSE file.
20 changes: 20 additions & 0 deletions Sources/Application/Annotation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Marcin Krzyżanowski <[email protected]>

import Foundation

enum AnnotationType: String, Codable {
case error = "error"
case warning = "warning"
case notice = "notice"
}

struct AnnotationLocation: Codable {
let row: Int
let column: Int
}

struct Annotation: Codable {
let type: AnnotationType
let location: AnnotationLocation
let description: String
}
73 changes: 73 additions & 0 deletions Sources/Application/Application.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright Marcin Krzyżanowski <[email protected]>

import Foundation
import Kitura
import KituraWebSocket
import LoggerAPI
import Configuration
import KituraSession
import Credentials
import KituraStencil
import FileKit
import Basic

public let projectPath = ConfigurationManager.BasePath.project.path

enum Error: Swift.Error {
case missingConfig
}

public class App {
let router: Router
let server: Server
let credentials: Credentials?
static let defaultContext:[String: Any] = [:]

public init() throws {
router = Router()

server = Server(router: router)

// UUID secret for the session. Session is not persistent
router.all(middleware: Session(secret: UUID().uuidString))

credentials = try setupCredentials(router: router)
}

func postInit() throws {
router.add(templateEngine: StencilTemplateEngine(), forFileExtensions: ["html"])

// Common endpoints
router.all("/static", middleware: StaticFileServer(path: "./static", options: StaticFileServer.Options(serveIndexForDirectory: false)))

router.get("/signin") { request, response, next in
response.headers["Content-Type"] = "text/html"
try response.render("signin.stencil.html", context: App.defaultContext).end()
}

router.get("/logout") { request, response, next in
self.credentials?.logOut(request: request)
try response.render("logout.stencil.html", context: App.defaultContext).end()
}

router.get("/") { request, response, next in
try response.render("playground.stencil.html", context: App.defaultContext)
next()
}

router.get("/embed") { request, response, next in
var context = App.defaultContext
context["embed"] = "1"
try response.render("playground.stencil.html", context: context)
next()
}

// WebSockets
WebSocket.register(service: TerminalService(), onPath: "terminal")
}

public func run() throws {
try postInit()
Server.init(router: router).run()
}
}
43 changes: 43 additions & 0 deletions Sources/Application/Authentication.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright Marcin Krzyżanowski <[email protected]>

import LoggerAPI
import Kitura
import KituraStencil
import Credentials
import CredentialsGitHub
import Cryptor
import SwiftyJSON
import Foundation

func setupCredentials(router: Router) throws -> Credentials? {
guard let githubConfig = Config.shared["github"] else {
Log.warning("Unable to load config/github.json setup. Authorisation disabled.")
return nil
}

let githubCredentials = CredentialsGitHub(clientId: githubConfig["clientId"] as! String,
clientSecret: githubConfig["clientSecret"] as! String,
callbackUrl: "", // callbackUrl is ignored by the GitHub anyway
userAgent: "SwiftPlayground.run",
options: ["scopes": ["user:email"]])

let credentials = Credentials()
credentials.register(plugin: githubCredentials)
credentials.options["failureRedirect"] = "/signin"
credentials.options["successRedirect"] = "/"
credentials.options["state"] = Data(bytes: try Random.generate(byteCount: 20)).base64EncodedString()
credentials.options["allow_signup"] = "true"

router.get("/login/github") { (request, response, next) in
let authRouterHandler = credentials.authenticate(credentialsType: githubCredentials.name)
try authRouterHandler(request, response, next)
}

router.get("/login/github/callback", handler: credentials.authenticate(credentialsType: githubCredentials.name))

#if !Xcode
router.all("/", allowPartialMatch: false, middleware: credentials)
#endif

return credentials
}
Loading

0 comments on commit 8a6b6f0

Please sign in to comment.