Thursday, May 23, 2024
HomeGolangHow do I get the present url with question string - Getting...

How do I get the present url with question string – Getting Assist


I need to get the present url with question string.

The rationale for that’s…

I need to do pagination in Golang frontend. I’m pondering the next.
First I’ve to get the present url. Then parse it. Add the web page url values to the url and ship it to the template.

How do I get the url?
Do I have to construct it from *http.Request?
Or is it already accessible?

Extra context ought to be useful. That is my try to know your query (a shot in the dead of night):

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

// break up url
func getpath(path string) (module, mode, val string) {
	elements := strings.Break up(path, "https://discussion")
	change len(elements) {
	case 4:
		val = elements[3]
	case 3:
		mode = elements[2]
	case 2:
		module = elements[1]
	return // Named return values are used, so simply return right here

1 Like

I show the customers listing in html desk. (utilizing go template and vary)

I need to add pagination for that.
I simply need to implement subsequent and former buttons.

The url already has question strings generally and plain url generally.
So how do I add both <a href="https://discussion page=4" <a href="https://discussion page=4"

How do I do know the url has some question strings already?
I used to be pondering that, if I can get the present url, I can parse it and add values.add("web page", "2")

Am I incorrect?

In easy, I simply need the following button to have subsequent web page and former button to have earlier web page

I’m about to implement this for my very own as quickly i received the time. However I’m going to make use of “infinitive scrolling” as an alternative (about the identical as pagination however with automated web page loading). I’ve no I thought how to do that simply now. Aside from utilizing agGrid or comparable Javascript libraries with this inbuilt capabilities. However I don’t like Javascript that a lot, so I’ll strive the Go method.

My purpose is to do server aspect infinite scrolling. I have no idea if that is doable, however I need to give it a strive.

Perhaps in a few weeks, I may give you a greater reply.

For now I’ll let Go fetch all data and render the web page att loading. A number of hundred data this can be sufficient.

I assume you merely add this to the “subsequent” button indirectly. However I assume it’s important to retailer the “present” web page quantity in sessionStorage and increment it or comparable. E g sessionStorage.web page+1

1 Like

Please submit right here when you do the infinite scrolling. Will probably be useful.

Since you possibly can’t scroll on the server, there should be a client-side component to this. The shopper aspect code isn’t truly that difficult. Again within the day it was as a result of browser compatibility was a nightmare (which is why we used issues like jQuery) however lately it’s truly comparatively easy. Do a seek for “pure JavaScript infinite scroll” and you need to discover many examples you possibly can construct on.

To get querystring params, do one thing alongside the traces of this:

// Assuming r is kind *http.Request
web page := r.URL.Question().Get("web page")

See additionally:

However I can get the “scroll worth” utilizing Javascript after which name the Go server based mostly on this worth. However as I mentioned, that is in solely my desires… :slight_smile:

Proper – that’s the client-side side of it I discussed. You’ll be able to actually render on the server after which return a piece of HTML to the shopper and append it to a component within the DOM.

1 Like

Sure. I received the question string utilizing the above code.

web page := r.URL.Question().Get("web page")

I convert the web page to int.

//lets assume there's at all times a web page question string with some int worth
pageInt, _ := strconv.Atoi(web page)
//after conforming pageInt is definitely a int.
//for simplicity, lets assume there's a subsequent web page at all times
nextPage := pageInt + 1

within the template web page

//which url do I take advantage of now?
<a href="https://discussion page={{.Subsequent}}">Subsequent</a>
//This isn't a good suggestion. If the url already has some question string, then it would seem like this
// page=2

<a href="&web page={{.Subsequent}}">Subsequent</a>
//That is additionally not proper,
//If the url has no question string, then it is going to be like this
// page=2

So I’ve to go the whole url for subsequent button from go code. Am I proper?
Or is there a method to do that merely?

One thing like this (pseudo code not examined). I believe chances are you’ll retailer the present web page quantity in sessionStorage utilizing Javascript as effectively. The incremented web page ought to be despatched from browser IMO.

<button><a href="">Earlier</a>s</button>
<button><a href=" page=sessionStorage.web page+1">Subsequent</a></button>

OR ship to a perform that takes care of the incrementing and storage in a single step:

<button onclick="prev()">Earlier</button>
<button onclick="subsequent()">Subsequent</button>

1 Like

Thanks a lot. I’ve an unrelated query. Please information me.

Golang doesn’t have in constructed session supervisor. What’s your suggestion? Do I’ve to create one myself? or use third occasion library? Which library do you employ and suggest?

Are we speaking about “session” in type of authentication? Or session pooling for the database?

Session for authentication in addition to for sustaining the state

This can be a bit tougher. In the event you ask 10 individuals, chances are you’ll get 10 totally different solutions. I can guarantee you that others have totally different opinions. And I stay up for hear them :slight_smile: However that is how I interpret session dealing with:

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

  2. Retrieve this session_id and retailer on the internet server in some type of cache. One thing like cache2go. Set the cache to run out in x hours

  3. Make a session cookie within the browser with similar expire time because the server cache.

  4. When the consumer ship an url to the net server, verify that this cookie exists within the cache.

  5. Retrieve essential knowledge based mostly on this session cookie from the authentication server OR already saved within the session cache.

My opinion is {that a} session_id is innocent to retailer within the browser. As all different processes are dealt with by the net server and/or authentication server. And if the session cookie is manipulated, the session will fail, as the brand new cookie doesn’t match with the cached model within the net server.

By utilizing a cached session_id, there can be much less knowledge site visitors to the authentication server.

Now solely I perceive why did you ask what sort of session.
It appears it’s going to be an enormous work.

Why do you want classes? For what function?

http is stateless. We don’t know which consumer has logged in on web page refresh. To maintain monitor the consumer and their position.

Person and position. Means to me each authentication (who’re you?) and authorization (what are you allowed to do?).

However preserve monitor of the consumer could a session_id be sufficient. I’m making an attempt to do that utilizing 3 servers.

  1. Authentication server (who’re you?) (set session_id and preserve monitor of customers)
  2. REST API server (talk with the database)
  3. Internet Server (endpoints for pages and many others).

So as to add a job (authorization) is one other layer. As it could contain totally different ranges of entry. Do you want this?

1 Like

Sure. I want authorisation and management menus in accordance the signed in consumer.
Some customers can solely view and a few customers could make posts.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments