| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func handler(w http.ResponseWriter, r *http.Request) { |
| 11 | if r.URL.Path != "/" && !strings.HasSuffix(r.URL.Path, ".svg") { |
| 12 | http.NotFound(w, r) |
| 13 | return |
| 14 | } |
| 15 | |
| 16 | logf("----------------------") |
| 17 | logf(" => handling request: %v", r.URL) |
| 18 | logf("----------------------") |
| 19 | |
| 20 | |
| 21 | Analysis.OptsSetup() |
| 22 | |
| 23 | |
| 24 | Analysis.OverrideByHTTP(r) |
| 25 | |
| 26 | var img string |
| 27 | if img = Analysis.FindCachedImg(); img != "" { |
| 28 | log.Println("serving file:", img) |
| 29 | http.ServeFile(w, r, img) |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | |
| 34 | if e := Analysis.ProcessListArgs(); e != nil { |
| 35 | http.Error(w, "invalid parameters", http.StatusBadRequest) |
| 36 | return |
| 37 | } |
| 38 | |
| 39 | output, err := Analysis.Render() |
| 40 | if err != nil { |
| 41 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | if r.Form.Get("format") == "dot" { |
| 46 | log.Println("writing dot output..") |
| 47 | fmt.Fprint(w, string(output)) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | log.Printf("converting dot to %s..\n", *outputFormat) |
| 52 | |
| 53 | img, err = dotToImage("", *outputFormat, output) |
| 54 | if err != nil { |
| 55 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | err = Analysis.CacheImg(img) |
| 60 | if err != nil { |
| 61 | http.Error(w, "cache img error: "+err.Error(), http.StatusBadRequest) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | log.Println("serving file:", img) |
| 66 | http.ServeFile(w, r, img) |
| 67 | } |
| 68 | |
| 69 | |