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 getConfig(w http.ResponseWriter, r *http.Request, p httprouter.Params) { w.Header().Add("Access-Control-Allow-Origin", "*") w.Header().Add("Content-Type", "application/json") w.Header().Add("Access-Control-Allow-Origin", "*") cfg := utils.GetConfig() d, err := json.Marshal(cfg) if err != nil { w.WriteHeader(500) w.Write([]byte(err.Error())) return } w.Write(d) return }
func show1PData(w http.ResponseWriter, r *http.Request, p httprouter.Params) { cfg := utils.GetConfig() dfs := http.FileServer(http.Dir(cfg.MainLocation)) fp := p.ByName("filepath") if fp == "" { fp = "/" } if fp != "/" { w.Header().Set("Vary", "Accept-Encoding") w.Header().Set("Cache-Control", "public, max-age=60") w.Header().Set("Access-Control-Allow-Origin", "*") dfs.ServeHTTP(w, r) return } f, err := ioutil.ReadFile(filepath.ToSlash(cfg.MainLocation + "/1Password.html")) if err != nil { log.Printf("Error reading 1password file: %s", err.Error()) fmt.Fprint(w, utils.HTMLErr) return } fmt.Fprint(w, string(f)) return }