Created
April 3, 2019 15:04
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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