summary refs log tree commit diff homepage
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2023-03-04 20:49:10 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2023-03-04 20:49:10 +0900
commit8a4a6b2d4a655cf4d23f5b94062b0eee005bb6f4 (patch)
tree372b3a5d28bf1f78518afbbe379f97871217656b /src
parent006bc8235e57f04980e2f641fb2ddd91e0d57499 (diff)
downloadhybring-8a4a6b2d4a655cf4d23f5b94062b0eee005bb6f4.tar.gz
Rewrite static pages on DB update
Diffstat (limited to 'src')
-rw-r--r--src/http.cr31
-rw-r--r--src/sqlite.cr42
2 files changed, 48 insertions, 25 deletions
diff --git a/src/http.cr b/src/http.cr
index 1179f73..1d0be13 100644
--- a/src/http.cr
+++ b/src/http.cr
@@ -35,8 +35,9 @@ end
 
 class Server
   def initialize(cfg)
-    @opennic_host = URI.parse(cfg.opennic_remote).host
     @db = Database.new cfg.db, cfg.opennic_remote, cfg.icann_remote
+
+    @opennic_host = URI.parse(cfg.opennic_remote).host
     @opennic_page = Page.new cfg.opennic_local, cfg.opennic_remote,
                              cfg.api, @db
     @opennic_page.write
@@ -116,7 +117,35 @@ class Server
     end
   end
 
+  getter db
+  getter opennic_page
+  getter icann_page
+
   def listen(port)
+    @db.update_hook ->(arg : Void*, action : Database::UpdateAction,
+                       db : LibC::Char*, table : LibC::Char*, rowid : Int64) {
+      return unless db == "main"
+      obj = arg.as Server
+      case table
+      when "member"
+        case action
+        in .delete?
+          obj.db.members.delete rowid
+        in .insert?, .update?
+          obj.db.update_member rowid
+        end
+      when "applicant"
+        case action
+        in .delete?
+          obj.db.applicants.delete rowid
+        in .insert?, .update?
+          obj.db.update_applicant rowid
+        end
+      end
+      obj.opennic_page.write
+      obj.icann_page.write
+    }, self.as Void*
+
     puts "Listening on http://#{@server.bind_tcp port}"
     @server.listen
   end
diff --git a/src/sqlite.cr b/src/sqlite.cr
index 2975031..0032694 100644
--- a/src/sqlite.cr
+++ b/src/sqlite.cr
@@ -75,6 +75,8 @@ class Database
   INSERT_APPLICANT = "INSERT INTO applicant (nick, opennic, icann)
     VALUES (%Q, %Q, %Q)"
 
+  alias UpdateAction = SQLite::UpdateAction
+
   class Statement
     def initialize(db, query)
       Database.check SQLite.prepare db, query, LibC.strlen(query),
@@ -160,30 +162,10 @@ class Database
       self.finalize
       raise ex
     end
-    SQLite.update_hook @ref, ->(arg, action, db, table, rowid) {
-      return unless db == "main"
-      obj = arg.as Database
-      case table
-      when "member"
-        case action
-        in .delete?
-          obj.members.delete rowid
-        in .insert?, .update?
-          obj.exec SELECT_MEMBER_ROW, rowid do |row|
-            obj.members[rowid] = {row[0].text, row[1].text, row[2].text}
-          end
-        end
-      when "applicant"
-        case action
-        in .delete?
-          obj.applicants.delete rowid
-        in .insert?, .update?
-          obj.exec SELECT_APPLICANT_ROW, rowid do |row|
-            obj.applicants[rowid] = {row[0].text, row[1].text, row[2].text}
-          end
-        end
-      end
-    }, self.as Void*
+  end
+
+  def update_hook(callback, arg)
+    SQLite.update_hook @ref, callback, arg
   end
 
   def exec(query : String, *values)
@@ -212,6 +194,18 @@ class Database
   getter members
   getter applicants
 
+  def update_member(rowid)
+    self.exec SELECT_MEMBER_ROW, rowid do |row|
+      self.members[rowid] = {row[0].text, row[1].text, row[2].text}
+    end
+  end
+
+  def update_applicant(rowid)
+    self.exec SELECT_APPLICANT_ROW, rowid do |row|
+      self.applicants[rowid] = {row[0].text, row[1].text, row[2].text}
+    end
+  end
+
   def add_applicant(nick, opennic, icann)
     self.exec INSERT_APPLICANT, nick, opennic, icann do end
   end