Skip to content

Commit

Permalink
deployed to production (#6)
Browse files Browse the repository at this point in the history
* adding server side configuration
* switching back to HttpClient
* making blog list async
* fixing code for ga
* renamed command for server
* fixing configuration for supervisor with queues
  • Loading branch information
leogdion authored Aug 9, 2020
1 parent 4aeab05 commit f3d3d05
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 171 deletions.
21 changes: 21 additions & 0 deletions Configuration/etc/nginx/sites-available/orchardnest.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
server {
server_name orchardnest.com;
listen 80;

root /home/orchardnest/app/Public;

location / {
try_files $uri @proxy;
}

location @proxy {
proxy_pass http://127.0.0.1:8080;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header Server;
proxy_connect_timeout 3s;
proxy_read_timeout 10s;
}
}
7 changes: 7 additions & 0 deletions Configuration/etc/supervisor/conf.d/orchardnestd.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[program:orchardnestd]
command=/home/orchardnest/app/.build/release/orchardnestd serve --env production
directory=/home/orchardnest/app
user=orchardnest
environment=DATABASE_URL='postgres://orchardnest:12345@localhost/orchardnest'
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log
7 changes: 7 additions & 0 deletions Configuration/etc/supervisor/conf.d/orchardnestq.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[program:orchardnestq]
command=/home/orchardnest/app/.build/release/orchardnestd queues --scheduled --env production
directory=/home/orchardnest/app
user=orchardnest
environment=DATABASE_URL='postgres://orchardnest:12345@localhost/orchardnest'
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let package = Package(
name: "OrchardNestServer",
targets: ["OrchardNestServer"]
),
.executable(name: "orcnst", targets: ["orcnst"])
.executable(name: "orchardnestd", targets: ["orchardnestd"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand Down Expand Up @@ -47,7 +47,7 @@ let package = Package(
.product(name: "Plot", package: "Plot"),
.product(name: "Ink", package: "Ink")]
),
.target(name: "orcnst",
.target(name: "orchardnestd",
dependencies: ["OrchardNestKit", "OrchardNestServer", "FeedKit"]),
.testTarget(
name: "OrchardNestKitTests",
Expand Down
2 changes: 2 additions & 0 deletions Resources/Views/about.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# About

Coming Soon...
2 changes: 2 additions & 0 deletions Resources/Views/support.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# Support

Coming Soon...
26 changes: 21 additions & 5 deletions Sources/OrchardNestServer/Configurator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import OrchardNestKit
import Plot
import QueuesFluentDriver
import Vapor
extension Date {
func get(_ type: Calendar.Component) -> Int {
let calendar = Calendar.current
return calendar.component(type, from: self)
}
}

extension HTML: ResponseEncodable {
public func encodeResponse(for request: Request) -> EventLoopFuture<Response> {
Expand Down Expand Up @@ -101,18 +107,30 @@ public final class Configurator: ConfiguratorProtocol {
// }

app.queues.add(RefreshJob())
app.queues.schedule(RefreshJob()).daily().at(.midnight)
app.queues.schedule(RefreshJob()).daily().at(7, 30)
app.queues.schedule(RefreshJob()).daily().at(19, 30)
#if DEBUG
if !app.environment.isRelease {
let minute = Date().get(.minute)
[0, 30].map { ($0 + minute + 5).remainderReportingOverflow(dividingBy: 60).partialValue }.forEach { minute in
app.queues.schedule(RefreshJob()).hourly().at(.init(integerLiteral: minute))
}
}
#endif
try app.queues.startInProcessJobs(on: .default)
app.commands.use(RefreshCommand(help: "Imports data into the database"), as: "refresh")

try app.autoMigrate().wait()
// services.register(wss, as: WebSocketServer.self)

let api = app.grouped("api", "v1")

let markdownDirectory = app.directory.viewsDirectory
let parser = MarkdownParser()

let textPairs = FileManager.default.enumerator(atPath: app.directory.viewsDirectory)?.compactMap { $0 as? String }.map { path in
print(app.directory.viewsDirectory + path)
return URL(fileURLWithPath: app.directory.viewsDirectory + path)
let textPairs = FileManager.default.enumerator(atPath: markdownDirectory)?.compactMap { $0 as? String }.map { path in
URL(fileURLWithPath: app.directory.viewsDirectory + path)
}.compactMap { url in
(try? String(contentsOf: url)).map { (url.deletingPathExtension().lastPathComponent, $0) }
}
Expand All @@ -121,8 +139,6 @@ public final class Configurator: ConfiguratorProtocol {
parser.parse
)

debugPrint(pages)

try app.register(collection: HTMLController(views: pages))
try api.grouped("entires").register(collection: EntryController())

Expand Down
11 changes: 11 additions & 0 deletions Sources/OrchardNestServer/Controllers/Routing/HTMLController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,17 @@ extension Node where Context == HTML.DocumentContext {
.head(
.title("OrchardNest - \(subtitle)"),
.meta(.charset(.utf8)),
.raw("""
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GXSE03BMPF"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-GXSE03BMPF');
</script>
"""),
.link(.rel(.appleTouchIcon), .sizes("180x180"), .href("/apple-touch-icon.png")),
.link(.rel(.appleTouchIcon), .type("image/png"), .sizes("32x32"), .href("/favicon-32x32.png")),
.link(.rel(.appleTouchIcon), .type("image/png"), .sizes("16x16"), .href("/favicon-16x16.png")),
Expand Down Expand Up @@ -197,6 +207,7 @@ extension Node where Context == HTML.ListContext {
.unwrap(item.podcastEpisodeURL) {
.audio(
.controls(true),
.attribute(named: "preload", value: "metadata"),
.source(
.src($0)
)
Expand Down
82 changes: 52 additions & 30 deletions Sources/OrchardNestServer/Models/FeedChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,63 @@ import Vapor
struct EmptyError: Error {}

extension FeedChannel {
static func parseSite(_ site: OrganizedSite, using _: Client, on eventLoop: EventLoop) -> EventLoopFuture<Result<FeedChannel, FeedError>> {
static func parseSite(_ site: OrganizedSite, using client: Client, on eventLoop: EventLoop) -> EventLoopFuture<Result<FeedChannel, FeedError>> {
// let uri = URI(string: site.site.feed_url.absoluteString)
// let headers = HTTPHeaders([("Host", uri.host!), ("User-Agent", "OrchardNest-Robot"), ("Accept", "*/*")])

let promise = eventLoop.makePromise(of: Result<Data, Error>.self)
URLSession.shared.dataTask(with: site.site.feed_url) { data, _, error in
let result: Result<Data, Error>
if let error = error {
result = .failure(error)
} else if let data = data {
result = .success(data)
} else {
promise.fail(EmptyError())
return
// let promise = eventLoop.makePromise(of: Result<Data, Error>.self)
return client.get(URI(string: site.site.feed_url.absoluteString)).map { (response) -> Data? in
response.body.map { buffer in
Data(buffer: buffer)
}
promise.succeed(result)
}.resume()
return promise.futureResult.flatMap { (result) -> EventLoopFuture<Result<FeedChannel, FeedError>> in

let responseBody: Data
do {
responseBody = try result.get()
} catch {
return eventLoop.future(.failure(.download(site.site.feed_url, error)))
}
let channel: FeedChannel
do {
channel = try FeedChannel(language: site.languageCode, category: site.categorySlug, site: site.site, data: responseBody)
} catch {
return eventLoop.future(.failure(.parser(site.site.feed_url, error)))
}
guard channel.items.count > 0 || channel.itemCount == channel.items.count else {
return eventLoop.future(.failure(.items(site.site.feed_url)))
}.flatMapAlways { (result) -> EventLoopFuture<Result<FeedChannel, FeedError>> in
let newResult = result.mapError { FeedError.download(site.site.feed_url, $0) }.flatMap { (data) -> Result<Data, FeedError> in
guard let data = data else {
return .failure(.empty(site.site.feed_url))
}
return .success(data)
}.flatMap { data in
Result {
try FeedChannel(language: site.languageCode, category: site.categorySlug, site: site.site, data: data)
}.mapError { .parser(site.site.feed_url, $0) }
}.flatMap { (channel) -> Result<FeedChannel, FeedError> in
guard channel.items.count > 0 || channel.itemCount == channel.items.count else {
return .failure(.items(site.site.feed_url))
}
return .success(channel)
}
return eventLoop.future(.success(channel))
return eventLoop.future(newResult)
}
// URLSession.shared.dataTask(with: site.site.feed_url) { data, _, error in
// let result: Result<Data, Error>
// if let error = error {
// result = .failure(error)
// } else if let data = data {
// result = .success(data)
// } else {
// promise.fail(EmptyError())
// return
// }
// promise.succeed(result)
// }.resume()
// return promise.futureResult.flatMap { (result) -> EventLoopFuture<Result<FeedChannel, FeedError>> in
//
// let responseBody: Data
// do {
// responseBody = try result.get()
// } catch {
// return eventLoop.future(.failure(.download(site.site.feed_url, error)))
// }
// let channel: FeedChannel
// do {
// channel = try FeedChannel(language: site.languageCode, category: site.categorySlug, site: site.site, data: responseBody)
// } catch {
// return eventLoop.future(.failure(.parser(site.site.feed_url, error)))
// }
// guard channel.items.count > 0 || channel.itemCount == channel.items.count else {
// return eventLoop.future(.failure(.items(site.site.feed_url)))
// }
// return eventLoop.future(.success(channel))
// }
}
}
Loading

0 comments on commit f3d3d05

Please sign in to comment.