Related
I am looking at this type
type GzipResponseWriter struct {
gw *gzip.Writer
http.ResponseWriter
}
And functions that will implement it
func (w GzipResponseWriter) Write(b []byte) (int, error) {
if _, ok := w.Header()["Content-Type"]; !ok {
// If content type is not set, infer it from the uncompressed body.
w.Header().Set("Content-Type", http.DetectContentType(b))
}
return w.gw.Write(b)
}
func (w GzipResponseWriter) Flush() {
w.gw.Flush()
if fw, ok := w.ResponseWriter.(http.Flusher); ok {
fw.Flush()
}
}
Does the http.ResponseWriter relate to the second field?
Why not
gw1 http.ResponseWriter?
I know there are some questions and posts/articles regarding this question, but from my newbie view, not exactly.
The thing is, I've got a main program listening to a port and redirecting the calls to a specific handler. The typical structure:
func main() {
http.HandleFunc("/something", specificHandler)
http.ListenAndServe(":8080", nil)
}
With the handler being something like:
func specificHandler(w http.ResponseWriter, r *http.Request) {
somepackage.foo()
}
Then somepackage, which contains the function foo, has some global variables, basically because they're needed for functions to share (e.g., when using a priority queue implemented with a container/heap, which will get the priorities in the Swap function from a global matrix of distances which is of course changable). And many other examples. In summary, global variables...
The problem is, as you might see, that those variables are shared among all the calls to the handler. And that's bad.
How can I actually solve this? There must be an easy to way to do it that I haven't got to yet, because it looks like something so usual...
Thanks in advance.
EDIT
To make it clearer. For example, in my A* package, I've got the following global variables:
var openVerticesAS PriorityQueueAStar
// which vertices are closed
var closedVertices map[int]bool
// which vertices are currently open
var openVertices map[int]bool
// cost from start to node
var gScore map[int]float64
// cost from start to end, passing by node i (g+h)
var fScore map[int]float64
Then, PriorityQueueAStar is implemented as follows:
type PriorityQueueAStar []int // rel id
func (pq PriorityQueueAStar) Len() int { return len(pq) }
func (pq PriorityQueueAStar) Empty() bool { return len(pq) == 0 }
func (pq PriorityQueueAStar) Less(i, j int) bool {
return fScore[pq[i]] < fScore[pq[j]]
}
func (pq PriorityQueueAStar) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueueAStar) Push(x interface{}) {
*pq = append(*pq, x.(int))
}
func (pq *PriorityQueueAStar) Pop() interface{} {
old := *pq
n := len(old)
rel := old[n-1]
*pq = old[0 : n-1]
return rel
}
func (pq PriorityQueueAStar) Top() interface{} {
return pq[0]
}
The question then, is, how do I keep doing this without having all those maps as global variables? If they are part of the struct, how do I access the struct from the priority queue functions?
When your handler needs a variable, usually that means you should implement the Handler interface instead of providing a HandlerFunc function.
Here is a BAD example (using global variables):
var globalThing string
func specificHandler(w http.ResponseWriter, r *http.Request) {
w.Write(globalConfigThing)
}
func main() {
globalThing = "Hello world!"
http.HandleFunc("/something", specificHandler)
http.ListenAndServe(":8080", nil)
}
Here is a BETTER example (not using global variables):
type specificHandler struct {
Thing string
}
func (h *specificHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(h.Thing)
}
func main() {
http.Handle("/something", &specificHandler{Thing: "Hello world!"})
http.ListenAndServe(":8080", nil)
}
As you can see, a Handler can encapsulate variables.
For completeness, the other approach is to use a function closure. This works well for once-off handlers but is not re-usable and is harder to write unit tests for.
func main() {
scopedThing := "Hello world!"
http.HandleFunc("/something", func (w http.ResponseWriter, r *http.Request) {
w.Write(scopedThing)
})
http.ListenAndServe(":8080", nil)
}
Done correctly, you can now avoid global variables in your package somepackage by passing them as arguments, etc.
EDIT: For example, you can define your handler struct with several PriorityQueueAStar fields from the somepackage package:
type specificHandler struct {
QueueA somepackage.PriorityQueueAStar
QueueB somepackage.PriorityQueueAStar
QueueC somepackage.PriorityQueueAStar
}
func (h *specificHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.QueueA.Push(h.QueueB.Pop)
h.QueueB.Push(h.QueueC.Pop)
w.Write([]byte("Queues pushed and popped"))
}
The closure method was mentioned by chowey, but had a caveat that it isn't testable or reusable. It is actually testable and reusable if you have a function to return the closure. For some types of data this may make for a cleaner abstraction and implementation:
func handleThing(thing string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Write(thing)
}
}
func main() {
http.HandleFunc("/something", handleThing("Hello world!"))
http.ListenAndServe(":8080", nil)
}
I'm attempting write a test for my package and failing at comparing funcs. Here's essentially what i'm doing.
package main
import (
"fmt"
"reflect"
)
type HandlerFunc func(cmd interface{})
type Bus struct {
handlers map[reflect.Type]HandlerFunc
}
func (bus *Bus) RegisterHandler(cmd interface{}, handler HandlerFunc) {
bus.handlers[reflect.TypeOf(cmd)] = handler
}
func (bus *Bus) GetHandler(cmd interface{}) HandlerFunc {
t := reflect.TypeOf(cmd)
for kind, handler := range bus.handlers {
if t == kind {
return handler
}
}
return nil
}
func New() *Bus {
return &Bus{
handlers: make(map[reflect.Type]HandlerFunc),
}
}
type RegisterUserCommand struct {}
func main() {
bus := New()
handler := func (cmd interface{}) {}
bus.RegisterHandler(&RegisterUserCommand{}, handler)
retrieved := bus.GetHandler(&RegisterUserCommand{})
if retrieved != handler {
fmt.Println("Not the same!")
return
}
fmt.Println("Same!")
}
Comparing retrieved with handler causes the following error
invalid operation: (func(interface {}))(retrieved) != handler (func can only be compared to nil)
How can i properly test the function i'm retrieving is the same one added previously?
Given that you can't compare functions, you can write your test in a different way. You can make handler set a boolean value in your test and check that you've got the right function by calling it and seeing if the boolean changes.
Here's an example:
func main() {
bus := New()
called := false
handler := func (cmd interface{}) { called = true }
bus.RegisterHandler(&RegisterUserCommand{}, handler)
bus.GetHandler(&RegisterUserCommand{})(nil)
if called {
fmt.Println("We got the right handler!")
return
}
fmt.Println("We didn't get the right handler")
}
package main
import (
"fmt"
"github.com/ant0ine/go-json-rest"
"net/http"
)
type App struct {
Id string
Name string
}
func GetApp(w *rest.ResponseWriter, req *rest.Request) {
user := App{
Id: req.PathParam("id"),
Name: "Antoine",
}
w.WriteJson(&user)
}
type MyResourceHandler struct {
rest.ResourceHandler
}
type ResourceController interface {
Show(w *rest.ResponseWriter, req *rest.Request)
Create(w *rest.ResponseWriter, req *rest.Request)
Update(w *rest.ResponseWriter, req *rest.Request)
Delete(w *rest.ResponseWriter, req *rest.Request)
}
func (self *MyResourceHandler) AddResource(name string, c ResourceController) error {
show_func := func(w *rest.ResponseWriter, r *rest.Request) {
c.Show(w, r)
}
create_func := func(w *rest.ResponseWriter, r *rest.Request) {
c.Create(w, r)
}
update_func := func(w *rest.ResponseWriter, r *rest.Request) {
c.Update(w, r)
}
delete_func := func(w *rest.ResponseWriter, r *rest.Request) {
c.Delete(w, r)
}
err := self.ResourceHandler.SetRoutes(
rest.Route{"GET", fmt.Sprintf("/%s/:id", name), show_func},
rest.Route{"POST", fmt.Sprintf("/%s", name), create_func},
rest.Route{"PUT", fmt.Sprintf("/%s/:id", name), update_func},
rest.Route{"DELETE", fmt.Sprintf("/%s/:id", name), delete_func},
)
return err
}
type AppController struct{}
func (self *AppController) Show(w *rest.ResponseWriter, r *rest.Request) {
app := App{
Id: r.PathParam("id"),
Name: "Antoine",
}
w.WriteJson(&app)
}
func (self *AppController) Create(w *rest.ResponseWriter, r *rest.Request) {
app := App{
Id: r.PathParam("id"),
Name: "Antoine",
}
w.WriteJson(&app)
}
func (self *AppController) Update(w *rest.ResponseWriter, r *rest.Request) {
app := App{
Id: r.PathParam("id"),
Name: "Antoine",
}
w.WriteJson(&app)
}
func (self *AppController) Delete(w *rest.ResponseWriter, r *rest.Request) {
app := App{
Id: r.PathParam("id"),
Name: "Antoine",
}
w.WriteJson(&app)
}
func main() {
handler := MyResourceHandler{}
controler := AppController{}
handler.AddResource("app", controler)
http.ListenAndServe(":9008", &handler)
}
I have implemented all of four methods of interface ResourceController, but it tells me:
./fakeapi.go:93: cannot use controler (type AppController) as type ResourceController in function argument:
AppController does not implement ResourceController (Create method requires pointer receiver)
It's in the error message: (Create method requires pointer receiver)
You've defined Create on *AppController (pointer to an AppController), but you're trying to use an AppController, which does not satisfy the interface.
The solution you probably want is to change controler := AppController{} to controler := &AppController{}
As cthom06 says, the answer is in the error.
The reason for the error message is that you have only implemented ResourceController when you are passing a pointer and not a value.
When you implemented Show, Create, etc., you used
func (self *AppController)...
This requires the method to get a pointer receiver, but AddResources is given the value of controler, not the pointer.
Here is some example code to show the problem:
package main
import "fmt"
type I interface {
Show(i int)
}
type T struct{}
func (t *T) Show(i int) {
fmt.Println(i)
}
func CallShow(i I) {
i.Show(32);
}
func main() {
obj := T{}
// CallShow(obj) // This doesn't work unless you change func (t *T) to func (t T)
CallShow(&obj) // This works because you pass a pointer
}
Also "ResourceHandler" is deprecated. I would recommend to use the v3 API:
api := rest.NewApi()
api.Use(rest.DefaultDevStack...)
router, err := rest.MakeRouter(
// your routes here ...
)
if err != nil {
log.Fatal(err)
}
api.SetApp(router)
log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
Antoine - go-json-rest author
Assuming that we have:
http.HandleFunc("/smth", smthPage)
http.HandleFunc("/", homePage)
User sees a plain "404 page not found" when they try a wrong URL. How can I return a custom page for that case?
Update concerning gorilla/mux
Accepted answer is ok for those using pure net/http package.
If you use gorilla/mux you should use something like this:
func main() {
r := mux.NewRouter()
r.NotFoundHandler = http.HandlerFunc(notFound)
}
And implement func notFound(w http.ResponseWriter, r *http.Request) as you want.
I usually do this:
package main
import (
"fmt"
"net/http"
)
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/smth/", smthHandler)
http.ListenAndServe(":12345", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
errorHandler(w, r, http.StatusNotFound)
return
}
fmt.Fprint(w, "welcome home")
}
func smthHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/smth/" {
errorHandler(w, r, http.StatusNotFound)
return
}
fmt.Fprint(w, "welcome smth")
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
w.WriteHeader(status)
if status == http.StatusNotFound {
fmt.Fprint(w, "custom 404")
}
}
Here I've simplified the code to only show custom 404, but I actually do more with this setup: I handle all the HTTP errors with errorHandler, in which I log useful information and send email to myself.
Following is the approach I choose. It is based on a code snippet which I cannot acknowledge since I lost the browser bookmark.
Sample code : (I put it in my main package)
type hijack404 struct {
http.ResponseWriter
R *http.Request
Handle404 func (w http.ResponseWriter, r *http.Request) bool
}
func (h *hijack404) WriteHeader(code int) {
if 404 == code && h.Handle404(h.ResponseWriter, h.R) {
panic(h)
}
h.ResponseWriter.WriteHeader(code)
}
func Handle404(handler http.Handler, handle404 func (w http.ResponseWriter, r *http.Request) bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
hijack := &hijack404{ ResponseWriter:w, R: r, Handle404: handle404 }
defer func() {
if p:=recover(); p!=nil {
if p==hijack {
return
}
panic(p)
}
}()
handler.ServeHTTP(hijack, r)
})
}
func fire404(res http.ResponseWriter, req *http.Request) bool{
fmt.Fprintf(res, "File not found. Please check to see if your URL is correct.");
return true;
}
func main(){
handler_statics := http.StripPrefix("/static/", http.FileServer(http.Dir("/Path_To_My_Static_Files")));
var v_blessed_handler_statics http.Handler = Handle404(handler_statics, fire404);
http.Handle("/static/", v_blessed_handler_statics);
// add other handlers using http.Handle() as necessary
if err := http.ListenAndServe(":8080", nil); err != nil{
log.Fatal("ListenAndServe: ", err);
}
}
Please customize the func fire404 to output your own version of message for error 404.
If you happen to be using Gorilla Mux, you may wish to replace the main function with below :
func main(){
handler_statics := http.StripPrefix("/static/", http.FileServer(http.Dir("/Path_To_My_Static_Files")));
var v_blessed_handler_statics http.Handler = Handle404(handler_statics, fire404);
r := mux.NewRouter();
r.PathPrefix("/static/").Handler(v_blessed_handler_statics);
// add other handlers with r.HandleFunc() if necessary...
http.Handle("/", r);
log.Fatal(http.ListenAndServe(":8080", nil));
}
Please kindly correct the code if it is wrong, since I am only a newbie to Go. Thanks.
Ancient thread, but I just made something to intercept http.ResponseWriter, might be relevant here.
package main
//GAE POC originally inspired by https://thornelabs.net/2017/03/08/use-google-app-engine-and-golang-to-host-a-static-website-with-same-domain-redirects.html
import (
"net/http"
)
func init() {
http.HandleFunc("/", handler)
}
// HeaderWriter is a wrapper around http.ResponseWriter which manipulates headers/content based on upstream response
type HeaderWriter struct {
original http.ResponseWriter
done bool
}
func (hw *HeaderWriter) Header() http.Header {
return hw.original.Header()
}
func (hw *HeaderWriter) Write(b []byte) (int, error) {
if hw.done {
//Silently let caller think they are succeeding in sending their boring 404...
return len(b), nil
}
return hw.original.Write(b)
}
func (hw *HeaderWriter) WriteHeader(s int) {
if hw.done {
//Hmm... I don't think this is needed...
return
}
if s < 400 {
//Set CC header when status is < 400...
//TODO: Use diff header if static extensions
hw.original.Header().Set("Cache-Control", "max-age=60, s-maxage=2592000, public")
}
hw.original.WriteHeader(s)
if s == 404 {
hw.done = true
hw.original.Write([]byte("This be custom 404..."))
}
}
func handler(w http.ResponseWriter, r *http.Request) {
urls := map[string]string{
"/example-post-1.html": "https://example.com/post/example-post-1.html",
"/example-post-2.html": "https://example.com/post/example-post-2.html",
"/example-post-3.html": "https://example.com/post/example-post-3.html",
}
w.Header().Set("Strict-Transport-Security", "max-age=15768000")
//TODO: Put own logic
if value, ok := urls[r.URL.Path]; ok {
http.Redirect(&HeaderWriter{original: w}, r, value, 301)
} else {
http.ServeFile(&HeaderWriter{original: w}, r, "static/"+r.URL.Path)
}
}
i think the clean way is this:
func main() {
http.HandleFunc("/calculator", calculatorHandler)
http.HandleFunc("/history", historyHandler)
http.HandleFunc("/", notFoundHandler)
log.Fatal(http.ListenAndServe(":80", nil))
}
if the address is not /calulator or /history, then it handles notFoundHandler function.
Maybe I'm wrong, but I just checked the sources: http://golang.org/src/pkg/net/http/server.go
It seems like specifying custom NotFound() function is hardly possible: NotFoundHandler() returns a hardcoded function called NotFound().
Probably, you should submit an issue on this.
As a workaround, you can use your "/" handler, which is a fallback if no other handlers were found (as it is the shortest one). So, check is page exists in that handler and return a custom 404 error.
You just need to create your own notFound handler and register it with HandleFunc for the path that you don't handle.
If you want the most control over your routing logic you will need to use a custom server and custom handler type of your own.
http://golang.org/pkg/net/http/#Handler
http://golang.org/pkg/net/http/#Server
This allows you to implement more complex routing logic than the HandleFunc will allow you to do.
you can define
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/" {
writer.WriteHeader(404)
writer.Write([]byte(`not found, da xiong dei !!!`))
return
}
})
when access not found resource, it will execute to http.HandleFunc("/", xxx)
You can simply use something like:
func Handle404(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "404 error\n")
}
func main(){
http.HandleFunc("/", routes.Handle404)
}
If you need to get the standard one, just write:
func main(){
http.HandleFunc("/", http.NotFound)
}
And you'll get:
404 page not found