Http Server how do I keep each client data seperately

I have a straightforward HTTP server with one route. Can someone help me set up the server so that when I call the route localhost:8090/api//m_test for each connected client, it keeps the random values for each connection separately? I opened two sessions, one in Postman and one in the browser, and I am getting all the values from each client in the previous map. Do I have to configure the server separately or set up channels to separate the data?

The values seem to be appended for each connected client, for example:
{
“current”: “jZjHwForIZ”,
“previous”: [
“pPevdwCPHD”,
“pcifcDJtDg”,
“eHemlejDlt”,
“nlEGpitRVL”,
“xChOocByrP”
]
}

Code below:

package main

import (
	"github.com/gin-gonic/gin"
	"src/src/textfiles"
)

func main() {

	router := initRouter()

	router.Run(":8090")

}
func initRouter() *gin.Engine {

	router := gin.Default()

	router.Use(gin.Logger())
	router.Use(gin.Recovery())
	api := router.Group("/api")

	api.GET("/m_test", textfiles.RandomTexts)

	return router
}
func CORSMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {

		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Credentials", "false")
		c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
		c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT")

		if c.Request.Method == "OPTIONS" {
			c.AbortWithStatus(204)
			return
		}

		c.Next()
	}
}
package textfiles

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"time"
)

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var previousLetter = []string{}

//var v =""

func RandomTexts(context *gin.Context) {

	v := RandStringRunes(10)

	fmt.Println(v)

	context.JSON(http.StatusCreated, gin.H{"current": v, "previous": previousLetter})
	//context.
	previousLetter = append(previousLetter, v)

	fmt.Println(previousLetter)
}

func RandStringRunes(n int) string {
	b := make([]rune, n)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}
func init() {
	rand.Seed(time.Now().UnixNano())
	previousLetter = []string{}
}

If I understand your question correct: Each database have a “key”. This key is database credentials and consists of ip, database name, user and password. Each database have its own credentials. An for each query you have to send the correct “key” to connect to the correct database. How exactly you do this may differ depending on how you connect to the database.

Hi Sibert,

If I understand you correctly, are you saying I should use sessions to know to whom to send the correct information? The problem I am having is sharing the same data from client to client.

The code
`previousLetter = append(previousLetter, v)’

It is appending for each call to the func ‘RandomTexts.’ I need different values for each connecting client, whether Postman or the browser.

previousLetter is a global variable. It sounds like you want to implement something that most people would refer to as a “session” or something similar in web dev. Usually, at a high level, this looks something like this:

  1. Client (browser) identifies itself to the server in some way (often via authenticating).
  2. Server sends some sort of session identifier back to the client (often JWT or a generated key/GUID of some sort).
  3. Client stores the identifier somewhere (often in a cookie or local storage).
  4. On subsequent requests, client sends identifier (often as a header) so the server knows which session the request belongs to.
2 Likes

I do not get the whole picture yet. But assuming that the same data is shared amongst all users, and you want to keep track of what part of the data a certain user is sharing at the moment. It maybe have to be cached for the user at the browser level. SessionStore, LocalStorage or IndexedDB? As an index or data itself.

Thank you so much. It worked as you suggested.

Hi Sibert,

Thank you for your suggestion. I tried what Dean suggested, and it works now.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.