Skip to content

Commit

Permalink
Make Application abstract.
Browse files Browse the repository at this point in the history
  • Loading branch information
MykytaPimonovTD committed Oct 15, 2024
1 parent 6d577f3 commit e659c85
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,37 @@
package io.spine.examples.pingh.server

import io.ktor.client.engine.cio.CIO
import io.spine.environment.DefaultMode
import io.spine.examples.pingh.clock.Clock
import io.spine.examples.pingh.github.ClientId
import io.spine.examples.pingh.github.ClientSecret
import io.spine.environment.Environment
import io.spine.examples.pingh.github.GitHubApp
import io.spine.examples.pingh.github.of
import io.spine.examples.pingh.mentions.RemoteGitHubSearch
import io.spine.examples.pingh.mentions.newMentionsContext
import io.spine.examples.pingh.server.datastore.DatastoreStorageFactory
import io.spine.examples.pingh.sessions.RemoteGitHubAuthentication
import io.spine.examples.pingh.sessions.RemoteGitHubUsers
import io.spine.examples.pingh.sessions.newSessionsContext
import io.spine.server.Server
import io.spine.server.ServerEnvironment
import io.spine.server.delivery.Delivery
import io.spine.server.transport.memory.InMemoryTransportFactory

/**
* The server side of the Pingh application.
*
* During the initialization, performs the actions as follows.
*
* 1. Configures the server environment for production use,
* including the interaction with GitHub API and Google Datastore.
*
* 2. Starts an [HTTP endpoint][startHeartbeatServer] receiving the current time values
* from an external clock or a system scheduler.
*/
internal class Application {
private companion object {
@Suppress("LeakingThis" /* Abstract method implementations are required to create server */)
internal abstract class Application {

internal companion object {
/**
* The port on which the Pingh server runs.
*/
private const val pinghPort = 50051

/**
* Secrets of the Pingh GitHub App required for the authentication flow.
* Returns a [CloudApplication] if the app is running on Google Cloud Platform;
* otherwise, returns [LocalApplication].
*/
private val gitHubApp = GitHubApp::class.of(
ClientId::class.of(Secret.named("github_client_id")),
ClientSecret::class.of(Secret.named("github_client_secret"))
)
internal fun newInstance(): Application =
if (Environment.instance().`is`(CloudMode::class.java)) {
CloudApplication()
} else {
LocalApplication()
}
}

/**
Expand All @@ -78,23 +67,25 @@ internal class Application {

init {
configureEnvironment()
startClock()
server = createServer()
startHeartbeatServer(Clock())
}

/**
* Returns the GitHub App secrets required to make authentication requests
* on behalf of the App.
*/
protected abstract fun gitHubApp(): GitHubApp

/**
* Configures the server environment.
*
* Application data is stored using Google Cloud Datastore. Therefore, any changes made
* by users of this application will be persisted in-between the application launches.
*/
private fun configureEnvironment() {
ServerEnvironment
.`when`(DefaultMode::class.java)
.use(DatastoreStorageFactory.remote())
.use(Delivery.localAsync())
.use(InMemoryTransportFactory.newInstance())
}
protected abstract fun configureEnvironment()

/**
* Starts emitting periodic events with the current time to the server.
*/
protected abstract fun startClock()

/**
* Creates a new Spine `Server` instance at the [pinghPort].
Expand All @@ -108,7 +99,7 @@ internal class Application {
.atPort(pinghPort)
.add(
newSessionsContext(
RemoteGitHubAuthentication(gitHubApp, httpEngine),
RemoteGitHubAuthentication(gitHubApp(), httpEngine),
RemoteGitHubUsers(httpEngine)
)
)
Expand Down
132 changes: 0 additions & 132 deletions server/src/main/kotlin/io/spine/examples/pingh/server/LocalServer.kt

This file was deleted.

11 changes: 6 additions & 5 deletions server/src/main/kotlin/io/spine/examples/pingh/server/Server.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@

package io.spine.examples.pingh.server

import io.spine.environment.Environment

/**
* The entry point of the server application.
*
* Starts a Pingh server that handles RPC requests. Additionally, starts
* a [server][startHeartbeatServer] to handle HTTP requests that receive
* the current time from an external clock or system scheduler.
*/
public fun main() {
val app = Application()
Environment.instance()
.register(LocalMode::class.java)
.register(CloudMode::class.java)
val app = Application.newInstance()
app.server.start()
app.server.awaitTermination()
}

0 comments on commit e659c85

Please sign in to comment.