mirror of https://gitgud.io/cncanon/sg-proxy.git
Add proper token checking, just have .env directly in here
This commit is contained in:
parent
3c20b3b07f
commit
51d6736d5c
|
@ -1 +0,0 @@
|
|||
API_TOKEN="sgp_replace"
|
|
@ -1,2 +1 @@
|
|||
.env
|
||||
node_modules
|
11
README.md
11
README.md
|
@ -1,12 +1,3 @@
|
|||
# SG-proxy
|
||||
|
||||
Newer info on here: https://rentry.org/sg_proxy
|
||||
|
||||
1) Create a SourceGraph account
|
||||
2) Go to https://sourcegraph.com/, click your profile icon in upper-right, then Settings, then go to Account->Access tokens, and generate a new token
|
||||
3) Create a `.env` and put your token there
|
||||
4) `npm install`
|
||||
5) `node main.js`
|
||||
6) Change completion to Claude in ST, set any API key, then set the proxy URL as `http://localhost:3000/v1` and set any password.
|
||||
|
||||
Enjoy.
|
||||
For instructions see https://rentry.org/sg_proxy
|
55
main.js
55
main.js
|
@ -2,7 +2,6 @@ require("dotenv").config();
|
|||
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const { exit } = require("process");
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
|
||||
|
@ -116,14 +115,50 @@ process.on('unhandledRejection', (reason, promise) => {
|
|||
console.error('Unhandled Promise Rejection:', reason);
|
||||
});
|
||||
|
||||
if (!API_TOKEN) {
|
||||
console.error("SourceGraph API token not found! Create a file named '.env' and put your token there as an API_TOKEN. See .env.example for an example.");
|
||||
exit();
|
||||
}
|
||||
else if (API_TOKEN.indexOf("sgp_") == -1) {
|
||||
console.error("Invalid SourceGraph API token! Make sure you copied the whole token starting with sgp_, like 'sgp-blablabla'.");
|
||||
exit();
|
||||
async function checkToken(token) {
|
||||
const data = {
|
||||
query: 'query { currentUser { username } }'
|
||||
};
|
||||
|
||||
const config = {
|
||||
method: 'post',
|
||||
url: 'https://sourcegraph.com/.api/graphql',
|
||||
headers: {
|
||||
'Authorization': `token ${token}`
|
||||
},
|
||||
data: data
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await axios(config);
|
||||
if(response.data && response.data.data && response.data.data.currentUser) {
|
||||
console.log(`Token works, username: ${response.data.data.currentUser.username}`);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => console.log(`Server listening on port ${port}`));
|
||||
// Two basic checks
|
||||
if (!API_TOKEN) {
|
||||
console.error("SourceGraph API token not found! Create a file named '.env' and put your token there as an API_TOKEN. See .env.example for an example.");
|
||||
process.exit(1);
|
||||
}
|
||||
else if (API_TOKEN.indexOf("sgp_") == -1) {
|
||||
console.error("Invalid SourceGraph API token! Make sure you copied the whole token starting with sgp_, like 'sgp_blablabla'.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Check token validity
|
||||
checkToken(API_TOKEN).then(isValid => {
|
||||
if (!isValid) {
|
||||
console.error("Invalid SourceGraph API token! The token is not valid. Make sure you copied the whole token.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const port = process.env.PORT || 3000;
|
||||
app.listen(port, () => console.log(`Server listening on port ${port}`));
|
||||
});
|
Loading…
Reference in New Issue