From 734ce643ecbc992834a8f78b44904b82b09bc84b Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 30 May 2020 03:26:34 +0700 Subject: all: rename the module to "kamusku" --- .gitignore | 14 +- Makefile | 12 +- README.adoc | 2 +- _www-kamusku/assets/github.svg | 1 + _www-kamusku/assets/linux.svg | 7 + _www-kamusku/assets/macos.svg | 7 + _www-kamusku/assets/windows.svg | 7 + _www-kamusku/index.html | 311 ++++++++++++++++++++++++++++++++++++++++ _www-kamusku/index.js | 126 ++++++++++++++++ _www-kamusku/kbbiclient.js | 35 +++++ _www-kbbi/assets/github.svg | 1 - _www-kbbi/assets/linux.svg | 7 - _www-kbbi/assets/macos.svg | 7 - _www-kbbi/assets/windows.svg | 7 - _www-kbbi/index.html | 311 ---------------------------------------- _www-kbbi/index.js | 126 ---------------- _www-kbbi/kbbiclient.js | 35 ----- active_client.go | 2 +- api_client.go | 2 +- api_client_test.go | 2 +- client.go | 2 +- cmd/bot-kamusku/app.yaml | 9 ++ cmd/bot-kamusku/main.go | 43 ++++++ cmd/bot-kbbi/app.yaml | 9 -- cmd/bot-kbbi/main.go | 43 ------ cmd/kbbi/main.go | 9 +- cmd/www-kamusku/app.yaml | 8 ++ cmd/www-kamusku/main.go | 36 +++++ cmd/www-kbbi/app.yaml | 8 -- cmd/www-kbbi/main.go | 36 ----- daftar_kata.go | 2 +- definisi_kata.go | 2 +- definisi_response.go | 2 +- direct_client.go | 5 +- direct_client_test.go | 2 +- generate.go | 2 +- go.mod | 2 +- internal/cmd/mergedic/main.go | 2 +- kamus_cache.go | 2 +- kamus_cache_test.go | 2 +- kamusku.go | 52 +++++++ kamusku_test.go | 53 +++++++ kata.go | 2 +- kata_test.go | 2 +- kbbi.go | 56 -------- kbbi_test.go | 53 ------- server.go | 51 +++++-- telegram_bot.go | 16 ++- 48 files changed, 791 insertions(+), 742 deletions(-) create mode 100644 _www-kamusku/assets/github.svg create mode 100644 _www-kamusku/assets/linux.svg create mode 100644 _www-kamusku/assets/macos.svg create mode 100644 _www-kamusku/assets/windows.svg create mode 100644 _www-kamusku/index.html create mode 100644 _www-kamusku/index.js create mode 100644 _www-kamusku/kbbiclient.js delete mode 100644 _www-kbbi/assets/github.svg delete mode 100644 _www-kbbi/assets/linux.svg delete mode 100644 _www-kbbi/assets/macos.svg delete mode 100644 _www-kbbi/assets/windows.svg delete mode 100644 _www-kbbi/index.html delete mode 100644 _www-kbbi/index.js delete mode 100644 _www-kbbi/kbbiclient.js create mode 100644 cmd/bot-kamusku/app.yaml create mode 100644 cmd/bot-kamusku/main.go delete mode 100644 cmd/bot-kbbi/app.yaml delete mode 100644 cmd/bot-kbbi/main.go create mode 100644 cmd/www-kamusku/app.yaml create mode 100644 cmd/www-kamusku/main.go delete mode 100644 cmd/www-kbbi/app.yaml delete mode 100644 cmd/www-kbbi/main.go create mode 100644 kamusku.go create mode 100644 kamusku_test.go delete mode 100644 kbbi.go delete mode 100644 kbbi_test.go diff --git a/.gitignore b/.gitignore index e1376d0..a143412 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,13 @@ -/_www-kbbi/bin/* -/bot-kbbi -/bot-kbbi-linux-amd64 -/cmd/www-kbbi/static.go +/_www-kamusku/bin/* +/bot-kamusku +/bot-kamusku-linux-amd64 +/cmd/www-kamusku/static.go /internal/cmd/mergedic/id_ID.dic /internal/cmd/mergedic/id_ID.dic.new /kamus.gob /kamus.gob.new -/kbbi +/kamusku /testdata/kamus.gob /testdata/kamus.gob.new -/www-kbbi -/www-kbbi-linux-amd64 +/www-kamusku +/www-kamusku-linux-amd64 diff --git a/Makefile b/Makefile index e1a8775..6c9507c 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,8 @@ lint: --disable=gomnd \ --disable=wsl \ --disable=gocognit \ + --disable=goerr113 \ + --disable=testpackage \ ./... test: @@ -45,18 +47,18 @@ release: deploy: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ - go build -o www-kbbi-linux-amd64 ./cmd/www-kbbi/ - rsync --progress ./www-kbbi-linux-amd64 www-kbbi:~/bin/www-kbbi - rsync --progress --recursive ./_www-kbbi/ www-kbbi:~/bin/_www-kbbi/ + go build -o www-kamusku-linux-amd64 ./cmd/www-kamusku/ + rsync --progress ./www-kamusku-linux-amd64 www-kamusku:~/bin/www-kamusku + rsync --progress --recursive ./_www-kamusku/ www-kamusku:~/bin/_www-kamusku/ deploy-bot: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \ go build -o bot-kbbi-linux-amd64 ./cmd/bot-kbbi/ - rsync --progress ./bot-kbbi-linux-amd64 www-kbbi:~/bin/bot-kbbi + rsync --progress ./bot-kbbi-linux-amd64 www-kamusku:~/bin/bot-kbbi ## ## Development task ## dev-server: - DEBUG=1 go run ./cmd/www-kbbi + DEBUG=1 go run ./cmd/www-kamusku diff --git a/README.adoc b/README.adoc index 80b4375..d3127b6 100644 --- a/README.adoc +++ b/README.adoc @@ -1,4 +1,4 @@ -= kbbi += kamusku Proyek sumber terbuka implementasi antar-muka perintah dan API untuk Kamus Besar Bahasa Indonesia. diff --git a/_www-kamusku/assets/github.svg b/_www-kamusku/assets/github.svg new file mode 100644 index 0000000..3899712 --- /dev/null +++ b/_www-kamusku/assets/github.svg @@ -0,0 +1 @@ +GitHub icon \ No newline at end of file diff --git a/_www-kamusku/assets/linux.svg b/_www-kamusku/assets/linux.svg new file mode 100644 index 0000000..4c2bbed --- /dev/null +++ b/_www-kamusku/assets/linux.svg @@ -0,0 +1,7 @@ + + +IcoFont Icons +brand-linux + + + \ No newline at end of file diff --git a/_www-kamusku/assets/macos.svg b/_www-kamusku/assets/macos.svg new file mode 100644 index 0000000..701ef59 --- /dev/null +++ b/_www-kamusku/assets/macos.svg @@ -0,0 +1,7 @@ + + +IcoFont Icons +brand-mac-os + + + \ No newline at end of file diff --git a/_www-kamusku/assets/windows.svg b/_www-kamusku/assets/windows.svg new file mode 100644 index 0000000..8995329 --- /dev/null +++ b/_www-kamusku/assets/windows.svg @@ -0,0 +1,7 @@ + + +IcoFont Icons +brand-windows + + + \ No newline at end of file diff --git a/_www-kamusku/index.html b/_www-kamusku/index.html new file mode 100644 index 0000000..a9a5110 --- /dev/null +++ b/_www-kamusku/index.html @@ -0,0 +1,311 @@ + + + + Project KBBI + + + + + + + + + + + + + + +
+
+ Proyek KBBI +
+ +
+

+ Proyek sumber terbuka implementasi antar-muka perintah dan API + untuk Kamus Besar Bahasa Indonesia. +

+

+ Sumber kode untuk proyek ini dapat diambil di + + tautan berikut + +

+ +

Program kbbi

+

+ Program kbbi yaitu antar-muka untuk mencari definisi dari kata + lewat baris perintah. +

+

+ Program ini sangat sederhana, cara menggunakannya cukup dengan + memberikan kata yang dicari setelah nama program, misalnya, +

+
+$ kbbi kamus,bahasa
+
+

+ maka akan mencetak definisi dari kata "kamus" dan "bahasa" ke + layar, +

+
+=== bahasa
+  Definisi #1: sistem lambang bunyi yang arbitrer, yang digunakan oleh
+  anggota suatu masyarakat untuk bekerja sama, berinteraksi, dan
+  mengidentifikasikan diri
+    Kelas #1: Nomina: kata benda
+    Kelas #2: Linguistik: -
+
+  Definisi #2: percakapan (perkataan) yang baik; tingkah laku yang baik; sopan santun
+    Kelas #1: Nomina: kata benda
+    Contoh #1: baik budi --nya
+
+  ...
+
+=== kamus
+  Definisi #1: karya rujukan atau acuan dalam bentuk cetak maupun digital yang memuat kata dan ungkapan, dapat disusun menurut abjad atau tema, berisi keterangan tentang makna, pemakaian, atau terjemahan
+    Kelas #1: Nomina: kata benda
+
+  Definisi #2: buku yang memuat kumpulan istilah atau nama yang disusun menurut abjad beserta penjelasan tentang makna dan pemakaiannya
+    Kelas #1: Nomina: kata benda
+
+  ...
+
+ +

+ Unduh program KBBI untuk sistem operasi Anda, +

+ +
+ + + Linux 64bit + + + + macOS 64bit + + + + Windows 64bit + +
+ +

Bot Telegram

+ +

+ Dengan tersedianya API, membuka banyak implementasi terbuka lain, + salah satunya yaitu Bot untuk aplikasi Telegram: + + https://t.me/KamuskuBot + +

+

+ Untuk saat ini, KamuskuBot hanya punya satu perintah yaitu + "/definisi". Cara menggunakan perintah ini hampir sama dengan + program kbbi yaitu dengan memberikan kata yang dicari, contohnya, +

+
+/definisi kamus,bahasa
+
+ +

KBBI API

+ +

+ KBBI API adalah jantung dari semua implementasi di atas dan + pencarian definisi kata di bawah. KBBI API dapat diakses + menggunakan HTTP lewat: https://kilabit.info/project/kbbi/api. +

+ +

API Definisi

+

HTTP API untuk mencari definisi dari satu atau lebih kata.

+

+ Format permintaan, +

+
+GET /definisi?kata=<string>,...
+
+ +

+ Format respons dalam JSON, +

+
+{
+	"<string>": {
+		"dasar": "<string>",
+		"pesan": "<string>",
+		"definisi": [{
+			"isi": "<string>",
+			"kelas": [<string>],
+			"contoh": [<string>]
+		},
+		...
+		]
+	}
+}
+		
+ +

+ Jika kata tidak ditemukan atau bila kata bukan kata baku, bagian + "pesan" akan berisi keterangan yang menjelaskan galat dari + pencarian. +

+

+ Berikut contoh pemanggilan API untuk mencari definisi dari kata + "kamus", "bahaza" (kata tidak ditemukan), dan "analisa" (kata + tidak baku): + + /api/definisi?kata=kamus,bahaza,analisa + +

+ +

Definisi kata

+ +
+
+ +
+
+ +
+
+ +

+ Catatan: Pisahkan kata dengan koma untuk mencari lebih dari satu + kata. +

+ +
+ + diff --git a/_www-kamusku/index.js b/_www-kamusku/index.js new file mode 100644 index 0000000..f5f0217 --- /dev/null +++ b/_www-kamusku/index.js @@ -0,0 +1,126 @@ +/** + * Copyright 2020, Shulhan . All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + +let kbbiClient = new KBBIClient("") + +function cariDefinisi() { + let kata = document.getElementById("kata").value + kbbiClient.getDefinitions(kata, cariDefinisiCallback) +} + +function cariDefinisiCallback(res) { + let out = document.getElementById("definisi-result") + out.innerHTML = "" + + for (let kata in res) { + let root = document.createElement("div") + printResultKata(root, kata, res[kata]) + out.appendChild(root) + } +} + +function printResultKata(out, kata, defKata) { + let el = document.createElement("h3") + el.appendChild(document.createTextNode(kata)) + out.appendChild(el) + + if (typeof defKata.pesan !== "undefined" && defKata.pesan !== "") { + printPesan(out, defKata.pesan) + return + } + + printKataDasar(out, defKata.dasar) + printDefinitions(out, defKata.definisi) +} + +function printPesan(out, pesan) { + let root = document.createElement("div") + root.appendChild(document.createTextNode(pesan)) + out.appendChild(root) +} + +function printKataDasar(out, kataDasar) { + // Seriously, JavaScript? + if ( + typeof kataDasar === "undefined" || + kataDasar === null || + kataDasar === "" + ) { + return + } + + let root = document.createElement("div") + root.appendChild(document.createTextNode("Kata dasar: ")) + + let italic = document.createElement("i") + italic.appendChild(document.createTextNode(kataDasar)) + root.appendChild(italic) + + out.appendChild(root) +} + +function printDefinitions(out, definitions) { + if (typeof definitions === "undefined" || definitions === null) { + return + } + + for (let x = 0; x < definitions.length; x++) { + let def = definitions[x] + + let root = document.createElement("div") + let el = document.createElement("p") + el.classList.add("definisi") + el.appendChild( + document.createTextNode( + "Definisi #" + (x + 1) + ": " + def.isi + ".", + ), + ) + root.appendChild(el) + + printKelasKata(root, def.kelas) + printContoh(root, def.contoh) + out.appendChild(root) + } +} + +function printKelasKata(out, daftarKelas) { + if (typeof daftarKelas === "undefined") { + return + } + + let root = document.createElement("div") + root.classList.add("kelas-kata") + root.appendChild(document.createTextNode("Kelas,")) + + let el = document.createElement("ul") + for (let x = 0; x < daftarKelas.length; x++) { + let li = document.createElement("li") + li.appendChild(document.createTextNode(daftarKelas[x])) + el.appendChild(li) + } + root.appendChild(el) + out.appendChild(root) +} + +function printContoh(out, examples) { + if (typeof examples === "undefined") { + return + } + + let root = document.createElement("div") + root.classList.add("contoh") + root.appendChild(document.createTextNode("Contoh,")) + + let ul = document.createElement("ul") + + for (let x = 0; x < examples.length; x++) { + let li = document.createElement("li") + li.appendChild(document.createTextNode(examples[x])) + ul.appendChild(li) + } + root.appendChild(ul) + out.appendChild(root) +} diff --git a/_www-kamusku/kbbiclient.js b/_www-kamusku/kbbiclient.js new file mode 100644 index 0000000..ab35633 --- /dev/null +++ b/_www-kamusku/kbbiclient.js @@ -0,0 +1,35 @@ +/** + * Copyright 2020, Shulhan . All rights reserved. + * Use of this source code is governed by a BSD-style + * license that can be found in the LICENSE file. + */ + +class KBBIClient { + constructor(baseURL) { + if (baseURL.length === 0) { + baseURL = "https://kilabit.info/project/kbbi" + } + this.baseURL = baseURL + } + + getDefinitions(words, cb) { + if (words.length === 0) { + return + } + + let params = "kata=" + words + let xhr = new XMLHttpRequest() + + xhr.addEventListener("load", function() { + cb(JSON.parse(xhr.responseText)) + }) + + xhr.open("GET", this.baseURL + "/api/definisi?" + params) + xhr.setRequestHeader( + "Content-Type", + "application/x-www-form-urlencoded", + ) + + xhr.send(null) + } +} diff --git a/_www-kbbi/assets/github.svg b/_www-kbbi/assets/github.svg deleted file mode 100644 index 3899712..0000000 --- a/_www-kbbi/assets/github.svg +++ /dev/null @@ -1 +0,0 @@ -GitHub icon \ No newline at end of file diff --git a/_www-kbbi/assets/linux.svg b/_www-kbbi/assets/linux.svg deleted file mode 100644 index 4c2bbed..0000000 --- a/_www-kbbi/assets/linux.svg +++ /dev/null @@ -1,7 +0,0 @@ - - -IcoFont Icons -brand-linux - - - \ No newline at end of file diff --git a/_www-kbbi/assets/macos.svg b/_www-kbbi/assets/macos.svg deleted file mode 100644 index 701ef59..0000000 --- a/_www-kbbi/assets/macos.svg +++ /dev/null @@ -1,7 +0,0 @@ - - -IcoFont Icons -brand-mac-os - - - \ No newline at end of file diff --git a/_www-kbbi/assets/windows.svg b/_www-kbbi/assets/windows.svg deleted file mode 100644 index 8995329..0000000 --- a/_www-kbbi/assets/windows.svg +++ /dev/null @@ -1,7 +0,0 @@ - - -IcoFont Icons -brand-windows - - - \ No newline at end of file diff --git a/_www-kbbi/index.html b/_www-kbbi/index.html deleted file mode 100644 index a9a5110..0000000 --- a/_www-kbbi/index.html +++ /dev/null @@ -1,311 +0,0 @@ - - - - Project KBBI - - - - - - - - - - - - - - -
-
- Proyek KBBI -
- -
-

- Proyek sumber terbuka implementasi antar-muka perintah dan API - untuk Kamus Besar Bahasa Indonesia. -

-

- Sumber kode untuk proyek ini dapat diambil di - - tautan berikut - -

- -

Program kbbi

-

- Program kbbi yaitu antar-muka untuk mencari definisi dari kata - lewat baris perintah. -

-

- Program ini sangat sederhana, cara menggunakannya cukup dengan - memberikan kata yang dicari setelah nama program, misalnya, -

-
-$ kbbi kamus,bahasa
-
-

- maka akan mencetak definisi dari kata "kamus" dan "bahasa" ke - layar, -

-
-=== bahasa
-  Definisi #1: sistem lambang bunyi yang arbitrer, yang digunakan oleh
-  anggota suatu masyarakat untuk bekerja sama, berinteraksi, dan
-  mengidentifikasikan diri
-    Kelas #1: Nomina: kata benda
-    Kelas #2: Linguistik: -
-
-  Definisi #2: percakapan (perkataan) yang baik; tingkah laku yang baik; sopan santun
-    Kelas #1: Nomina: kata benda
-    Contoh #1: baik budi --nya
-
-  ...
-
-=== kamus
-  Definisi #1: karya rujukan atau acuan dalam bentuk cetak maupun digital yang memuat kata dan ungkapan, dapat disusun menurut abjad atau tema, berisi keterangan tentang makna, pemakaian, atau terjemahan
-    Kelas #1: Nomina: kata benda
-
-  Definisi #2: buku yang memuat kumpulan istilah atau nama yang disusun menurut abjad beserta penjelasan tentang makna dan pemakaiannya
-    Kelas #1: Nomina: kata benda
-
-  ...
-
- -

- Unduh program KBBI untuk sistem operasi Anda, -

- - - -

Bot Telegram

- -

- Dengan tersedianya API, membuka banyak implementasi terbuka lain, - salah satunya yaitu Bot untuk aplikasi Telegram: - - https://t.me/KamuskuBot - -

-

- Untuk saat ini, KamuskuBot hanya punya satu perintah yaitu - "/definisi". Cara menggunakan perintah ini hampir sama dengan - program kbbi yaitu dengan memberikan kata yang dicari, contohnya, -

-
-/definisi kamus,bahasa
-
- -

KBBI API

- -

- KBBI API adalah jantung dari semua implementasi di atas dan - pencarian definisi kata di bawah. KBBI API dapat diakses - menggunakan HTTP lewat: https://kilabit.info/project/kbbi/api. -

- -

API Definisi

-

HTTP API untuk mencari definisi dari satu atau lebih kata.

-

- Format permintaan, -

-
-GET /definisi?kata=<string>,...
-
- -

- Format respons dalam JSON, -

-
-{
-	"<string>": {
-		"dasar": "<string>",
-		"pesan": "<string>",
-		"definisi": [{
-			"isi": "<string>",
-			"kelas": [<string>],
-			"contoh": [<string>]
-		},
-		...
-		]
-	}
-}
-		
- -

- Jika kata tidak ditemukan atau bila kata bukan kata baku, bagian - "pesan" akan berisi keterangan yang menjelaskan galat dari - pencarian. -

-

- Berikut contoh pemanggilan API untuk mencari definisi dari kata - "kamus", "bahaza" (kata tidak ditemukan), dan "analisa" (kata - tidak baku): - - /api/definisi?kata=kamus,bahaza,analisa - -

- -

Definisi kata

- -
-
- -
-
- -
-
- -

- Catatan: Pisahkan kata dengan koma untuk mencari lebih dari satu - kata. -

- -
- - diff --git a/_www-kbbi/index.js b/_www-kbbi/index.js deleted file mode 100644 index f5f0217..0000000 --- a/_www-kbbi/index.js +++ /dev/null @@ -1,126 +0,0 @@ -/** - * Copyright 2020, Shulhan . All rights reserved. - * Use of this source code is governed by a BSD-style - * license that can be found in the LICENSE file. - */ - -let kbbiClient = new KBBIClient("") - -function cariDefinisi() { - let kata = document.getElementById("kata").value - kbbiClient.getDefinitions(kata, cariDefinisiCallback) -} - -function cariDefinisiCallback(res) { - let out = document.getElementById("definisi-result") - out.innerHTML = "" - - for (let kata in res) { - let root = document.createElement("div") - printResultKata(root, kata, res[kata]) - out.appendChild(root) - } -} - -function printResultKata(out, kata, defKata) { - let el = document.createElement("h3") - el.appendChild(document.createTextNode(kata)) - out.appendChild(el) - - if (typeof defKata.pesan !== "undefined" && defKata.pesan !== "") { - printPesan(out, defKata.pesan) - return - } - - printKataDasar(out, defKata.dasar) - printDefinitions(out, defKata.definisi) -} - -function printPesan(out, pesan) { - let root = document.createElement("div") - root.appendChild(document.createTextNode(pesan)) - out.appendChild(root) -} - -function printKataDasar(out, kataDasar) { - // Seriously, JavaScript? - if ( - typeof kataDasar === "undefined" || - kataDasar === null || - kataDasar === "" - ) { - return - } - - let root = document.createElement("div") - root.appendChild(document.createTextNode("Kata dasar: ")) - - let italic = document.createElement("i") - italic.appendChild(document.createTextNode(kataDasar)) - root.appendChild(italic) - - out.appendChild(root) -} - -function printDefinitions(out, definitions) { - if (typeof definitions === "undefined" || definitions === null) { - return - } - - for (let x = 0; x < definitions.length; x++) { - let def = definitions[x] - - let root = document.createElement("div") - let el = document.createElement("p") - el.classList.add("definisi") - el.appendChild( - document.createTextNode( - "Definisi #" + (x + 1) + ": " + def.isi + ".", - ), - ) - root.appendChild(el) - - printKelasKata(root, def.kelas) - printContoh(root, def.contoh) - out.appendChild(root) - } -} - -function printKelasKata(out, daftarKelas) { - if (typeof daftarKelas === "undefined") { - return - } - - let root = document.createElement("div") - root.classList.add("kelas-kata") - root.appendChild(document.createTextNode("Kelas,")) - - let el = document.createElement("ul") - for (let x = 0; x < daftarKelas.length; x++) { - let li = document.createElement("li") - li.appendChild(document.createTextNode(daftarKelas[x])) - el.appendChild(li) - } - root.appendChild(el) - out.appendChild(root) -} - -function printContoh(out, examples) { - if (typeof examples === "undefined") { - return - } - - let root = document.createElement("div") - root.classList.add("contoh") - root.appendChild(document.createTextNode("Contoh,")) - - let ul = document.createElement("ul") - - for (let x = 0; x < examples.length; x++) { - let li = document.createElement("li") - li.appendChild(document.createTextNode(examples[x])) - ul.appendChild(li) - } - root.appendChild(ul) - out.appendChild(root) -} diff --git a/_www-kbbi/kbbiclient.js b/_www-kbbi/kbbiclient.js deleted file mode 100644 index ab35633..0000000 --- a/_www-kbbi/kbbiclient.js +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2020, Shulhan . All rights reserved. - * Use of this source code is governed by a BSD-style - * license that can be found in the LICENSE file. - */ - -class KBBIClient { - constructor(baseURL) { - if (baseURL.length === 0) { - baseURL = "https://kilabit.info/project/kbbi" - } - this.baseURL = baseURL - } - - getDefinitions(words, cb) { - if (words.length === 0) { - return - } - - let params = "kata=" + words - let xhr = new XMLHttpRequest() - - xhr.addEventListener("load", function() { - cb(JSON.parse(xhr.responseText)) - }) - - xhr.open("GET", this.baseURL + "/api/definisi?" + params) - xhr.setRequestHeader( - "Content-Type", - "application/x-www-form-urlencoded", - ) - - xhr.send(null) - } -} diff --git a/active_client.go b/active_client.go index 0aebd0b..fed49a6 100644 --- a/active_client.go +++ b/active_client.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku // // activeClient define an interface for an active client. diff --git a/api_client.go b/api_client.go index eb1c2f7..d85f419 100644 --- a/api_client.go +++ b/api_client.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "fmt" diff --git a/api_client_test.go b/api_client_test.go index f2e9a87..a7b408a 100644 --- a/api_client_test.go +++ b/api_client_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "testing" diff --git a/client.go b/client.go index bf8c599..0e07c5e 100644 --- a/client.go +++ b/client.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "fmt" diff --git a/cmd/bot-kamusku/app.yaml b/cmd/bot-kamusku/app.yaml new file mode 100644 index 0000000..74fd1f5 --- /dev/null +++ b/cmd/bot-kamusku/app.yaml @@ -0,0 +1,9 @@ +service: bot +runtime: go113 +instance_class: F2 +automatic_scaling: + max_instances: 1 +env_variables: + DEBUG: "2" + TELEGRAM_TOKEN: "1121465148:AAH9vI-DkHUOPGTmy1Js0dxKSHLYIIkXaIE" + TELEGRAM_WEBHOOK_URL: "https://bot-dot-kamuskubot.df.r.appspot.com" diff --git a/cmd/bot-kamusku/main.go b/cmd/bot-kamusku/main.go new file mode 100644 index 0000000..c2f60e2 --- /dev/null +++ b/cmd/bot-kamusku/main.go @@ -0,0 +1,43 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +// Program bot-kamusku adalah Telegram Bot yang melayani pencarian definisi +// kata menggunakan perintah pesan di Telegram. +// +package main + +import ( + "log" + "os" + "os/signal" + + "github.com/shuLhan/kamusku" +) + +func main() { + log.SetFlags(0) + + // Use the token and Webhook URL from environment variables. + tgbot, err := kamusku.NewTelegramBot("", "") + if err != nil { + log.Fatal(err) + } + + go func() { + err := tgbot.Start() + if err != nil { + log.Println(err) + } + }() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + + err = tgbot.Stop() + if err != nil { + log.Println(err) + } +} diff --git a/cmd/bot-kbbi/app.yaml b/cmd/bot-kbbi/app.yaml deleted file mode 100644 index 89bf31a..0000000 --- a/cmd/bot-kbbi/app.yaml +++ /dev/null @@ -1,9 +0,0 @@ -service: bot-kbbi -runtime: go113 -instance_class: F2 -automatic_scaling: - max_instances: 1 -env_variables: - DEBUG: "2" - TELEGRAM_TOKEN: "1121465148:AAH9vI-DkHUOPGTmy1Js0dxKSHLYIIkXaIE" - TELEGRAM_WEBHOOK_URL: "https://bot-kbbi-dot-kamuskubot.df.r.appspot.com" diff --git a/cmd/bot-kbbi/main.go b/cmd/bot-kbbi/main.go deleted file mode 100644 index 91c2552..0000000 --- a/cmd/bot-kbbi/main.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2020, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// -// Program bot-kbbi adalah Telegram Bot yang melayani pencarian definisi kata -// menggunakan perintah pesan di Telegram. -// -package main - -import ( - "log" - "os" - "os/signal" - - "github.com/shuLhan/kbbi" -) - -func main() { - log.SetFlags(0) - - // Use the token and Webhook URL from environment variables. - tgbot, err := kbbi.NewTelegramBot("", "") - if err != nil { - log.Fatal(err) - } - - go func() { - err := tgbot.Start() - if err != nil { - log.Println(err) - } - }() - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - - err = tgbot.Stop() - if err != nil { - log.Println(err) - } -} diff --git a/cmd/kbbi/main.go b/cmd/kbbi/main.go index 09b3a92..984596c 100644 --- a/cmd/kbbi/main.go +++ b/cmd/kbbi/main.go @@ -14,7 +14,7 @@ import ( "log" "sort" - "github.com/shuLhan/kbbi" + "github.com/shuLhan/kamusku" ) func main() { @@ -25,6 +25,7 @@ func main() { ) log.SetFlags(0) + log.SetPrefix("kbbi") flag.StringVar(&surel, "surel", "", "Nama pengguna") flag.StringVar(&sandi, "sandi", "", "Sandi pengguna") @@ -33,7 +34,7 @@ func main() { flag.Parse() - cl, err := kbbi.NewClient() + cl, err := kamusku.NewClient() if err != nil { log.Fatal(err) } @@ -41,7 +42,7 @@ func main() { if len(surel) > 0 && len(sandi) > 0 { err = cl.Login(surel, sandi) if err != nil { - log.Fatal("kbbi: ", err) + log.Fatal(err) } } @@ -87,7 +88,7 @@ func main() { } } -func listKataDasar(cl *kbbi.Client) { +func listKataDasar(cl *kamusku.Client) { kataDasar, err := cl.ListKataDasar() if err != nil { log.Println(err) diff --git a/cmd/www-kamusku/app.yaml b/cmd/www-kamusku/app.yaml new file mode 100644 index 0000000..580f7ea --- /dev/null +++ b/cmd/www-kamusku/app.yaml @@ -0,0 +1,8 @@ +service: default +runtime: go113 +instance_class: F2 +automatic_scaling: + max_instances: 1 +env_variables: + KBBI_SUREL: "m.shulhan@gmail.com" + KBBI_SANDI: "tyuiopKEMDIKBUD5810" diff --git a/cmd/www-kamusku/main.go b/cmd/www-kamusku/main.go new file mode 100644 index 0000000..f559594 --- /dev/null +++ b/cmd/www-kamusku/main.go @@ -0,0 +1,36 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "log" + "os" + "os/signal" + + "github.com/shuLhan/kamusku" +) + +func main() { + server, err := kamusku.NewServer("") + if err != nil { + log.Fatal(err) + } + + go func() { + err = server.Start() + if err != nil { + log.Println(err) + } + }() + + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + + err = server.Shutdown() + if err != nil { + log.Println(err) + } +} diff --git a/cmd/www-kbbi/app.yaml b/cmd/www-kbbi/app.yaml deleted file mode 100644 index 580f7ea..0000000 --- a/cmd/www-kbbi/app.yaml +++ /dev/null @@ -1,8 +0,0 @@ -service: default -runtime: go113 -instance_class: F2 -automatic_scaling: - max_instances: 1 -env_variables: - KBBI_SUREL: "m.shulhan@gmail.com" - KBBI_SANDI: "tyuiopKEMDIKBUD5810" diff --git a/cmd/www-kbbi/main.go b/cmd/www-kbbi/main.go deleted file mode 100644 index 370ed16..0000000 --- a/cmd/www-kbbi/main.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2020, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import ( - "log" - "os" - "os/signal" - - "github.com/shuLhan/kbbi" -) - -func main() { - server, err := kbbi.NewServer("") - if err != nil { - log.Fatal(err) - } - - go func() { - err = server.Start() - if err != nil { - log.Println(err) - } - }() - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - - err = server.Shutdown() - if err != nil { - log.Println(err) - } -} diff --git a/daftar_kata.go b/daftar_kata.go index 1560355..6232dcd 100644 --- a/daftar_kata.go +++ b/daftar_kata.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku type DaftarKata map[string]struct{} diff --git a/definisi_kata.go b/definisi_kata.go index 8d812f8..b050c6b 100644 --- a/definisi_kata.go +++ b/definisi_kata.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "fmt" diff --git a/definisi_response.go b/definisi_response.go index 91808eb..602d0e4 100644 --- a/definisi_response.go +++ b/definisi_response.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import "encoding/json" diff --git a/direct_client.go b/direct_client.go index 5817b51..61824b1 100644 --- a/direct_client.go +++ b/direct_client.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "bytes" @@ -20,6 +20,7 @@ import ( "strings" "github.com/shuLhan/share/lib/debug" + libhttp "github.com/shuLhan/share/lib/http" "github.com/shuLhan/share/lib/net/html" "golang.org/x/net/publicsuffix" ) @@ -212,7 +213,7 @@ func (cl *directClient) login(surel, sandi string) (err error) { return fmt.Errorf("Login: %w", err) } - req.Header.Set(headerNameContentType, headerValueContentType) + req.Header.Set(libhttp.HeaderContentType, libhttp.ContentTypeForm) res, err := cl.httpc.Do(req) if err != nil { diff --git a/direct_client_test.go b/direct_client_test.go index dd3b59b..533fc78 100644 --- a/direct_client_test.go +++ b/direct_client_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "io/ioutil" diff --git a/generate.go b/generate.go index 05137b0..5176311 100644 --- a/generate.go +++ b/generate.go @@ -4,4 +4,4 @@ //go:generate go run ./internal/generate -package kbbi +package kamusku diff --git a/go.mod b/go.mod index 251ffd9..5b0e918 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/shuLhan/kbbi +module github.com/shuLhan/kamusku go 1.13 diff --git a/internal/cmd/mergedic/main.go b/internal/cmd/mergedic/main.go index 33e644c..7cdba38 100644 --- a/internal/cmd/mergedic/main.go +++ b/internal/cmd/mergedic/main.go @@ -3,7 +3,7 @@ // license that can be found in the LICENSE file. // -// Program mergedic merge daftar kata from kbbi with hunspell-id dictionary. +// Program mergedic merge daftar kata from KBBI with hunspell-id dictionary. // package main diff --git a/kamus_cache.go b/kamus_cache.go index add6ccb..52eb1f4 100644 --- a/kamus_cache.go +++ b/kamus_cache.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "bytes" diff --git a/kamus_cache_test.go b/kamus_cache_test.go index 44ccb92..09a51cf 100644 --- a/kamus_cache_test.go +++ b/kamus_cache_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "testing" diff --git a/kamusku.go b/kamusku.go new file mode 100644 index 0000000..151d85d --- /dev/null +++ b/kamusku.go @@ -0,0 +1,52 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +// Package kamusku is Go client for Kamus Besar Bahasa Indonesia at +// kbbi.kemdikbud.go.id. +// +package kamusku + +import "time" + +const ( + hostname = "kbbi.kemdikbud.go.id" + baseURL = "https://" + hostname + loginURL = baseURL + "/Account/Login" + entriPath = "/entri/" + + defServerAPI = "https://kamuskubot.df.r.appspot.com" + envPort = "PORT" + pathAPIDefinisi = "/api/definisi" + + attrNameClass = "class" + attrNameHref = "href" + attrNameTitle = "title" + attrNameValue = "value" + + attrValueRootWord = "rootword" + + tagNameAnchor = "a" + tagNameFont = "font" + tagNameHeader2 = "h2" + tagNameInput = "input" + tagNameItalic = "i" + tagNameOrderedList = "ol" + tagNameSpan = "span" + tagNameUnorderedList = "ul" + + paramNameIngatSaya = "IngatSaya" + paramNameKata = "kata" + paramNameKataSandi = "KataSandi" + paramNameMasukan = "masukan" + paramNameMasukanLengkap = "masukanLengkap" + paramNamePage = "page" + paramNamePosel = "Posel" + paramNameRequestVerificationToken = "__RequestVerificationToken" //nolint: gosec + + paramValueDasar = "dasar" + paramValueFalse = "false" + + defTimeout = 20 * time.Second +) diff --git a/kamusku_test.go b/kamusku_test.go new file mode 100644 index 0000000..9967c1c --- /dev/null +++ b/kamusku_test.go @@ -0,0 +1,53 @@ +// Copyright 2020, Shulhan . All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package kamusku + +import ( + "log" + "os" + "testing" +) + +const ( + testServerAPI = "http://127.0.0.1" + defListen + testKamusStorage = "testdata/kamus.gob" +) + +//nolint: gochecknoglobals +var ( + testServer *Server + + testKataMengeja = &Kata{ + Dasar: "eja", + Definisi: []*DefinisiKata{{ + Isi: "melafalkan (menyebutkan) huruf-huruf satu demi satu", + Kelas: []string{"Verba: kata kerja"}, + Contoh: []string{ + `kita ~ kata “dapat” dengan “d-a-p-a-t”`, + }, + }}, + } +) + +func TestMain(m *testing.M) { + var err error + + // Run the local server to test the apiClient. + testServer, err = NewServer(testKamusStorage) + if err != nil { + log.Fatal(err) + } + + testServer.kamus.set("mengeja", testKataMengeja) + + go func() { + err := testServer.Start() + if err != nil { + log.Fatal(err) + } + }() + + os.Exit(m.Run()) +} diff --git a/kata.go b/kata.go index dc19bcc..8cf6466 100644 --- a/kata.go +++ b/kata.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "bytes" diff --git a/kata_test.go b/kata_test.go index fa64c96..d471575 100644 --- a/kata_test.go +++ b/kata_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "io/ioutil" diff --git a/kbbi.go b/kbbi.go deleted file mode 100644 index dfc08c8..0000000 --- a/kbbi.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2020, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// -// Package kbbi is Go client for Kamus Besar Bahasa Indonesia at -// kbbi.kemdikbud.go.id. -// -package kbbi - -import "time" - -const ( - hostname = "kbbi.kemdikbud.go.id" - baseURL = "https://" + hostname - loginURL = baseURL + "/Account/Login" - entriPath = "/entri/" - - defServerAPI = "https://kamuskubot.df.r.appspot.com" - envPort = "PORT" - pathAPIDefinisi = "/api/definisi" - - attrNameClass = "class" - attrNameHref = "href" - attrNameTitle = "title" - attrNameValue = "value" - - attrValueRootWord = "rootword" - - headerNameACAO = "Access-Control-Allow-Origin" - headerNameContentType = "Content-Type" - headerValueContentType = "application/x-www-form-urlencoded" - - tagNameAnchor = "a" - tagNameFont = "font" - tagNameHeader2 = "h2" - tagNameInput = "input" - tagNameItalic = "i" - tagNameOrderedList = "ol" - tagNameSpan = "span" - tagNameUnorderedList = "ul" - - paramNameIngatSaya = "IngatSaya" - paramNameKata = "kata" - paramNameKataSandi = "KataSandi" - paramNameMasukan = "masukan" - paramNameMasukanLengkap = "masukanLengkap" - paramNamePage = "page" - paramNamePosel = "Posel" - paramNameRequestVerificationToken = "__RequestVerificationToken" //nolint: gosec - - paramValueDasar = "dasar" - paramValueFalse = "false" - - defTimeout = 20 * time.Second -) diff --git a/kbbi_test.go b/kbbi_test.go deleted file mode 100644 index a9250aa..0000000 --- a/kbbi_test.go +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2020, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package kbbi - -import ( - "log" - "os" - "testing" -) - -const ( - testServerAPI = "http://127.0.0.1" + defListen - testKamusStorage = "testdata/kamus.gob" -) - -//nolint: gochecknoglobals -var ( - testServer *Server - - testKataMengeja = &Kata{ - Dasar: "eja", - Definisi: []*DefinisiKata{{ - Isi: "melafalkan (menyebutkan) huruf-huruf satu demi satu", - Kelas: []string{"Verba: kata kerja"}, - Contoh: []string{ - `kita ~ kata “dapat” dengan “d-a-p-a-t”`, - }, - }}, - } -) - -func TestMain(m *testing.M) { - var err error - - // Run the local server to test the apiClient. - testServer, err = NewServer(testKamusStorage) - if err != nil { - log.Fatal(err) - } - - testServer.kamus.set("mengeja", testKataMengeja) - - go func() { - err := testServer.Start() - if err != nil { - log.Fatal(err) - } - }() - - os.Exit(m.Run()) -} diff --git a/server.go b/server.go index ef7f930..3e2ebd0 100644 --- a/server.go +++ b/server.go @@ -2,18 +2,20 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "context" "fmt" "log" + "math/rand" stdhttp "net/http" "os" "strings" "sync" "time" + "github.com/shuLhan/share/lib/ascii" "github.com/shuLhan/share/lib/debug" "github.com/shuLhan/share/lib/http" ) @@ -21,7 +23,6 @@ import ( const ( envKbbiSandi = "KBBI_SANDI" envKbbiSurel = "KBBI_SUREL" - defRootDir = "_www-kbbi" defListen = ":3394" emptyResponse = "{}" ) @@ -36,12 +37,12 @@ type Server struct { // The client that forward request to official KBBI server. forwardc *directClient + stopped chan bool + wg sync.WaitGroup + // If offline is true and the word definition is not found on cache, // the server will not forward request to official KBBI server. offline bool - - wg sync.WaitGroup - stopped chan bool } // @@ -157,13 +158,22 @@ func (server *Server) dumpCache() { } } -func (server *Server) handleDefinisi( +// +// handleAdmin is endpoint to manage dictionary cache on the web. +// +func (server *Server) handleAdmin( httpRes stdhttp.ResponseWriter, httpReq *stdhttp.Request, reqBody []byte, ) (resBody []byte, err error) { - httpRes.Header().Set(headerNameACAO, "*") + return resBody, nil +} +func (server *Server) handleDefinisi( + httpRes stdhttp.ResponseWriter, + httpReq *stdhttp.Request, + reqBody []byte, +) (resBody []byte, err error) { paramKata := httpReq.Form.Get(paramNameKata) if len(paramKata) == 0 { return []byte(emptyResponse), nil @@ -229,5 +239,30 @@ func (server *Server) registerEndpoints() (err error) { ResponseType: http.ResponseTypeJSON, Call: server.handleDefinisi, } - return server.http.RegisterEndpoint(epDefinisi) + + err = server.http.RegisterEndpoint(epDefinisi) + if err != nil { + return fmt.Errorf("registerEndpoints %q: %w", pathAPIDefinisi, + err) + } + + rand.Seed(time.Now().Unix()) + pathAdmin := string(ascii.Random([]byte(ascii.LettersNumber), 16)) + + epAdmin := &http.Endpoint{ + Method: http.RequestMethodGet, + Path: pathAdmin, + RequestType: http.RequestTypeQuery, + ResponseType: http.ResponseTypeHTML, + Call: server.handleAdmin, + } + + err = server.http.RegisterEndpoint(epAdmin) + if err != nil { + return fmt.Errorf("registerEndpoints %q: %w", pathAdmin, err) + } + + fmt.Println("administration path:", pathAdmin) + + return nil } diff --git a/telegram_bot.go b/telegram_bot.go index 71adb94..061bb4c 100644 --- a/telegram_bot.go +++ b/telegram_bot.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package kbbi +package kamusku import ( "bytes" @@ -150,6 +150,20 @@ func (tgbot *TelegramBot) sendError(msg *bot.Message, parseMode, errMsg string) } } +//nolint: unused +func (tgbot *TelegramBot) sendShutdownNotice(msg *bot.Message) { + text := `Mohon maaf, KamuskuBot untuk sementara berhenti beroperasi dulu karena masalah legalitas. + +Kami sedang memperbarui kanal kamus ke id.wiktionary.org, yang lebih terbuka +dan bebas dipakai oleh publik. +` + _, err := tgbot.SendMessage(msg, "", text) + if err != nil { + log.Printf("sendShutdownNotice: %s", err) + return + } +} + func formatText(definisiKata DefinisiResponse) string { buf := &bytes.Buffer{} for k, kata := range definisiKata { -- cgit v1.3