How do I get the current url with query string

Hi

I want to get the current url with query string.

The reason for that is…

I want to do pagination in Golang frontend. I am thinking the following.
First I have to get the current url. Then parse it. Add the page url values to the url and send it to the template.

How do I get the url?
Do I need to build it from *http.Request?
Or is it already available?

More context should be helpful. This is my attempt to understand your question (a shot in the dark):

func endpoint(w http.ResponseWriter, r *http.Request) {
	module, mode, val := getpath(r.URL.Path)
}

// split url
func getpath(path string) (module, mode, val string) {
	parts := strings.Split(path, "/")
	switch len(parts) {
	case 4:
		val = parts[3]
		fallthrough
	case 3:
		mode = parts[2]
		fallthrough
	case 2:
		module = parts[1]
	}
	return // Named return values are used, so just return here
}
1 Like

I display the users list in html table. (using go template and range)

I want to add pagination for that.
I just want to implement next and previous buttons.

The url already has query strings sometimes and plain url sometimes.
So how do I add either <a href="?page=4" <a href="&page=4"

How do I know the url has some query strings already?
I was thinking that, if I can get the current url, I can parse it and add values.add("page", "2")

Am I wrong?

In simple, I just want the next button to have next page and previous button to have previous page

I am about to implement this for my own as soon i got the time. But I am going to use “infinitive scrolling” instead (about the same as pagination but with automatic page loading). I have no I idea how to do this just now. Other than using agGrid or similar Javascript libraries with this built in functions. But I do not like Javascript that much, so I will try the Go approach.

My goal is to do server side infinite scrolling. I do not know if this is possible, but I want to give it a try.

Maybe in a couple of weeks, I can give you a better answer.

For now I will let Go fetch all records and render the page att loading. A few hundred records this will be enough.

I guess you simply add this to the “next” button in some way. But I guess you have to store the “current” page number in sessionStorage and increment it or similar. E g sessionStorage.page+1

1 Like

Please post here if you do the infinite scrolling. It will be helpful.

Since you can’t scroll on the server, there will have to be a client-side element to this. The client side code isn’t actually that complicated. Back in the day it was because browser compatibility was a nightmare (which is why we used things like jQuery) but these days it is actually relatively simple. Do a search for “pure JavaScript infinite scroll” and you should find many examples you can build on.

To get querystring params, do something along the lines of this:

// Assuming r is type *http.Request
page := r.URL.Query().Get("page")

See also:

But I can get the “scroll value” using Javascript and then call the Go server based on this value. But as I said, this is in only my dreams… :slight_smile:

Right - that’s the client-side aspect of it I mentioned. You can certainly render on the server and then return a chunk of HTML to the client and append it to an element in the DOM.

1 Like

Yes. I got the query string using the above code.
But

page := r.URL.Query().Get("page")

I convert the page to int.

//lets assume there is always a page query string with some int value
pageInt, _ := strconv.Atoi(page)
//after conforming pageInt is actually a int.
//for simplicity, lets assume there is a next page always
nextPage := pageInt + 1

in the template page

//which url do I use now?
<a href="?page={{.Next}}">Next</a>
//This is not a good idea. If the url already has some query string, then it will look like this
//http://someurl.com/somepage?somequery=somevalue?page=2

<a href="&page={{.Next}}">Next</a>
//This is also not right,
//If the url has no query string, then it will be like this
//http://someuser.com/somepage&page=2

So I have to pass the entire url for next button from go code. Am I right?
Or is there a way to do this simply?

Something like this (pseudo code not tested). I think you may store the current page number in sessionStorage using Javascript as well. The incremented page should be sent from browser IMO.

<button><a href="https://site.org?page=sessionStorage.page-1">Previous</a>s</button>
<button><a href="https://site.org?page=sessionStorage.page+1">Next</a></button>

OR send to a function that takes care of the incrementing and storage in one step:

<button onclick="prev()">Previous</button>
<button onclick="next()">Next</button>
1 Like

Thank you so much. I have an unrelated question. Please guide me.

Golang does not have in built session manager. What is your recommendation? Do I have to create one myself? or use third party library? Which library do you use and recommend?

Are we talking about “session” in form of authentication? Or session pooling for the database?

Session for authentication as well as for maintaining the state

This is a bit harder. If you ask 10 persons, you may get 10 different answers. I can assure you that others have different opinions. And I look forward to hear them :slight_smile: But this is how I interpret session handling:

  1. Use an authentication server that produces an uuid as session_id and store the session_id in a database for a limited time (days).

  2. Retrieve this session_id and store on the web server in some form of cache. Something like cache2go. Set the cache to expire in x hours

  3. Make a session cookie in the browser with same expire time as the server cache.

  4. When the user send an url to the web server, check that this cookie exists in the cache.

  5. Retrieve necessary data based on this session cookie from the authentication server OR already stored in the session cache.

My opinion is that a session_id is harmless to store in the browser. As all other processes are handled by the web server and/or authentication server. And if the session cookie is manipulated, the session will fail, as the new cookie does not match with the cached version in the web server.

By using a cached session_id, there will be less data traffic to the authentication server.

Now only I understand why did you ask what kind of session.
It seems it is going to be a big work.

Why do you need sessions? For what purpose?

http is stateless. We don’t know which user has logged in on page refresh. To keep track the user and their role.

User and role. Means to me both authentication (who are you?) and authorization (what are you allowed to do?).

But keep track of the user may a session_id be enough. I am trying to do this using 3 servers.

  1. Authentication server (who are you?) (set session_id and keep track of users)
  2. REST API server (communicate with the database)
  3. Web Server (endpoints for pages etc).

To add a role (authorization) is another layer. As it may involve different levels of access. Do you need this?

1 Like

Yes. I need authorisation and control menus according the signed in user.
Some users can only view and some users can make posts.