added unsecured sql api for server
This commit is contained in:
		
							parent
							
								
									9bf78374b7
								
							
						
					
					
						commit
						549073dd95
					
				
							
								
								
									
										1
									
								
								client/.tool-versions
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								client/.tool-versions
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					nodejs 23.1.0
 | 
				
			||||||
							
								
								
									
										5812
									
								
								client/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										5812
									
								
								client/package-lock.json
									
									
									
										generated
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
					@ -31,7 +31,7 @@
 | 
				
			||||||
    "@types/node": "^20",
 | 
					    "@types/node": "^20",
 | 
				
			||||||
    "@types/react": "^18",
 | 
					    "@types/react": "^18",
 | 
				
			||||||
    "@types/react-dom": "^18",
 | 
					    "@types/react-dom": "^18",
 | 
				
			||||||
    "eslint": "^8",
 | 
					    "eslint": "^9.14.0",
 | 
				
			||||||
    "eslint-config-next": "15.0.3",
 | 
					    "eslint-config-next": "15.0.3",
 | 
				
			||||||
    "typescript": "^5"
 | 
					    "typescript": "^5"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,8 +31,8 @@
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "build": {
 | 
					  "build": {
 | 
				
			||||||
    "beforeBuildCommand": "bun run build",
 | 
					    "beforeBuildCommand": "npm run build",
 | 
				
			||||||
    "beforeDevCommand": "bun run dev",
 | 
					    "beforeDevCommand": "npm run dev",
 | 
				
			||||||
    "devUrl": "http://localhost:3000",
 | 
					    "devUrl": "http://localhost:3000",
 | 
				
			||||||
    "frontendDist": "../out"
 | 
					    "frontendDist": "../out"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,14 @@
 | 
				
			||||||
 | 
					import { Box, Button, IconButton, useTheme } from "@mui/material";
 | 
				
			||||||
import { useState } from "react";
 | 
					import { useState } from "react";
 | 
				
			||||||
 | 
					import { defaultSettings } from "../../lib/settings";
 | 
				
			||||||
import { FloatingDialog } from "../Generic/FloatingDialog";
 | 
					import { FloatingDialog } from "../Generic/FloatingDialog";
 | 
				
			||||||
import { Button } from "@mui/material";
 | 
					import { BugReportOutlined } from "@mui/icons-material";
 | 
				
			||||||
 | 
					import { pgClient } from "../../lib/postgresql";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const Testing = () => {
 | 
					export const Testing = () => {
 | 
				
			||||||
 | 
					  // contexts
 | 
				
			||||||
 | 
					  const theme = useTheme();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // states
 | 
					  // states
 | 
				
			||||||
  const [openState, setOpenState] = useState(false);
 | 
					  const [openState, setOpenState] = useState(false);
 | 
				
			||||||
  const [maximisedState, setMaximisedState] = useState(false);
 | 
					  const [maximisedState, setMaximisedState] = useState(false);
 | 
				
			||||||
| 
						 | 
					@ -12,10 +18,35 @@ export const Testing = () => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <FloatingDialog
 | 
					    <FloatingDialog
 | 
				
			||||||
      body={<div>Test</div>}
 | 
					      body={
 | 
				
			||||||
 | 
					        <Box
 | 
				
			||||||
 | 
					          sx={{
 | 
				
			||||||
 | 
					            border: "1px solid " + theme.palette.grey[700],
 | 
				
			||||||
 | 
					            borderRadius: defaultSettings.style.radius + "px",
 | 
				
			||||||
 | 
					            display: "flex",
 | 
				
			||||||
 | 
					            flexDirection: "column",
 | 
				
			||||||
 | 
					            flexGrow: 1,
 | 
				
			||||||
 | 
					            my: 2,
 | 
				
			||||||
 | 
					            overflow: "hidden",
 | 
				
			||||||
 | 
					            p: 0,
 | 
				
			||||||
 | 
					          }}
 | 
				
			||||||
 | 
					        >
 | 
				
			||||||
 | 
					          <Button
 | 
				
			||||||
 | 
					            onClick={() => {
 | 
				
			||||||
 | 
					              const asdf = pgClient;
 | 
				
			||||||
 | 
					            }}
 | 
				
			||||||
 | 
					          >
 | 
				
			||||||
 | 
					            connect to pg
 | 
				
			||||||
 | 
					          </Button>
 | 
				
			||||||
 | 
					        </Box>
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      close={close}
 | 
					      close={close}
 | 
				
			||||||
      maximisedState={maximisedState}
 | 
					      maximisedState={maximisedState}
 | 
				
			||||||
      openButton={<Button onClick={() => setOpenState(true)}>open</Button>}
 | 
					      openButton={
 | 
				
			||||||
 | 
					        <IconButton onClick={() => setOpenState(true)} size="small">
 | 
				
			||||||
 | 
					          <BugReportOutlined />
 | 
				
			||||||
 | 
					        </IconButton>
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      openState={openState}
 | 
					      openState={openState}
 | 
				
			||||||
      setMaximisedState={setMaximisedState}
 | 
					      setMaximisedState={setMaximisedState}
 | 
				
			||||||
      title="Testing"
 | 
					      title="Testing"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										13
									
								
								server/go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								server/go.mod
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,13 @@
 | 
				
			||||||
 | 
					module cspj-server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					go 1.23.2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require (
 | 
				
			||||||
 | 
						github.com/jackc/pgpassfile v1.0.0 // indirect
 | 
				
			||||||
 | 
						github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
 | 
				
			||||||
 | 
						github.com/jackc/pgx/v5 v5.7.1 // indirect
 | 
				
			||||||
 | 
						github.com/jackc/puddle/v2 v2.2.2 // indirect
 | 
				
			||||||
 | 
						golang.org/x/crypto v0.27.0 // indirect
 | 
				
			||||||
 | 
						golang.org/x/sync v0.8.0 // indirect
 | 
				
			||||||
 | 
						golang.org/x/text v0.18.0 // indirect
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
							
								
								
									
										21
									
								
								server/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								server/go.sum
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,21 @@
 | 
				
			||||||
 | 
					github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
				
			||||||
 | 
					github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
 | 
				
			||||||
 | 
					github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
 | 
				
			||||||
 | 
					github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
 | 
				
			||||||
 | 
					github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
 | 
				
			||||||
 | 
					github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
 | 
				
			||||||
 | 
					github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
 | 
				
			||||||
 | 
					github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
 | 
				
			||||||
 | 
					github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
 | 
				
			||||||
 | 
					github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
				
			||||||
 | 
					github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
				
			||||||
 | 
					github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 | 
				
			||||||
 | 
					github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
				
			||||||
 | 
					golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
 | 
				
			||||||
 | 
					golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
 | 
				
			||||||
 | 
					golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
 | 
				
			||||||
 | 
					golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
 | 
				
			||||||
 | 
					golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
 | 
				
			||||||
 | 
					golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
 | 
				
			||||||
 | 
					gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
				
			||||||
 | 
					gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
				
			||||||
							
								
								
									
										94
									
								
								server/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								server/main.go
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,94 @@
 | 
				
			||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/jackc/pgx/v5/pgxpool"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// db connection info
 | 
				
			||||||
 | 
					// !MIGHT CHANGE
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						host     = "localhost"
 | 
				
			||||||
 | 
						port     = 5432
 | 
				
			||||||
 | 
						user     = "asdfuser"
 | 
				
			||||||
 | 
						password = "asdfpassword"
 | 
				
			||||||
 | 
						dbname   = "asdfdb"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var pool *pgxpool.Pool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// initialize connection to db
 | 
				
			||||||
 | 
					func connectToDb() (*pgxpool.Pool, error) {
 | 
				
			||||||
 | 
						// this server is intended to be ran on the same system as the db
 | 
				
			||||||
 | 
						dbUrl := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", user, password, host, port, dbname)
 | 
				
			||||||
 | 
						config, err := pgxpool.ParseConfig((dbUrl))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("unable to parse data URL: %w", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pool, err := pgxpool.NewWithConfig(context.Background(), config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("unable to create connection pool: %w", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						log.Println("Connected to DB :)")
 | 
				
			||||||
 | 
						return pool, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// take http reqeust body as raw sql and pass to db
 | 
				
			||||||
 | 
					func executeSql(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						// read the request body
 | 
				
			||||||
 | 
						sqlQuery, err := io.ReadAll(r.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							http.Error(w, "Failed to read request body", http.StatusBadRequest)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer r.Body.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// execute the sql query without any sanitization
 | 
				
			||||||
 | 
						rows, err := pool.Query(context.Background(), string(sqlQuery))
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							http.Error(w, "Query execution error", http.StatusInternalServerError)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer rows.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// prepare the response by iterating over the returned rows
 | 
				
			||||||
 | 
						var response string
 | 
				
			||||||
 | 
						for rows.Next() {
 | 
				
			||||||
 | 
							values, err := rows.Values()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								http.Error(w, "Error reading query result", http.StatusInternalServerError)
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							response += fmt.Sprintf("%v\n", values)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// send the response to the client
 | 
				
			||||||
 | 
						w.Write([]byte(response))
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// setup the http server
 | 
				
			||||||
 | 
					func serveApi() {
 | 
				
			||||||
 | 
						http.HandleFunc("/executeSql", executeSql)
 | 
				
			||||||
 | 
						log.Println("Unsecure server is running on http://localhost:3001")
 | 
				
			||||||
 | 
						if err := http.ListenAndServe(":3001", nil); err != nil {
 | 
				
			||||||
 | 
							log.Fatalf("Failed to start server: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						var err error
 | 
				
			||||||
 | 
						pool, err = connectToDb()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Fatalf("Failed to connect to db: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer pool.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						serveApi()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in a new issue