I'm new to Go and I have the following problem. I tried to simplify it:
I have a server which has for example a global variable myvar
. All users can POST the endpoint /step1
and save some data in the variable, which can be retrieved with a GET using the second endpoint /step2
. Between these 2 calls the value of myvar
shouldn't change for that user.
I would like to know if there is a way to instantiate this process for every user, because I need that if one user changes the variable, it doesn't affect the other users. I don't necessarily need to use the global variable, it is just to expose what I want to do with the endpoints.
Code:
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/rs/cors"
"fmt"
)
type Test struct {
test string `json:"test,omitempty"`
}
func main() {
var myvar = "test"
router := mux.NewRouter()
router.HandleFunc("/step1", func(w http.ResponseWriter, r *http.Request) {
var test Test
_ = json.NewDecoder(r.Body).Decode(&test)
myvar = test.test
})
router.HandleFunc("/step2", func(w http.ResponseWriter, r *http.Request) {
fmt.Println(myvar)
})
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{"*"},
})
handler := c.Handler(router)
http.ListenAndServe(":8003", handler)
}