Jelajahi Sumber

api raw도 Go로 이전

잉여개발기 (SPDV) 2 tahun lalu
induk
melakukan
62835acaba

+ 3 - 3
app.py

@@ -676,9 +676,9 @@ app.route('/bbs/delete/<int:bbs_num>/<int:post_num>/<comment_num>', methods = ['
 app.route('/api/render/<everything:name>', methods = ['POST'])(api_w_render)
 app.route('/api/render', methods = ['POST'])(api_w_render)
 
-app.route('/api/raw_exist/<everything:name>', defaults = { 'exist_check' : 'on' })(api_w_raw)
-app.route('/api/raw_rev/<int(signed = True):rev>/<everything:name>')(api_w_raw)
-app.route('/api/raw/<everything:name>')(api_w_raw)
+app.route('/api/raw_exist/<everything:name>', defaults = { 'exist_check' : 'on', 'db_set' : db_set_str })(api_w_raw)
+app.route('/api/raw_rev/<int(signed = True):rev>/<everything:name>', defaults = { 'db_set' : db_set_str })(api_w_raw)
+app.route('/api/raw/<everything:name>', defaults = { 'db_set' : db_set_str })(api_w_raw)
 
 app.route('/api/bbs/w/<sub_code>')(api_bbs_w_post)
 app.route('/api/bbs/w/comment/<sub_code>')(api_bbs_w_comment)

+ 2 - 1
route/__init__.py

@@ -1,6 +1,5 @@
 from route.api_func_lang import api_func_lang
 from route.api_image_view import api_image_view
-from route.api_w_raw import api_w_raw
 from route.api_recent_change import api_recent_change
 from route.api_recent_discuss import api_recent_discuss
 from route.api_search import api_search
@@ -171,6 +170,8 @@ from route.vote_select import vote_select
 
 from route.go_api_func_sha224 import api_func_sha224
 
+from route.go_api_w_raw import api_w_raw
+
 from route.go_view_random import view_random
 
 from route.go_main_func_easter_egg import main_func_easter_egg

+ 0 - 30
route/api_w_raw.py

@@ -1,30 +0,0 @@
-from .tool.func import *
-
-def api_w_raw(name = 'Test', rev = '', exist_check = ''):
-    with get_db_connect() as conn:
-        curs = conn.cursor()
-
-        if exist_check != '':
-            curs.execute(db_change("select title from data where title = ?"), [name])
-            if curs.fetchall():
-                return flask.jsonify({ 'exist' : '1' })
-            else:
-                return flask.jsonify({})
-        else:
-            if rev != '':
-                curs.execute(db_change("select data from history where title = ? and id = ?"), [name, rev])
-            else:
-                curs.execute(db_change("select data from data where title = ?"), [name])
-
-            data = curs.fetchall()
-            if data:
-                return flask.jsonify({
-                    "title" : name, 
-                    "data" : render_set(
-                        doc_name = name, 
-                        doc_data = data[0][0],
-                        data_type = 'raw'
-                    )
-                })
-            else:
-                return flask.jsonify({})

+ 29 - 0
route/go_api_w_raw.py

@@ -0,0 +1,29 @@
+from .tool.func import *
+
+def api_w_raw(db_set, name = 'Test', rev = '', exist_check = ''):
+    with get_db_connect() as conn:
+        curs = conn.cursor()
+        
+        other_set = {}
+        other_set["name"] = name
+        other_set["rev"] = rev
+        other_set["exist_check"] = exist_check
+        other_set = json.dumps(other_set)
+
+        if acl_check(name, 'render') != 1:
+            if platform.system() == 'Linux':
+                if platform.machine() in ["AMD64", "x86_64"]:
+                    data = subprocess.Popen([os.path.join(".", "route_go", "bin", sys._getframe().f_code.co_name + ".amd64.bin"), db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+                else:
+                    data = subprocess.Popen([os.path.join(".", "route_go", "bin", sys._getframe().f_code.co_name + ".arm64.bin"), db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+            else:
+                if platform.machine() in ["AMD64", "x86_64"]:
+                    data = subprocess.Popen([os.path.join(".", "route_go", "bin", sys._getframe().f_code.co_name + ".amd64.exe"), db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+                else:
+                    data = subprocess.Popen([os.path.join(".", "route_go", "bin", sys._getframe().f_code.co_name + ".arm64.exe"), db_set, other_set], stdout = subprocess.PIPE).communicate()[0]
+
+            data = data.decode('utf8')
+
+            return flask.Response(response = data, status = 200, mimetype = 'application/json')
+        else:
+            return flask.jsonify({})

+ 6 - 1
route_go/api_func_sha224.go

@@ -3,6 +3,7 @@ package main
 import (
 	"crypto/sha256"
 	"encoding/hex"
+	"encoding/json"
 	"fmt"
 	"os"
 )
@@ -17,5 +18,9 @@ func main() {
 	hash_byte := hasher.Sum(nil)
 	hash_str := hex.EncodeToString(hash_byte)
 
-	fmt.Print(hash_str)
+	new_data := map[string]string{}
+	new_data["data"] = hash_str
+
+	json_data, _ := json.Marshal(new_data)
+	fmt.Print(string(json_data))
 }

+ 95 - 0
route_go/api_w_raw.go

@@ -0,0 +1,95 @@
+package main
+
+import (
+	"database/sql"
+	"encoding/json"
+	"fmt"
+	"os"
+
+	"opennamu/tool"
+)
+
+func main() {
+	call_arg := os.Args[1:]
+
+	db_set := map[string]string{}
+	json.Unmarshal([]byte(call_arg[0]), &db_set)
+
+	other_set := map[string]string{}
+	json.Unmarshal([]byte(call_arg[1]), &other_set)
+
+	db := tool.DB_connect(db_set)
+	if db == nil {
+		return
+	}
+	defer db.Close()
+
+	if other_set["exist_check"] != "" {
+		stmt, err := db.Prepare(tool.DB_change(db_set, "select title from data where title = ?"))
+		if err != nil {
+			return
+		}
+		defer stmt.Close()
+
+		new_data := map[string]string{}
+		var title string
+
+		err = stmt.QueryRow(other_set["name"]).Scan(&title)
+		if err != nil {
+			if err == sql.ErrNoRows {
+			} else {
+				return
+			}
+		} else {
+			new_data["exist"] = "1"
+		}
+
+		json_data, _ := json.Marshal(new_data)
+		fmt.Print(string(json_data))
+	} else {
+		new_data := map[string]string{}
+		var data string
+
+		if other_set["rev"] != "" {
+			stmt, err := db.Prepare(tool.DB_change(db_set, "select data from history where title = ? and id = ?"))
+			if err != nil {
+				return
+			}
+			defer stmt.Close()
+
+			err = stmt.QueryRow(other_set["name"], other_set["rev"]).Scan(&data)
+			if err != nil {
+				if err == sql.ErrNoRows {
+				} else {
+					return
+				}
+			} else {
+				new_data["title"] = other_set["name"]
+				new_data["data"] = data
+			}
+
+			json_data, _ := json.Marshal(new_data)
+			fmt.Print(string(json_data))
+		} else {
+			stmt, err := db.Prepare(tool.DB_change(db_set, "select data from data where title = ?"))
+			if err != nil {
+				return
+			}
+			defer stmt.Close()
+
+			err = stmt.QueryRow(other_set["name"]).Scan(&data)
+			if err != nil {
+				if err == sql.ErrNoRows {
+				} else {
+					return
+				}
+			} else {
+				new_data["title"] = other_set["name"]
+				new_data["data"] = data
+			}
+
+			json_data, _ := json.Marshal(new_data)
+			fmt.Print(string(json_data))
+		}
+	}
+}

TEMPAT SAMPAH
route_go/bin/api_func_sha224.amd64.bin


TEMPAT SAMPAH
route_go/bin/api_func_sha224.amd64.exe


TEMPAT SAMPAH
route_go/bin/api_func_sha224.arm64.bin


TEMPAT SAMPAH
route_go/bin/api_func_sha224.arm64.exe


TEMPAT SAMPAH
route_go/bin/api_w_raw.amd64.bin


TEMPAT SAMPAH
route_go/bin/api_w_raw.amd64.exe


TEMPAT SAMPAH
route_go/bin/api_w_raw.arm64.bin


TEMPAT SAMPAH
route_go/bin/api_w_raw.arm64.exe


TEMPAT SAMPAH
route_go/bin/view_random.amd64.bin


TEMPAT SAMPAH
route_go/bin/view_random.amd64.exe


TEMPAT SAMPAH
route_go/bin/view_random.arm64.bin


TEMPAT SAMPAH
route_go/bin/view_random.arm64.exe


+ 6 - 2
route_go/view_random.go

@@ -1,6 +1,7 @@
 package main
 
 import (
+	"database/sql"
 	"encoding/json"
 	"fmt"
 	"os"
@@ -23,8 +24,11 @@ func main() {
 	var title string
 	err := db.QueryRow(tool.DB_change(db_set, "select title from data where title not like 'user:%' and title not like 'category:%' and title not like 'file:%' order by random() limit 1")).Scan(&title)
 	if err != nil {
-		fmt.Println(err)
-		return
+		if err == sql.ErrNoRows {
+			title = ""
+		} else {
+			return
+		}
 	}
 
 	fmt.Print(title)