Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
configuable port and db name
Browse files Browse the repository at this point in the history
  • Loading branch information
Wond Wood committed Mar 5, 2018
1 parent 6eb11e4 commit c00d427
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 105 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

都说 Telegram 官方永远不重视 CJK 用户.
难道广大的 CJK 朋友会坐以待毙?  
哦, 我的上帝, 看在帕罗尔.杜若这么帅的份上, 当然是选择原……
哦, 我的上帝, 看在帕维尔.杜若这么帅的份上, 当然是选择原……
**不可能!**

## 条件
Expand All @@ -19,7 +19,7 @@

## 特(que) 色(dian)

- 采用数据库暴搜, 无分词/同义词. 技术含量低.
- 胶水工程, 数据库暴搜, 无分词/同义词/模糊. 技术含量低.
- 原生态网页界面.
- 数据源需手动备份, 和真实数据相比有延时.
- 取得所有匹配消息后才加载结果.
Expand Down Expand Up @@ -85,13 +85,14 @@ python3 export.py -d "tgsearch.db" -e "../tg/bin/telegram-cli"
部署, 就是啧么自信.

下载解压
[tgsearch_linux_amd64.zip](https://github.com/cxumol/tg-search/releases/download/alpha/tgsearch_linux_amd64.zip)
[tgsearch_linux_amd64.zip](https://github.com/cxumol/tg-search/releases/download/0.1a/tgsearch_linux_amd64.zip)

找个文件夹,
把软件 `tgsearch` 放进去,
把网页模版 `main.tmpl` 放进去,
把文件配置 `config.json` 放进去,
把数据库文件 `tgsearch.db` 放进去,
然后运行,
然后运行,

```bash
chmod +x ./tg-search
Expand All @@ -105,14 +106,15 @@ chmod +x ./tg-search
见证奇迹的时刻到了.

> 不急
因为我把数据库文件名写死了🌚.
端口也写死了🌚.
<s> 因为我把数据库文件名写死了🌚. </s>
<s> 端口也写死了🌚. </s>
> (已经可以自定义数据库文件名和端口号了, 在 "config.json" 里面改.)
且只编译了 linux_amd64 下的二进制包🌚.
如不适请自编

#### 编译

> 虽不必要, 但万一谁想<s>不开</s>编译呢
> 虽无必要, 但万一谁想<s>不开</s>编译呢
```bash
# 1. 装go, 抄自 https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/01.1.md
Expand Down Expand Up @@ -156,14 +158,14 @@ go build --tags "libsqlite3 linux" -o tgsearch
- 灵活配置端口, 数据库文件名
- 前端装修
- 静态 (网页模版等) 打包到二进制
- 进门加密码
- 访问密码
- 加钱上 `elasticsearch`, 妙妙妙!
- 查询加速: 尝试每找到一条消息, 立马在网页上加载一条
- 查询加速: 尝试每找到一条消息, 立马在网页上加载一条 (channel 通信 + 轮询)
- 整合 travis 云编译
- 求路过的高手指点, 求前端网页装修方案

> 以上是伪.后续计划
真.后续计划: 坐等有缘人接手
> 以上是伪.后续计划
真.后续计划: **坐等有缘人接手**

---

Expand Down
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dbname":"tgsearch.db",
"port":"8082"
}
206 changes: 114 additions & 92 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,114 @@
package main

import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
"regexp"

_ "github.com/mattn/go-sqlite3"
)

var (
dbName = "tgsearch.db"
port = "8082"
)

func main() {
http.HandleFunc("/tgs", func(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("q")
Results := search_sqlite(w, key)

tgfinding := map[string]interface{}{
"Keyword": key,
"Results": Results,
}
tmpl, err := template.ParseFiles("main.tmpl")
if err != nil {
fmt.Fprintf(w, "Parse: %v", err)
log.Printf("Parse: %v", err)
return
}

err = tmpl.Execute(w, tgfinding)
if err != nil {
fmt.Fprintf(w, "Excute: %v", err)
log.Printf("Excute: %v", err)
return
}
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("main.tmpl")
if err != nil {
fmt.Fprintf(w, "Parse: %v", err)
log.Printf("Parse: %v", err)
return
}

err = tmpl.Execute(w, map[string]interface{}{
"Keyword": "",
})
if err != nil {
fmt.Fprintf(w, "Excute: %v", err)
log.Printf("Excute: %v", err)
return
}
})

log.Print("It works! port:" + port)
//log.Fatal(http.ListenAndServe(":443", nil))
log.Fatal(http.ListenAndServe(":"+port, nil))
}

func search_sqlite(w http.ResponseWriter, keyword string) []string {
db, err := sql.Open("sqlite3", "./"+dbName)
if err != nil {
fmt.Fprintf(w, "db open: %v", err)
return []string{"error"}
}

if m, _ := regexp.MatchString("^[a-zA-Z一-龟 ]+$", keyword); !m {
return []string{"不可以! 请勿包含中英文外的字符..."}
}

searchQuery := `SELECT text FROM messages WHERE text LIKE "%` + keyword + `%"`
rows, err := db.Query(searchQuery)
if err != nil {
fmt.Fprintf(w, "db matching: %v", err)
return []string{"error"}
}

result := make([]string, 0, 42)
for rows.Next() {
var arow string
err = rows.Scan(&arow)
result = append(result, arow)
}
return result

}
package main

import (
"database/sql"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"regexp"

_ "github.com/mattn/go-sqlite3"
)

// var (
// dbName = "tgsearch.db"
// port = "8082"
// )

type Config struct {
DbName string
Port string
}

func main() {

var conf Config = Config{"tgsearch.db", "4896"}
readConfig("config.json", &conf)

http.HandleFunc("/tgs", func(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("q")
Results := search_sqlite(w, key, conf)

tgfinding := map[string]interface{}{
"Keyword": key,
"Results": Results,
}
tmpl, err := template.ParseFiles("main.tmpl")
if err != nil {
fmt.Fprintf(w, "Parse: %v", err)
log.Printf("Parse: %v", err)
return
}

err = tmpl.Execute(w, tgfinding)
if err != nil {
fmt.Fprintf(w, "Excute: %v", err)
log.Printf("Excute: %v", err)
return
}
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("main.tmpl")
if err != nil {
fmt.Fprintf(w, "Parse: %v", err)
log.Printf("Parse: %v", err)
return
}

err = tmpl.Execute(w, map[string]interface{}{
"Keyword": "",
})
if err != nil {
fmt.Fprintf(w, "Excute: %v", err)
log.Printf("Excute: %v", err)
return
}
})

log.Print("It works! port:" + conf.Port)
//log.Fatal(http.ListenAndServe(":443", nil))
log.Fatal(http.ListenAndServe(":"+conf.Port, nil))
}

func search_sqlite(w http.ResponseWriter, keyword string, conf Config) []string {
db, err := sql.Open("sqlite3", "./"+conf.DbName)
if err != nil {
fmt.Fprintf(w, "db open: %v", err)
return []string{"error"}
}

if m, _ := regexp.MatchString("^[a-zA-Z一-龟 ]+$", keyword); !m {
return []string{"不可以! 请勿包含中英文外的字符..."}
}

searchQuery := `SELECT text FROM messages WHERE text LIKE "%` + keyword + `%"`
rows, err := db.Query(searchQuery)
if err != nil {
fmt.Fprintf(w, "db matching: %v", err)
return []string{"error"}
}

result := make([]string, 0, 42)
for rows.Next() {
var arow string
err = rows.Scan(&arow)
result = append(result, arow)
}
return result

}

func readConfig(jsonfile string, conf *Config) {
raw, err := ioutil.ReadFile(jsonfile)
if err != nil {
log.Printf("无法读取 %s 配置\n", jsonfile)
log.Println(err.Error())
panic(err)
}
json.Unmarshal(raw, conf)
// log.Println(conf)
}
4 changes: 2 additions & 2 deletions main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<body>
<form action="/tgs" method="get">
<input type="text" name="q">
<input type="text" name="q" size="100%">
<input type="submit" value="🌚">
</form>
{{if ne .Keyword ""}}
Expand All @@ -22,7 +22,7 @@

<footer><center>
<p>
version: 又不是不能用
version: 咕咕咕等有缘人接盘
</p>
<p><a href="https://github.com/cxumol/tg-search/edit/master/README.md" >
告诉我怎么搞出搜索来
Expand Down

0 comments on commit c00d427

Please sign in to comment.