Skip to content

Instantly share code, notes, and snippets.

@rbwendt
Last active October 5, 2016 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbwendt/074150835be9d0b6ad19e01b37327c18 to your computer and use it in GitHub Desktop.
Save rbwendt/074150835be9d0b6ad19e01b37327c18 to your computer and use it in GitHub Desktop.
dumb Perfect blog
<!doctype html>
<html>
<head>
<title>Blog</title>
</head>
<body>
<h1>A blog</h1>
<div id="app">
<article v-for="item in items">
<h2>{{ item.title }}</h2>
<h3>by {{ item.author}}</h3>
<h4>{{ item.date }}</h4>
<section class="content" v-html="item.content"></section
</article>
</div>
<script src="https://cdn.jsdelivr.net/vue/1.0.20/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/markdown-it/8.0.0/markdown-it.min.js"></script>
<script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>
<script>
vm = new Vue({
el: '#app',
data: {
items:[]
},
methods: {
fetchData: function() {
md = window.markdownit()
this.$http.get('/api/posts').then((response) => {
this.$set('items', JSON.parse(response.body).items.map((item) => {
item.content = md.render(item.content)
return item
}))
})
}
}
})
vm.fetchData()
</script>
</body>
</html>
//
// main.swift
// PerfectTemplate
//
// Created by Kyle Jessup on 2015-11-05.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
import PerfectLib
import PerfectHTTP
import PerfectHTTPServer
// Create HTTP server.
let server = HTTPServer()
// Register your own routes and handlers
var routes = Routes()
routes.add(method: .get, uri: "/api/posts", handler: {
request, response in
response.setHeader(.contentType, value: "text/json")
let docRoot = request.documentRoot
let author = "Ben"
var items: [[String:String]] = []
let dir = Dir("\(docRoot)/posts")
do {
try dir.forEachEntry(closure: {
element in
let contentFile = File("\(docRoot)/posts/\(element)")
let content = try contentFile.readString()
items.append(["title":"title", "author": author, "content": content])
})
} catch {
response.status = .internalServerError
response.setBody(string: "Error reading posts directory")
}
let d: [String:Any] = ["items":items]
do {
try response.setBody(json: d)
} catch {
response.status = .internalServerError
response.setBody(string: "Error handling request: \(error)")
}
response.completed()
}
)
routes.add(method: .get, uri: "/", handler: {
request, response in
do {
let docRoot = request.documentRoot
let indexFile = File("\(docRoot)/index.html")
let indexSize = indexFile.size
let indexBytes = try indexFile.readSomeBytes(count: indexSize)
response.setHeader(.contentType, value: MimeType.forExtension("html"))
response.setHeader(.contentLength, value: "\(indexBytes.count)")
response.setBody(bytes: indexBytes)
} catch {
response.status = .internalServerError
response.setBody(string: "Error handling request: \(error)")
}
response.completed()
})
// Add the routes to the server.
server.addRoutes(routes)
// Set a listen port of 8181
server.serverPort = 8181
// Set a document root.
// This is optional. If you do not want to serve static content then do not set this.
// Setting the document root will automatically add a static file handler for the route /**
server.documentRoot = "./webroot"
// Gather command line options and further configure the server.
// Run the server with --help to see the list of supported arguments.
// Command line arguments will supplant any of the values set above.
configureServer(server)
do {
// Launch the HTTP server.
try server.start()
} catch PerfectError.networkError(let err, let msg) {
print("Network error thrown: \(err) \(msg)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment