Skip to content

Instantly share code, notes, and snippets.

@patientplatypus
Created September 5, 2018 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patientplatypus/36230e665051465cd51291e17825f1f5 to your computer and use it in GitHub Desktop.
Save patientplatypus/36230e665051465cd51291e17825f1f5 to your computer and use it in GitHub Desktop.
My main.go for files question
package main
import (
// standard library
"fmt"
"log"
"net/http"
"encoding/json"
"io/ioutil"
"bytes"
"strings"
// "path/filepath"
// "context"
// "io/ioutil"
// other libraries
"github.com/rs/cors"
"github.com/gorilla/mux"
// "github.com/dgrijalva/jwt-go"
// project files
"github.com/patientplatypus/webserver/authentication"
"github.com/patientplatypus/webserver/database"
"github.com/patientplatypus/webserver/requests"
"github.com/patientplatypus/webserver/utilities"
)
type ExampleResponse struct {
Status string
}
type clientJWT struct {
ClientJWT string `json:"localJWT"`
}
func JWTHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Println("inside JWTHandler")
fmt.Println("value of req.URL.Path: ", req.URL.Path)
if strings.Contains(req.URL.Path, "files"){
// fmt.Println("hello there files")
// next.ServeHTTP(w,req)
}
if req.URL.Path == "/registerUser" || req.URL.Path == "/loginUser" || req.URL.Path == "/cookieLogin"{
fmt.Println("inside JWTHandler handle Auth")
next.ServeHTTP(w,req)
}else if req.URL.Path == "/upload"{
fmt.Println("uploading file don't know how to validate multiform!")
next.ServeHTTP(w,req)
}else if strings.Contains(req.URL.Path, "files"){
// fmt.Println("file should already be served!")
}else{
fmt.Println("inside JWTHandler authing routes")
var cJWT clientJWT
fmt.Println("test line 1")
buf, _ := ioutil.ReadAll(req.Body)
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf))
rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf))
fmt.Println("test line 2")
err := json.NewDecoder(rdr1).Decode(&cJWT)
if err != nil {
fmt.Println("error in decodeing localJWT in JWTHandler")
http.Error(w, err.Error(), 400)
return
}
fmt.Println("test line 3")
req.Body = rdr2 // OK since rdr2 implements the io.ReadCloser interface
fmt.Println("test line 4")
fmt.Println("cJWT.ClientJWT: ")
fmt.Println(cJWT.ClientJWT)
validateJWTChan := make(chan bool)
go auth.ValidateJWT(cJWT.ClientJWT, false, validateJWTChan)
isAuthenticated := <- validateJWTChan
if isAuthenticated == true {
fmt.Println("inside JWTHandler isAuthenticated true")
next.ServeHTTP(w, req)
}else{
fmt.Println("inside JWTHandler isAuthenticated false")
util.HandleRequestResponse(w, "Error", "Not Authenticated")
}
}
})
}
func main() {
data.InitDB()
auth.RotateJWTkey()
fmt.Println("we get past RotateJWTkey function call !!!")
r := mux.NewRouter()
r.HandleFunc("/testJWT", reqt.TestJWT)
r.HandleFunc("/registerUser", auth.RegisterUser)
r.HandleFunc("/loginUser", auth.LoginUser)
r.HandleFunc("/cookieLogin", auth.CookieLogin)
s := r.PathPrefix("/moneySubmit").Subrouter()
s.HandleFunc("/profit", reqt.ProfitSubmit)
s.HandleFunc("/expense/{expenseType}", reqt.ExpenseSubmit)
t := r.PathPrefix("/talk").Subrouter()
t.HandleFunc("/newChannel", reqt.NewChannel)
t.HandleFunc("/getChannels", reqt.GetChannels)
t.HandleFunc("/newSubChannel", reqt.NewSubChannel)
t.HandleFunc("/getSubChannels", reqt.GetSubChannels)
r.HandleFunc("/upload", util.UploadFileHandler())
r.PathPrefix("/files/").Handler(http.StripPrefix("/files/", http.FileServer(http.Dir("/go/src/github.com/patientplatypus/webserver/files/"))))
// fs := http.FileServer(http.Dir("/go/src/github.com/patientplatypus/webserver/files"))
// r.PathPrefix("/files/").Handler(fs)
// r.PathPrefix("/files").Handler(http.FileServer(http.Dir("./files/")))
// r.PathPrefix("/files/").Handler(http.StripPrefix("/files/", http.FileServer(http.Dir("/go/src/github.com/patientplatypus/webserver/files"))))
// r.HandleFunc("/files/", func(w http.ResponseWriter, r *http.Request) {
// fmt.Println("***************")
// fmt.Println(r.URL.Path[1:])
// fmt.Println("***************")
// http.ServeFile(w, r, r.URL.Path[1:])
// })
// r.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir("/go/src/github.com/patientplatypus/webserver/files"))))
// fs := http.FileServer(http.Dir("/go/src/github.com/patientplatypus/webserver/files"))
// http.Handle("/files/", http.StripPrefix("/files", fs))
// http.Handle("/files/", http.StripPrefix("/files", fs))
JWTMux := JWTHandler(r)
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:8080"},
AllowCredentials: true,
})
handler := c.Handler(JWTMux)
log.Fatal(http.ListenAndServe(":8000", handler))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment