Struct value been stored in every request Golang using net/http - go

I am new to Golang, I am testing the net/http to run some path but I got some problem that I don't understand.
Here is my codes.
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404");
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
So, if I go to index http://localhost:8080/, I got empty content index content {{false } map[]} ,
And I goto http://localhost:8080/test I got the content correctly , test content {{false } map[data1:data 1 content data2:data 2 content]},
But when I go back to index http://localhost:8080/ there already content there index content {{false } map[data1:data 1 content data2:data 2 content]},
So question here, why am I not getting the empty struct content when I back to the index? I thought the struct will be in initial state with every single request? The http should be stateless, right?

What you are probably experiencing is the result of this code or something similar (your code does not compile):
package main
import (
"fmt"
"net/http"
)
var Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
Solution
With this you are creating a global variable Content that keeps its state across calls to the webserver. What you probably intended is this:
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
var c Content
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", c)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
var c Content
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
c.Data = data
fmt.Println("test content ", c)
}
Changes made
make Content a type as you already did in your sample (that way it is not a global variable any more but defining a type we can reuse)
declare Content in each call where it is needed (not globally as we do not want it to keep its content across server calls)
Essence
You cannot use a type without declaring a variable from it first. That is why your sample did not build. If you try go will complain that Content is not an expression.

Related

Simplifying route handler to handle parameters without framework

In rocket.rs, we've this simple route code:
#[get("/hello/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
where if you were to visit http://localhost:8000/hello/John/58 in the browser, you’d see:
Hello, 58 year old named John!
I read this, but the accepted answer is about a way to do Go url parameters mapping for single route, that could read http://localhost:8080/blob/123/test as /blob/{id}/test and display the required route.
I know there are some great routers/frameworks there, but looking to build simple code myself to understand http route handlers in a better way.
Let's say I've:
type Tender struct {
tenderReference string
venderCode int
}
func (t Tender) readWrite() {
fmt.Printf("Tender %s is ready for vendor %d to review and submit\n", t.tenderReference, t.venderCode)
}
func (t Tender) readOnly(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Tender %s already submitted by vender %d\n", t.tenderReference, t.venderCode)
}
And want my routes to be something like:
/api/tender/readWrite/{tenderReference}/vendor/{venderCode} that is calling func (t Tender) readWrite(){}
/api/tender/readOnly/{tenderReference}/vendor/{venderCode} that is calling func (t Tender) readOnly(){}
How many route handler do I have to build?
I solved it as below, other thoughts are welcomed:
404.go
package main
import (
"fmt"
"net/http"
)
func handle404(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "mmm, it looks you are playing around, page is not available :)\n")
}
getField.go
package main
import "net/http"
type ctxKey struct{}
func getField(r *http.Request, index int) string {
fields := r.Context().Value(ctxKey{}).([]string)
return fields[index]
}
routes.go
package main
import (
"net/http"
"regexp"
)
type route struct {
method string
regex *regexp.Regexp
handler http.HandlerFunc
}
var routes = []route{
newRoute("GET", "/api/tender/(rw|r)/([^/]+)/vendor/([0-9]+)", apiTenders),
}
func newRoute(method, pattern string, handler http.HandlerFunc) route {
return route{method, regexp.MustCompile("^" + pattern + "$"), handler}
}
tendor.go
package main
import (
"fmt"
"net/http"
"strconv"
)
type Tender struct {
tenderReference string
venderCode int
}
// Handles GET /api/tender/(rw|r)/([^/]+)/vendor/([0-9]+)
func apiTenders(w http.ResponseWriter, r *http.Request) {
action := getField(r, 0)
tenderReference := getField(r, 1)
venderCode, _ := strconv.Atoi(getField(r, 2))
tender := Tender{tenderReference, venderCode}
switch action {
case "rw":
tender.readWrite(w, r) // Display tender and allow vendor to submit feedback
case "r":
tender.readOnly(w, r) // Display readOnly copy of the tender
default:
fmt.Fprintf(w, "Tendert ERROR\n")
}
}
func (t Tender) readWrite(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Tender %s is ready for vendor %d to review and submit\n", t.tenderReference, t.venderCode)
}
func (t Tender) readOnly(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Tender %s already submitted by vender %d\n", t.tenderReference, t.venderCode)
}
server.go
package main
import (
"context"
"fmt"
"net/http"
"strings"
)
type apiHandler struct{}
func main() {
http.Handle("/api/", apiHandler{})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check
// that we're at the root here.
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
fmt.Fprintf(w, "Welcome to the home page!")
})
http.ListenAndServe(":8000", nil)
}
func (apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var allow []string
for _, route := range routes {
matches := route.regex.FindStringSubmatch(r.URL.Path)
if len(matches) > 0 {
if r.Method != route.method {
allow = append(allow, route.method)
continue
}
ctx := context.WithValue(r.Context(), ctxKey{}, matches[1:])
route.handler(w, r.WithContext(ctx))
return
}
}
if len(allow) > 0 {
w.Header().Set("Allow", strings.Join(allow, ", "))
http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed)
return
}
handle404(w, r)
//http.NotFound(w, r)
}

How do I use Twitter API with github.com/ChimeraCoder/anaconda?

I have a web API written Go, I would like to use it to consume the Twitter API and return some properties from a user's timeline.
I have added https://github.com/ChimeraCoder/anaconda to my web API, however I cannot understand from the docs just how to get a timeline for a user.
This is my application currently.
I am trying to use the FeedHandler method. I can trigger a search, but when it comes to actually returning a user's timeline, I am stuck.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"github.com/ChimeraCoder/anaconda"
"github.com/gorilla/mux"
)
type Meta struct {
Description string `json:"description"`
Version string `json:"version"`
Bugs GitHubLink `json:"bugs"`
}
type GitHubLink struct {
Url string `json:"url"`
}
type ErrorMessage struct {
Status int `json:"status"`
Message string `json:"message"`
}
var (
consumerKey = getenv("TWITTER_CONSUMER_KEY")
consumerSecret = getenv("TWITTER_CONSUMER_SECRET")
)
func getenv(name string) string {
v := os.Getenv(name)
if v == "" {
panic("required environment variable " + name + "is missing")
}
return v
}
func main() {
anaconda.SetConsumerKey(consumerKey)
anaconda.SetConsumerSecret(consumerSecret)
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/feed/{screenName}", FeedHandler)
r.HandleFunc("/healthz", HealthzHandler)
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
log.Fatal(http.ListenAndServe(":8000", r))
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
meta := Meta{
Description: "API returning twitter.com posted images",
Version: "0.0.0",
Bugs: GitHubLink{Url: "https://foo.bar"}}
json.NewEncoder(w).Encode(meta)
}
func FeedHandler(w http.ResponseWriter, r *http.Request) {
api := anaconda.NewTwitterApi("", "")
// vars := mux.Vars(r)
tweets := api.GetUserTimeline('<users_screen_name>')
// searchResult, _ := api.
// for _, tweet := range searchResult.Statuses {
// fmt.Println(tweet.Text)
// }
fmt.Fprintf(w, "Not Implemented Yet")
}
func HealthzHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-cache, no-store, max-age=0")
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
errorMessage := ErrorMessage{Status: 404, Message: "Request resource does not exist"}
json.NewEncoder(w).Encode(errorMessage)
}
You need to pass in the user's screen name as a property on url.Values{}
func FeedHandler(w http.ResponseWriter, r *http.Request) {
api := anaconda.NewTwitterApi("", "")
v := url.Values{}
v.Set("screen_name", "some_user_name")
searchResult, _ := api.GetUserTimeline(v)
json.NewEncoder(w).Encode(searchResult)
}

http HandleFunc argument in golang

I want to use a rate limiting or throttler library to limit the number of client requests. I use a vendor library in my code base. I want to pass in a ResponseWriter, Request and a third variable retrieved from the URL. When I use the library for throttling, it gives me back a handler that only handles two arguments. How can I pass my third argument into the handler?
Here is my current code:
package main
import (
"fmt"
"github.com/didip/tollbooth"
"net/http"
)
func viewHandler(
w http.ResponseWriter,
r *http.Request,
uniqueId string,
) {
//data := getData(uniqueId)
fmt.Println("Id:", uniqueId)
p := &objects.ModelApp{LoggedUser: "Ryan Hardy", ViewData: "data"}
renderTemplate(w, "view", p)
}
//URL validation for basic web services
var validPath = regexp.MustCompile("^/$|/(home|about|view)/(|[a-zA-Z0-9]+)$")
func makeHandler(
fn func(
http.ResponseWriter,
*http.Request, string,
)) http.HandlerFunc {
return func(
w http.ResponseWriter,
r *http.Request,
) {
m := validPath.FindStringSubmatch(r.URL.Path)
if m == nil {
http.NotFound(w, r)
return
}
fn(w, r, m[2])
}
}
func main() {
http.Handle("/view/", makeHandler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, time.Second), viewHandler)))
http.ListenAndServe(":8080", nil)
}
Could anyone help me with this?
I'm on my phone so this may be difficult to type but you could use the http.Handle function which takes an interface of Handler something like
type makeHandler struct {
YourVariable string
}
func (m *makeHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
yourVariableYouNeed := m.YourVariable
// do whatever
w.Write()
}
// do whatever you need to get your variable
blah := &makeHandler{ yourThing }
http.Handle("/views", blah)
On my phone so can't test but it should work, let me know if it doesn't.

mux.Vars is empty when using httputil.ReverseProxy

I am trying to use gorilla mux and httputil.ReverseProxy together, but when trying to get the mux.Vars it is empty. According to https://golang.org/src/net/http/httputil/reverseproxy.go?s=2744:2819#L93 it seems like the http.Request pointer is a shallow copy of the original request, which should still work.
Any ideas?
https://play.golang.org/p/JpjNvEMIFB
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
type route struct {
match string
base string
}
var routes = []route{
// proxy http://localhost:3000/api/foo/bar => https://api.bar.com/5/foo/bar
route{match: "/api/{path}", base: "https://api.bar.com/5"},
route{match: "/sales/{path}", base: "https://sales.bar.com/3"},
}
func NewProxy(r *route) http.Handler {
director := func(req *http.Request) {
out, _ := url.Parse(r.base)
req.URL.Scheme = out.Scheme
req.URL.Host = out.Host
req.URL.Path = out.Path + "/" + mux.Vars(req)["path"] // mux Vars are empty here
}
return &httputil.ReverseProxy{Director: director}
}
func main() {
for _, route := range routes {
http.Handle(route.match, NewProxy(&route))
}
log.Println("Listening on port 8080")
http.ListenAndServe(":8080", nil)
}
You have two different problems here.
The first one, you are not using a mux.Router, so gorilla/mux has not the opportunity to pre-process your request. In other words, the requests are going directly from http package to your reverse proxies. This issue has an easy fix:
r := mux.NewRouter()
for _, route := range routes {
r.Handle(route.match, NewProxy(&route))
}
http.Handle("/", r)
The second problem is more tricky than the first one. This issue is related to how is mux package implemented. If you look mux.Vars() implementation, you will see that it uses something called Context. A Context, as described in the official documentation, is something that stores values shared during a request lifetime. A simplified Context implementation will be:
type Context map[*http.Request]interface{}
func (c Context) Set(req *http.Request, v interface{}) {
c[req] = v
}
func (c Context) Get(req *http.Request) interface{} {
return c[req]
}
As you see, given a http.Request, we can store values in a context. Later we can retrieve these values using the same Context and the same http.Request. mux uses a global Context to store the vars parsed in routing process so that you can use the standard http.request. But, because httputil.ReverseProxy passes a copy of the actual request and Context links values by request, this new Request has no values in the Context.
To fix it, you can implement your own ReverseProxy based on httputil.ReverseProxy:
type MyReverseProxy struct {
httputil.ReverseProxy
Director func(inr, outr *http.Request)
}
func (p *MyReverseProxy) ServeHTTP(rw http.ResponseWriter, inr *http.Request) {
p.ReverseProxy.Director = func(outr *http.Request) {
p.Director(inr, outr)
}
p.ReverseProxy.ServeHTTP(rw, inr)
}
func NewProxy(r *route) http.Handler {
director := func(inr, outr *http.Request) {
out, _ := url.Parse(r.base)
outr.URL.Scheme = out.Scheme
outr.URL.Host = out.Host
outr.URL.Path = out.Path + "/" + mux.Vars(inr)["path"]
log.Printf("IN VARS: %#v\n", mux.Vars(inr)) // Now inr has proper vars
log.Printf("OUT VARS: %#v\n", mux.Vars(outr))
}
return &MyReverseProxy{Director: director}
You can even use context and keep Director declaration:
type MyReverseProxy struct {
httputil.ReverseProxy
Director func(req *http.Request)
}
func (p *MyReverseProxy) ServeHTTP(rw http.ResponseWriter, inr *http.Request) {
p.ReverseProxy.Director = func(outr *http.Request) {
context.Set(outr, "in_req", inr)
p.Director(outr)
}
p.ReverseProxy.ServeHTTP(rw, inr)
}
func NewProxy(r *route) http.Handler {
director := func(outr *http.Request) {
out, _ := url.Parse(r.base)
inr := context.Get(outr, "in_req").(*http.Request)
outr.URL.Scheme = out.Scheme
outr.URL.Host = out.Host
outr.URL.Path = out.Path + "/" + mux.Vars(inr)["path"]
log.Printf("IN VARS: %#v\n", mux.Vars(inr)) // Now inr has proper vars
log.Printf("OUT VARS: %#v\n", mux.Vars(outr))
}
return &MyReverseProxy{Director: director}
}
Both implementations seem tricky to me. They have to change httputil.ReverseProxy's Director in every call. So, I probably accept that mux is not a good choice here, and instead I will use some simpler solution:
var routes = []route{
route{match: "/api/", base: "https://api.bar.com/5"},
route{match: "/sales/", base: "https://sales.bar.com/3"},
}
func NewProxy(r *route) http.Handler {
director := func(req *http.Request) {
out, _ := url.Parse(r.base)
req.URL.Scheme = out.Scheme
req.URL.Host = out.Host
req.URL.Path = out.Path + "/" + strings.TrimPrefix(req.URL.Path, r.match)
}
return &httputil.ReverseProxy{Director: director}
}
You can read mux source code to implement a complex solution based on regular expressions.

one common http handler instead of several

Is it possible to not copy paste expression commonHanlder(handler1), commonHanlder(handler2) ... commonHanlder(handlerN) in this code:
rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")
and set it in one place like
http.ListenAndServe(":3000", commonHanlder(http.DefaultServeMux))
But this variant is not working and gives two errors on compile:
./goRelicAndMux.go:20: cannot use http.DefaultServeMux (type *http.ServeMux) as type gorelic.tHTTPHandlerFunc in argument to commonHanlder
./goRelicAndMux.go:20: cannot use commonHanlder(http.DefaultServeMux) (type gorelic.tHTTPHandlerFunc) as type http.Handler in argument to http.ListenAndServe:
gorelic.tHTTPHandlerFunc does not implement http.Handler (missing ServeHTTP method)
The full code:
package main
import (
"github.com/gorilla/mux"
"github.com/yvasiyarov/gorelic"
"log"
"net/http"
)
func main() {
initNewRelic()
rtr := mux.NewRouter()
var commonHanlder = agent.WrapHTTPHandlerFunc
rtr.HandleFunc("/", commonHanlder(handler1)).Methods("GET")
rtr.HandleFunc("/page2", commonHanlder(handler2)).Methods("GET")
http.Handle("/", rtr)
log.Println("Listening...")
http.ListenAndServe(":3000", http.DefaultServeMux)
}
func handler1(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("mainPage"))
}
func handler2(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("page 2"))
}
var agent *gorelic.Agent
func initNewRelic() {
agent = gorelic.NewAgent()
agent.Verbose = true
agent.NewrelicName = "test"
agent.NewrelicLicense = "new relic key"
agent.Run()
}
It seems like you want to call commonHandler on the root of your application and have it work for all. Since you are using mux, just wrap the mux router once.
func main() {
initNewRelic()
rtr := mux.NewRouter()
var commonHandler = agent.WrapHTTPHandler
rtr.HandleFunc("/", handler1).Methods("GET")
rtr.HandleFunc("/page2", handler2).Methods("GET")
http.Handle("/", commonHandler(rtr))
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
I also removed the http.DefaultServeMux reference in ListenAndServe since passing nil will automatically use the default.

Resources