Parcourir la source

Working signup

Aaro Saila il y a 2 semaines
Parent
commit
19a8fd206c

+ 1 - 1
makefile

@@ -1,6 +1,6 @@
 .PHONY: run
 run:
-	go -C ./src/auth-server/ run .
+	go -C ./src/auth-server/ run . -res-path ./res
 
 .PHONY: schema
 schema:

+ 5 - 1
src/auth-server/controllers/user.go

@@ -19,7 +19,11 @@ func UserSignup(c *gin.Context) {
 		return
 	}
 
-	conn := db.Connect()
+	conn, err := db.Connect()
+	if err != nil {
+		c.JSON(http.StatusInternalServerError, err.Error())
+		return
+	}
 	defer conn.Close(context.Background())
 	rows := conn.QueryRow(
 		context.Background(),

+ 2 - 8
src/auth-server/db/db.go

@@ -2,21 +2,15 @@ package db
 
 import (
 	"context"
-	"fmt"
-	"os"
 
 	"github.com/jackc/pgx/v5"
 )
 
-func Connect() *pgx.Conn {
+func Connect() (*pgx.Conn, error) {
 	conn, err := pgx.Connect(
 		context.Background(),
 		"postgres://pong:pong@localhost:5432/pong",
 	)
-	if err != nil {
-		fmt.Printf("Failed to connect to db: %+v", err)
-		os.Exit(1)
-	}
 
-	return conn
+	return conn, err
 }

+ 2 - 0
src/auth-server/go.mod

@@ -27,6 +27,8 @@ require (
 	github.com/pelletier/go-toml/v2 v2.2.4 // indirect
 	github.com/quic-go/qpack v0.6.0 // indirect
 	github.com/quic-go/quic-go v0.59.0 // indirect
+	github.com/rs/cors v1.11.1 // indirect
+	github.com/rs/cors/wrapper/gin v0.0.0-20261008204034-5d834403882d // indirect
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.3.1 // indirect
 	go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect

+ 4 - 0
src/auth-server/go.sum

@@ -51,6 +51,10 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
 github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
 github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
 github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
+github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
+github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
+github.com/rs/cors/wrapper/gin v0.0.0-20261008204034-5d834403882d h1:NOMR2Xi+RJj6vDDf6Npx5F+Rl4VbPXuUIzz+676HONo=
+github.com/rs/cors/wrapper/gin v0.0.0-20261008204034-5d834403882d/go.mod h1:UkcVz4d5PVHMbLZcN5lqy4KOGXiO9vVdxlITo+boMCE=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
 github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

+ 13 - 1
src/auth-server/main.go

@@ -1,13 +1,25 @@
 package main
 
 import (
+	"flag"
+
 	"github.com/gin-gonic/gin"
+	cors "github.com/rs/cors/wrapper/gin"
 )
 
+var resPath string = ""
+
 func main() {
+	_resPath := flag.String("res-path", "./res", "Path to web resources")
+	flag.Parse()
+
+	resPath = *_resPath
+
 	r := gin.Default()
 
+	r.Use(cors.Default())
+
 	RoutesSet(r)
 
-	r.Run()
+	r.Run("127.0.0.1:8080")
 }

+ 17 - 0
src/auth-server/res/index.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <script src="index.js" defer></script>
+        <title>Pong</title>
+    </head>
+    <body>
+        <h1>Signup</h1>
+        <label>Email</label>
+        <input id="input-signup-email" type=email></input>
+        <label>Password</label>
+        <input id="input-signup-password" type=password></input>
+        <button id="button-signup" type=button>Send</button>
+
+        <p id="result-text"></p>
+    </body>
+</html>

+ 24 - 0
src/auth-server/res/index.js

@@ -0,0 +1,24 @@
+const inputEmail = document.getElementById("input-signup-email");
+const inputPassword = document.getElementById("input-signup-password");
+const buttonSignup = document.getElementById("button-signup");
+const resultTxt = document.getElementById("result-text");
+
+buttonSignup.onclick = async () => {
+    const email = inputEmail.textContent;
+    const password = inputPassword.textContent;
+
+    const res = await fetch("http://127.0.0.1:8080/api/user/signup", {
+        method: "POST",
+        headers: {
+            "Content-Type": "application/json"
+        },
+        body: JSON.stringify({
+            "email": email,
+            "password": password
+        })
+    });
+
+    const resJson = await res.text();
+
+    resultTxt.textContent = `${res.status} ${resJson}`;
+};

+ 7 - 2
src/auth-server/routes.go

@@ -1,17 +1,22 @@
 package main
 
 import (
+	"fmt"
 	"pong-auth/controllers"
 
 	"github.com/gin-gonic/gin"
 )
 
-func UserRoutes(r *gin.Engine) {
+func UserRoutes(r *gin.RouterGroup) {
 	g := r.Group("user")
 
 	g.POST("/signup", controllers.UserSignup)
 }
 
 func RoutesSet(r *gin.Engine) {
-	UserRoutes(r)
+	fmt.Println(resPath)
+	r.Static("/", resPath)
+
+	g := r.Group("api")
+	UserRoutes(g)
 }