-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
40 lines (36 loc) · 1014 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 图片服务器
*/
package main
import (
"log"
"net/http"
"runtime/debug"
"github.com/wolanx/go-fs/src/config"
"github.com/wolanx/go-fs/src/handle"
)
func main() {
mux := http.NewServeMux()
handle.StaticHandler(mux, "/assets/", "./assets", 0)
mux.HandleFunc("/list", SafeHandler(handle.ListHandler))
mux.HandleFunc("/info", SafeHandler(handle.InfoHandler))
mux.HandleFunc("/demo", SafeHandler(handle.DemoHandler))
mux.HandleFunc("/upload", SafeHandler(handle.UploadHandler))
mux.HandleFunc("/", SafeHandler(handle.IndexHandler))
err := http.ListenAndServe(":"+config.Port, mux)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}
func SafeHandler(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e, ok := recover().(error); ok {
http.Error(w, e.Error(), http.StatusInternalServerError)
log.Println("Warn:panic in %v. - %v", fn, e)
log.Println(string(debug.Stack()))
}
}()
fn(w, r)
}
}