func main() { err := utils.SetConfig() if err != nil { panic("Could not set base config: \n" + err.Error()) } randPort := getRandPort() router := httprouter.New() cfg := utils.GetConfig() if !cfg.InitialConfig { router.GET("/", showSetConfData) router.GET("/rest/config", getConfig) router.POST("/rest/config/validateVault", validateVaultPath) fileServer := http.FileServer(http.Dir("static")) router.GET("/static/*filepath", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Header().Set("Vary", "Accept-Encoding") w.Header().Set("Cache-Control", "public, max-age=60") w.Header().Set("Access-Control-Allow-Origin", "*") r.URL.Path = p.ByName("filepath") fileServer.ServeHTTP(w, r) }) } else { router.GET("/*filepath", show1PData) } fmt.Printf("http://localhost:%d", randPort) http.ListenAndServe(fmt.Sprintf(":%d", randPort), router) }
func validateVaultPath(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Header().Add("Access-Control-Allow-Origin", "*") body, err := ioutil.ReadAll(r.Body) appData := utils.AppData{} appData.StatusCode = 200 if err != nil { appData.IsError = true appData.StatusCode = 400 appData.Error = "Bad Request Body Received" appData.WriteRestResponse(w) return } defer r.Body.Close() var d utils.Configuration err = json.Unmarshal(body, &d) if err != nil { appData.IsError = true appData.StatusCode = 500 appData.Error = "Invalid Data Encoding" appData.WriteRestResponse(w) return } log.Printf("Vault Location: %s", d.MainLocation) vaultPath := d.MainLocation + "/1Password.html" if _, err := os.Stat(vaultPath); os.IsNotExist(err) { appData.IsError = true appData.StatusCode = 404 appData.Error = "Vault not found at location" appData.WriteRestResponse(w) return } d.InitialConfig = true err = utils.UpdateConfig(d) if err != nil { appData.IsError = true appData.StatusCode = 500 appData.Error = err.Error() appData.WriteRestResponse(w) } utils.SetConfig() appData.Content = "Success" appData.WriteRestResponse(w) return }