一旦提供了一次内容,有没有办法从服务器中删除整个静态目录?(服务是指在浏览器上显示一次)。
func main() {
fs := http.FileServer(http.Dir(tempDir))
http.Handle("/", fs)
http.HandleFunc("/app/wo", workOrderApp)
log.Fatal(http.ListenAndServe(":"+os.Args[1], nil))
}
func workOrderApp(w http.ResponseWriter, r *http.Request) {
workOrderAppProcess(w)
time.Sleep(time.Duration(4 * time.Second)) //some time to let render the html
os.RemoveAll(tempDir)
}
这sleep os.RemoveAll是一个打击和错过。必须将睡眠时间调整为几秒钟,否则有时会提供文件,有时不提供文件,我相信是因为带宽或网络相关的东西。但它也有延迟整个页面渲染的副作用。
在此示例中,我删除了所有目录,这正是我想要的。
func workOrderAppProcess(aid, date, language, token string, w http.ResponseWriter) {
zipDir := os.Args[2]
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
log.Printf("Creating directory: %v", tempDir)
err := os.MkdirAll(tempDir, 0777)
if err != nil {
log.Print(err.Error())
}
}
log.Printf("Extracting file: %v to: %v", date+".zip", tempDir)
zipPath, _ := filepath.Abs(zipDir + "/" + date + ".zip")
app.ExtractZip(zipPath, tempDir)
batch := app.ReturnBatchNumber(tempDir + date)
typesData := app.ReturnWorkTypeData(app.ParseXML(tempDir + date + "/" + batch + "_type_list.xml"))
record := app.FindAppointmentRecord(aid, app.ParseXML(tempDir+date+"/"+batch+"_appt.xml"))
signatureFileURL := app.ReturnSignatureFileURL(tempDir+date, aid, date)
app.RenderTemplate(record, typesData, "template/wo.html", language, "/"+signatureFileURL, w)
}
倚天杖
相关分类