Skip to content

Instantly share code, notes, and snippets.

@toravir
Created April 3, 2019 15:04
Show Gist options
  • Save toravir/8c9bab5756f33963fa745d977d85f94e to your computer and use it in GitHub Desktop.
Save toravir/8c9bab5756f33963fa745d977d85f94e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/gorilla/mux"
"net"
"net/http"
)
func returnFj(w http.ResponseWriter, r *http.Request) {
fj := `{"VERSION":1.0, "ep-db" : "", "interfaces" : "" }`
w.WriteHeader(201)
if _, e := w.Write([]byte(fj)); e != nil {
panic(e)
}
}
type statusRecorder struct {
http.ResponseWriter
status int
}
func (rec *statusRecorder) WriteHeader(code int) {
rec.status = code
}
func Middleware(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rec := statusRecorder{w, 0}
h.ServeHTTP(&rec, r)
if rec.status == http.StatusMovedPermanently {
fmt.Println("Fixing 301 -> 308")
w.WriteHeader(http.StatusPermanentRedirect)
}
})
}
func main() {
rtr := mux.NewRouter()
rtr.Handle("/cc/fj", http.HandlerFunc(returnFj)).Methods("POST")
l, _ := net.Listen("tcp", "localhost:11225")
http.Serve(l, Middleware(rtr))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment