Skip to content

Instantly share code, notes, and snippets.

@aavrug
Created July 12, 2020 18:21
Show Gist options
  • Save aavrug/8da135bf16cb78b943577a6fa7d86493 to your computer and use it in GitHub Desktop.
Save aavrug/8da135bf16cb78b943577a6fa7d86493 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// isAuthorized checks authorization and returns response
func isAuthorized(endpoint func(http.ResponseWriter, *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header["Cookie"] != nil {
token := r.Header["Cookie"][0]
if token != "" {
endpoint(w, r)
// More logic will be here
} else {
JSONError(w, "You are not authorized!", 401)
}
} else {
fmt.Fprintf(w, "Not Authorized")
}
})
}
// validateDataType checks authorization and returns response
func validateDataType(endpoint func(http.ResponseWriter, *http.Request)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header["Cookie"] != nil {
token := r.Header["Cookie"][0]
if token != "" {
fmt.Println(token)
} else {
JSONError(w, "You are not authorized!", 401)
}
} else {
fmt.Fprintf(w, "Not Authorized")
}
})
}
// JSONError returns error copde
func JSONError(w http.ResponseWriter, err interface{}, code int) {
log.Printf("Status code: %v Error message: %v", code, err)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(code)
json.NewEncoder(w).Encode(err)
}
func authRequestHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Println("This is a get request")
} else if r.Method == http.MethodPost {
fmt.Println("This is a post request")
} else if r.Method == http.MethodPut {
fmt.Println("This is a " + r.Method + " request")
} else if r.Method == http.MethodDelete {
fmt.Println("Delete request")
}
rBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", rBody)
fmt.Fprintf(w, "Welcome to the HomePage!")
fmt.Println("Endpoint Hit: homePage")
}
func todoRequestHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
sendRequest(w, r)
// fmt.Println("This is a get request")
} else if r.Method == http.MethodPost {
fmt.Println("This is a post request")
} else if r.Method == http.MethodPut {
fmt.Println("This is a " + r.Method + " request")
} else if r.Method == http.MethodDelete {
fmt.Println("Delete request")
}
rBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", rBody)
fmt.Fprintf(w, "Welcome to the HomePage!")
fmt.Println("Endpoint Hit: homePage")
}
func sendRequest(w http.ResponseWriter, r *http.Request) {
url := r.URL
url.Host = "example.com"
proxyReq, err := http.NewRequest(r.Method, "https:"+url.String(), r.Body)
if err != nil {
fmt.Println(err)
}
proxyReq.Header.Set("Host", r.Host)
proxyReq.Header.Set("X-Forwarded-For", r.RemoteAddr)
for header, values := range r.Header {
for _, value := range values {
proxyReq.Header.Add(header, value)
}
}
client := &http.Client{}
proxyRes, err := client.Do(proxyReq)
fmt.Println(proxyRes)
if err != nil {
fmt.Println(err)
}
fmt.Println(proxyRes)
}
func validateRequest(w http.ResponseWriter, r *http.Request) {
JSONError(w, "Invalid request!", 400)
}
func handleRequests() {
http.HandleFunc("/", validateRequest)
http.Handle("/auth", isAuthorized(authRequestHandler))
http.Handle("/todo", isAuthorized(todoRequestHandler))
log.Fatal(http.ListenAndServe(":3001", nil))
}
func main() {
handleRequests()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment