Skip to content

Commit

Permalink
Edit application config
Browse files Browse the repository at this point in the history
  • Loading branch information
yasszu committed Apr 30, 2018
1 parent 9c2a456 commit 4de5718
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- ./mysql/data:/var/lib/mysql
- ./mysql/conf:/etc/mysql/conf.d
environment:
MYSQL_DATABASE: web_crawler
MYSQL_DATABASE: crawler
MYSQL_ROOT_PASSWORD: root

web:
Expand Down
4 changes: 3 additions & 1 deletion mysql/conf/my.cnf
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-character-set-client-handshake

explicit_defaults_for_timestamp = 1
4 changes: 3 additions & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mysql {
host=db
port=3306
user=root
password=root
db=finagle_web_crawler
db=crawler
}
7 changes: 2 additions & 5 deletions src/main/scala/app/Server.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package app

import java.net.InetSocketAddress

import akka.actor.ActorSystem
import akka.util.Timeout
import app.actor.GoogleBlogActor
import app.api.GoogleBlogApi
import app.service.MysqlClient
import com.twitter.app.Flag
import com.twitter.finagle.Http
import com.twitter.server.TwitterServer
import com.twitter.util.{Await, Future}
Expand All @@ -23,10 +20,11 @@ object Server extends TwitterServer with MysqlClient {
override def failfastOnFlagsNotParsed: Boolean = true

val conf: Config = ConfigFactory.load()
val host: Flag[InetSocketAddress] = flag("db.host", new InetSocketAddress("db", 3306), "Mysql server address")
val user: String = conf.getString("mysql.user")
val password: String = conf.getString("mysql.password")
val db: String = conf.getString("mysql.db")
val host: String = conf.getString("mysql.host")
val port: Int = conf.getInt("mysql.port")

implicit val timeout: Timeout = Timeout(180 seconds)

Expand Down Expand Up @@ -57,7 +55,6 @@ object Server extends TwitterServer with MysqlClient {
def main() {
println("Listening for HTTP on /0.0.0.0:8080")
for {
_ <- createSchema()
_ <- createArticlesTables()
_ <- createCategoriesTables()
_ <- startActors
Expand Down
17 changes: 5 additions & 12 deletions src/main/scala/app/service/MysqlClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package app.service
import java.net.InetSocketAddress

import app.util.DDL
import com.twitter.app.Flag
import com.twitter.finagle.Mysql
import com.twitter.finagle.mysql._
import com.twitter.util.Future
Expand All @@ -13,24 +12,18 @@ import com.twitter.util.Future
*/
trait MysqlClient {

val host: Flag[InetSocketAddress]
val host: String
val port: Int
val user: String
val password: String
val db: String

lazy val addr: InetSocketAddress = new InetSocketAddress(host, port)

implicit lazy val mysqlClient: Client with Transactions = Mysql.client
.withCredentials(user, password)
.withDatabase(db)
.newRichClient("%s:%d".format(host().getHostName, host().getPort))

def createSchema(): Future[Result] = {
Mysql.client
.withCredentials(user, password)
.newRichClient("%s:%d".format(host().getHostName, host().getPort))
.query(DDL.createSchema).onSuccess { _ =>
println("[INFO] Create schema finagle_web_crawler")
}
}
.newRichClient("%s:%d".format(addr.getHostName, addr.getPort))

def createArticlesTables()(implicit client: Client): Future[Result] = {
client.query(DDL.createArticlesTable).onSuccess { _ =>
Expand Down
3 changes: 0 additions & 3 deletions src/main/scala/app/util/DDL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package app.util

object DDL {

val createSchema =
"""CREATE SCHEMA IF NOT EXISTS `finagle_web_crawler` DEFAULT CHARACTER SET utf8mb4"""

val createArticlesTable =
"""CREATE TABLE IF NOT EXISTS `articles` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
Expand Down

0 comments on commit 4de5718

Please sign in to comment.