duycop/api-go

By duycop

Updated 9 months ago

quick run api from mssl

Image
Databases & Storage
Developer Tools
0

14

Mssql:

Create table ABC:

CREATE TABLE [ABC](
	[id] [int] NOT NULL PRIMARY ,
	[name] [nvarchar](50) NULL
)

Insert data to talbe

USE[test]
GO
INSERT [ABC]([id],[name])VALUES(1,N'â'),(2,N'bb'),(3,N'cc'),(4,N'đ'),(5,N'ê'),(6,N'ff'),(7,N'gg'),(8,N'hh'),(9,N'ii'),(10,N'jj'),(11,N'việtnam'),(12,N'HồChíMinh')
GO

Create SP_

-- Author:	 Do Duy Cop
-- Create date: 2024-06-18
-- Description: SP for GO
-- =============================================
ALTER PROCEDURE [dbo].[SP_DATA]
	@json NVARCHAR(MAX)=NULL
AS
BEGIN
	if(@json is null or @json ='' )
	begin
		select ( select 
					0 as [ok], 
					N'Dữ liệu không hợp lệ' as [msg] 
				 FOR JSON PATH, WITHOUT_ARRAY_WRAPPER)
		as [json];		
	end
	else
	begin		
		DECLARE @action NVARCHAR(50) = JSON_VALUE(@json, '$.action');
		if(@action='cmd1')
		begin
			select (select 
				1 as [ok], 
				N'ok' as [msg],
				@action as [action], 
				( Select * 
				  From [ABC]
				  FOR JSON PATH) as [data] 
				FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) 
			as [json];
		end
		if(@action='search')
		begin
			DECLARE @q NVARCHAR(50) = JSON_VALUE(@json, '$.q');
			select (select 
				1 as [ok], 
				N'ok' as [msg],
				@action as [action], 
				( Select * 
				  From [ABC]
				  where [name] like @q or [id] like @q
				  FOR JSON PATH) as [data] 
				FOR JSON PATH, WITHOUT_ARRAY_WRAPPER) 
			as [json];
		end
	end;	
END

Config.ini . path on winOS: d:\db\config.ini ; path on linux /home/db/config.ini

[MSSQL]
server = "ip server or hostname"
port = 1433
database = "db_name"
user = "sa"
password = "password_here"
sp_name = "SP_DATA"

Run on docker desktop of windows ok

docker run --privileged --restart=unless-stopped \
  -p 1234:80 \
  --name api \
  -e "TZ=Asia/Ho_Chi_Minh" \
  -v "d:\db\config.ini":/db/config.ini \
  -d duycop/api-go

Run on linux docker

docker run --privileged --restart=unless-stopped \
  -p 1234:80 \
  --name api \
  -e "TZ=Asia/Ho_Chi_Minh" \
  -v "/home/mydb/config.ini":/db/config.ini \
  -d duycop/api-go

HTML form demo

<!DOCTYPE html>
<html>
<head>
<meta charSet="utf-8"/>
<title>Test API</title>
<style>
	body{padding:10px;font-family:tahoma}
	#json{width:100%;height:200px;}
	#cmd{width:100px; height:50px;}
</style>
<script>
	function item(id){
		return document.getElementById(id);
	}
	function show(res){
		var s=JSON.stringify(res);
		item('result').innerHTML=s;
	}
	function test(){
		item('result').innerHTML='fetching...';
		var data = item('json').value;
		fetch('http://192.168.31.50:1234/', {
		  method: 'POST',
		  headers: {'Content-Type':'application/x-www-form-urlencoded'},
		  body: data
		}).then(res => res.json())
		  .then(res => {show(res)})
		  .catch(error =>console.log(error))
	}
</script>
</head>
<body>
JSON input:
<textarea id="json">{"action":"cmd1","id":1, "q":"%i%"}</textarea>
<button id="cmd" onclick="test()"> Call API </button>
<div id="result">JSON Output here</div>
</body>
</html>

Docker Pull Command

docker pull duycop/api-go