Serving favicon.ico with JulienSchmidt httprouter - go

I can get favicon.icon with the standard net/http package but am having trouble with julienschmidt/httprouter. This is what I'm trying and am receiving a 404 error for the favicon.ico file.
import (
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func main(){
router := httprouter.New()
router.GET("/", index)
router.POST("/", login)
router.GET("/logout", logout)
router.GET("/favicon.ico", faviconHandler)
router.ServeFiles("/stuff/*filepath", http.Dir("stuff"))
log.Fatal(http.ListenAndServe(":8080", router))
}
func faviconHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.ServeFile(w, r, "/stuff/images/favicon.ico")
}

I was able to solve the problem by removing the leading slash from stuff/images/favicon.ico. Thanks #Peter.
import (
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func main(){
router := httprouter.New()
router.GET("/", index)
router.POST("/", login)
router.GET("/logout", logout)
router.GET("/favicon.ico", faviconHandler)
router.ServeFiles("/stuff/*filepath", http.Dir("stuff"))
log.Fatal(http.ListenAndServe(":8080", router))
}
func faviconHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.ServeFile(w, r, "stuff/images/favicon.ico")
}

Related

i'am trying to make web application in go, but stuck in connecting html file

Ok so following is my code:
import (
"net/http"
"html/template"
"github.com/gorilla/mux"
)
var templates *template.Template
func main() {
templates = template.Must(template.ParseGlob("templates/*.html"))
r := mux.NewRouter()
r.HandleFunc("/", indexHandler).Methods("GET")
http.Handle("/", r)
http.ListenAndServe(":9000", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecuteTemplate(w, "index.html", nil)
}
and I'm getting this error: panic: html/template: pattern matches no files: index.html
this is sequence of my files:
can any of you can help?
edit:
I hope this will help!

how to create a reverse proxy in golang

I wants to make a reverse proxy in golang using net package from stl library. Used httputil for creating reverse proxy. But when I make request to the proxy server it return 404 error.
Here is the proxy server code
package main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
func main() {
demoUrl , err := url.Parse("http://localhost:1000/run/")
if err!=nil{
log.Fatal(err)
return
}
proxy := httputil.NewSingleHostReverseProxy(demoUrl)
http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(rw, r)
})
http.ListenAndServe(":2000", nil)
}
Here is the origin server code :
package main
import (
"net/http"
)
func main(){
http.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("I am running"))
})
http.ListenAndServe(":1000", nil)
}
Please tell what I am missing here and how to fix the bug! Please
The router doesn't match.
this will be working as expected.
...
func main(){
http.HandleFunc("/run/", func(w http.ResponseWriter, r *http.Request){
w.Write([]byte("I am running"))
})
http.ListenAndServe(":1000", nil)
}
...

Goji SubRouter returns 404

Here is some code
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
)
type handler struct{}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
subMux := web.New()
subMux.Use(middleware.SubRouter)
subMux.Post("/:id", func(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
})
subMux.ServeHTTP(w, r)
}
func main() {
goji.Handle("/inner/*", handler{})
goji.Serve()
}
The main idea around this to encapsulate handler routes and use standart net/http Handler interface. So why the following code produce 404 and not use subrouter ?
curl -X POST http://localhost:8000/inner/5
404 page not found
If you change it like this, you can get post data.
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
"github.com/zenazn/goji/web/middleware"
)
type handler struct{}
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "OK")
}
func main() {
subMux := web.New()
subMux.Use(middleware.SubRouter)
subMux.Post("/:id", handler{})
goji.Handle("/inner/*", subMux)
goji.Serve()
}

How to add body title to Fprintf output?

I'm trying to add <title>Go</title> to my code example:
package main
import (
"fmt"
"net/http"
"os"
)
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "\n\nSystem info:\nHostname [pod name]: %s", hostname)
fmt.Fprintf(w, "\nCurrent URL: %s\n", r.Host)
}
func main() {
http.HandleFunc("/favicon.ico", favicon)
http.HandleFunc("/", sayhelloName)
http.ListenAndServe(":80", nil)
}
I tried to add like:
fmt.Fprintf(w, "<title>Go</title>"). It works but make mess with strings next to. I wouldn't like to use html template only to add title to page. Is there any ways to add title in one string?
not used html template. just used only fmt.Fprintf.
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
func sayhelloName(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "<html>")
fmt.Fprintf(w, "<head>")
fmt.Fprintf(w, "<title>%s</title>", "Go")
fmt.Fprintf(w, "</head>")
fmt.Fprintf(w, "<body>")
fmt.Fprintf(w, "<h1>System info: Hostname [pod name]: %s", hostname)
fmt.Fprintf(w, "<h1>Current URL: %s", r.Host)
fmt.Fprintf(w, "</body>")
fmt.Fprintf(w, "</html>")
}
func main() {
http.HandleFunc("/favicon.ico", favicon)
http.HandleFunc("/", sayhelloName)
log.Fatal(http.ListenAndServe(":8080", nil))
}

gorilla mux router handlers

I can not get the gorilla mux to work..
When requesting http://www.localhost:9000 this is returned by the web server 404 page not found
But this works http://localhost:9000/ and prints Hello world
package main
import (
"net/http"
"fmt"
"log"
"github.com/gorilla/mux"
)
func Handler(w http.ResponseWriter, r *http.Request){
fmt.Fprint(w, "Hello world")
}
func main(){
r := mux.NewRouter()
r.Host("www.localhost")
r.HandleFunc("/", Handler)
err := http.ListenAndServe(":9000", r)
if err != nil {
log.Fatal("ListenAndServe error: ", err)
}
}
You want to be able to support both localhost and www.localhost
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func Handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello world")
}
func main() {
r := mux.NewRouter()
r.Host("www.localhost").Path("/").HandlerFunc(Handler)
r.HandleFunc("/", Handler)
err := http.ListenAndServe(":9000", r)
if err != nil {
log.Fatal("ListenAndServe error: ", err)
}
}
If you read the documentation carefully, you'll notice that r.Host() is just another pattern matching function. It doesn't set any global rule for that router.
if you want to make that rule to be inherited you'll need to use a subrouter:
subrouter := r.Host("www.localhost").Subrouter()
then you use "subrouter" in place of "r"

Resources