I am trying to learn web programming with Go.
I stared out with a simple "hello world" web server:
package main
import "fmt"
import "net/http"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
And when I go to
http://localhost:8080/handler
in the browser, the browser canĀ“t seem to find anything and nothing happens. What could be the reason for this?
You mapped your handler to the root ("/") of your server.
Call it like this in your browser
http://localhost:8080/
If you want to map a service to a specific name you can do this :
http.HandleFunc("/something", handler)
Then you would type this in your browser :
http://localhost:8080/something
Related
I am trying to create a route for GET request with the username parameter. After sending the request from postman as localhost:9090/user/?username=abc I am unable to get the response. It is showing 404 error.
func main() {
router := gin.Default()
router.POST("/user", controllers.CreateUser)
router.GET("/user/:username", GetUser)
router.HEAD("/user/:username", controllers.GetUserMeta)
router.Run("localhost:9090")
}
func GetUser(context *gin.Context) {
// get specific record from database
uname := context.Param("username")
fmt.Println("received:", uname)
//context.IndentedJSON(http.StatusOK, user)
}
I am wondering why the GetUser method is not executed.
I am using auth0 and golang for a rest service that is similar implemented as shown here.
I wonder how I can find out the name of the user that is currently triggering a certain API call - for instance if someone requests http://localhost:3000/products - the go handler in this case looks like this:
var ProductsHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
payload, _ := json.Marshal(products)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(payload))
})
Does the request r contain more information about the current user?
Or do I need to find out the current user in the middleware authentication:
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
secret := []byte("{YOUR-AUTH0-API-SECRET}")
secretProvider := auth0.NewKeyProvider(secret)
audience := "{YOUR-AUTH0-API-AUDIENCE}"
configuration := auth0.NewConfiguration(secretProvider, audience, "https://{YOUR-AUTH0-DOMAIN}.auth0.com/", jose.HS256)
validator := auth0.NewValidator(configuration)
token, err := validator.ValidateRequest(r)
if err != nil {
fmt.Println(err)
fmt.Println("Token is not valid:", token)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
} else {
next.ServeHTTP(w, r)
}
})
}
Does the token contain more information about the user?
I am a bit lost here. auth0 works perfectly to ensure that only registered persons can use the REST-API, but I want to deliver user specific information. So it depends on the current user what a REST call is handing back. Initially, I was thinking that auth0 would take care of this. Is there a simple way to achieve this?
Yes, you need to use token to get information about request issue.
To sort all you want you need to take a look to next:
Check out how token extracted in this method: token extraction
And the Claims here: Claims structure
And how combine it here: retrieve Claims
The claims have a field
Issuer string `json:"iss,omitempty"`
you are interested in.
I've unsuccessfully tried to access the Soundcloud API with Go. For any language that isn't directly support by Soundcloud, their API is very convoluted. If anyone has any resources or code examples, I'd appreciate it if someone shared them with me.
My code is as follows:
func main() {
v := url.Values{}
v.Set("scope", "non-expiring")
v.Set("client_id", auth.ClientID)
v.Set("response_type", "code")
v.Set("redirect_uri", auth.RedirectURI)
c.AuthURL = AuthEndpoint + "?" + v.Encode()
c.Values = v.Encode()
res := c.Request("POST", url.Values{})
}
func (c *Client) Request(method string, params url.Values) []byte {
params.Set("client_id", "*************")
reqUrl := "https://api.soundcloud.com/oauth2/token"
req, _ := http.NewRequest(method, reqUrl, strings.NewReader(c.Values))
req.Header.Add("Accept", "application/json")
resp, _ := c.client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
return body
}
i'm active developing a package in go to access/work with Soundcloud API, it has OAuth2 support, and is already usable.
I invite you to look for it. https://github.com/njasm/gosoundcloud
Take in consideration that the package is still under heavy development, the API might change in the future.
You can have a look at yanatan16/golang-soundcloud, even though then authentication part isn't implemented yet (see issues)
There is an oauth class though.
Ad quite a few other calls to the API, for getting SoundCloud objects.
I got something running with the Goji framework:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func main() {
goji.Get("/hello/:name", hello)
goji.Serve()
}
What I was hoping someone could help me do is figure out how when an HTML form is submitted to send that data to Golang code.
So if there is an input field with the name attribute and the value of that is name and the user types a name in there and submits, then on the form submitted page the Golang code will print hello, name.
Here is what I could come up with:
package main
import(
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request){
name := r.PostFormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
func main(){
goji.Handle("/hello/", hello)
goji.Serve()
}
and here is my hello.html file:
in the body:
<form action="" method="get">
<input type="text" name="name" />
</form>
How do I connect hello.html to hello.go so that the Golang code gets what is in the input and returns hello, name in the form submitted page?
I'd greatly appreciate any and all help!
In order to read html form values you have to first call r.ParseForm(). The you can get at the form values.
So this code:
func hello(c web.C, w http.ResponseWriter, r *http.Request){
name := r.PostFormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
Should be this:
func hello(c web.C, w http.ResponseWriter, r *http.Request){
//Call to ParseForm makes form fields available.
err := r.ParseForm()
if err != nil {
// Handle error here via logging and then return
}
name := r.PostFormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
Edit: I should note that this was a point that tripped me up when learning the net/http package
Your form input name, name is the key to be fetched by go program.
<form action="" method="get">
<input type="text" name="name" />
</form>
You can use FormValue
https://golang.org/pkg/net/http/#Request.FormValue
FormValue returns the first value for the named component of the
query. POST and PUT body parameters take precedence over URL query
string values. FormValue calls ParseMultipartForm and ParseForm if
necessary and ignores any errors returned by these functions. If key
is not present, FormValue returns the empty string. To access multiple
values of the same key, call ParseForm and then inspect Request.Form
directly.
func hello(c web.C, w http.ResponseWriter, r *http.Request){
name := r.FormValue("name")
fmt.Fprintf(w, "Hello, %s!", name)
}
If FormFile doesn't works, better use ParseMultiForm
https://golang.org/pkg/net/http/#Request.ParseMultipartForm
You can use ParseMultipartForm
ParseMultipartForm parses a request body as multipart/form-data. The
whole request body is parsed and up to a total of maxMemory bytes of
its file parts are stored in memory, with the remainder stored on disk
in temporary files. ParseMultipartForm calls ParseForm if necessary.
After one call to ParseMultipartForm, subsequent calls have no effect
func hello(c web.C, w http.ResponseWriter, r *http.Request){
name := r.FormValue("name")
r.ParseMultipartForm(32 << 20)
fmt.Fprintf(w, "Hello, %s!", name)
}
Also, a form is useless unless some kind of processing takes place after the form is submitted. So use it accordingly.
In Golang, how do I serve static content out of the root directory while still having a root directory handler for serving the homepage.
Use the following simple web server as an example:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", HomeHandler) // homepage
http.ListenAndServe(":8080", nil)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "HomeHandler")
}
If I do
http.Handle("/", http.FileServer(http.Dir("./")))
I receive a panic saying that I have two registrations for "/". Every Golang example I've found on the internet suggests serving their static content out of different directories, but that doesn't help much for things like sitemap.xml, favicon.ico, robots.txt and other files which are by-practice or mandated to always be served out of the root.
The behavior I seek is the behavior which is found in most web servers such as Apache, Nginx, or IIS, where it first traverses your rules, and if no rule is found it looks for an actual file, and if no file is found it 404s. My guess is that instead of writing a http.HandlerFunc, I need to write a http.Handler which checks if I am referencing a file with an extension, and if so checks for file existence and serves the file, otherwise it 404s or serves the homepage is the request was for "/". Unfortunately I'm not certain how to even begin such a task.
Part of me says I'm massively over-complicating the situation which makes me think that I am missing something? Any guidance would be appreciated.
An alternative (not using ServeMux) solution is to serve explicitly each file located in the root directory. The idea behind is to keep the number of root-based files very small.
sitemap.xml, favicon.ico, robots.txt are indeed mandated to be served out of the root :
package main
import (
"fmt"
"net/http"
)
func HomeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "HomeHandler")
}
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
func main() {
http.HandleFunc("/", HomeHandler) // homepage
// Mandatory root-based resources
serveSingle("/sitemap.xml", "./sitemap.xml")
serveSingle("/favicon.ico", "./favicon.ico")
serveSingle("/robots.txt", "./robots.txt")
// Normal resources
http.Handle("/static", http.FileServer(http.Dir("./static/")))
http.ListenAndServe(":8080", nil)
}
Please move all other resources (CSS, JS, etc.) to a proper subdirectory, e.g. /static/ .
One thing I thought of that might help you is that you can create your own ServeMux. I added to your example so that chttp is a ServeMux that you can have serve static files. The HomeHandler then checks to see if it should serve a file or not. I just check for a "." but you could do a lot of things. Just an idea, might not be what you are looking for.
package main
import (
"fmt"
"net/http"
"strings"
)
var chttp = http.NewServeMux()
func main() {
chttp.Handle("/", http.FileServer(http.Dir("./")))
http.HandleFunc("/", HomeHandler) // homepage
http.ListenAndServe(":8080", nil)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
if (strings.Contains(r.URL.Path, ".")) {
chttp.ServeHTTP(w, r)
} else {
fmt.Fprintf(w, "HomeHandler")
}
}
Using Gorilla mux package :
r := mux.NewRouter()
//put your regular handlers here
//then comes root handler
r.HandleFunc("/", homePageHandler)
//if a path not found until now, e.g. "/image/tiny.png"
//this will look at "./public/image/tiny.png" at filesystem
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))
http.Handle("/", r)
http.ListenAndServe(":8080", nil)