forked from yinqiwen/go-didagle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (95 loc) · 2.37 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"github.com/yinqiwen/go-didagle"
)
type Page struct {
Title string
Body []byte
}
func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, _ := template.ParseFiles(tmpl + ".html")
t.Execute(w, p)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/view/"):]
p, _ := loadPage(title)
renderTemplate(w, "view", p)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[len("/edit/"):]
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, "edit", p)
}
type WebRes struct {
Path string `json:",omitempty"`
Err string `json:",omitempty"`
}
func main() {
log.Printf("Start web server")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "edit.html")
})
http.Handle("/pngs/", http.StripPrefix("/pngs/", http.FileServer(http.Dir("./pngs"))))
var cursor int64
http.HandleFunc("/gen_png", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
ops := r.FormValue("ops")
script := r.FormValue("script")
// log.Printf("Receive form ops:%s", ops)
// log.Printf("Receive form script:%s", script)
dag, err := didagle.NewDAGConfigByContent(ops, script)
rs := &WebRes{}
if nil != err {
rs.Err = fmt.Sprintf("%v", err)
b, _ := json.Marshal(rs)
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
path := fmt.Sprintf("/pngs/%d", cursor)
err = dag.GenPng("." + path)
if nil != err {
log.Printf("Error:%v", err)
rs.Err = fmt.Sprintf("%v", err)
b, _ := json.Marshal(rs)
w.WriteHeader(400)
w.Header().Set("Content-Type", "application/json")
w.Write(b)
return
}
png := path + ".png"
rs.Path = png
//w.WriteHeader(200)
b, _ := json.Marshal(rs)
w.Header().Set("Content-Type", "application/json")
w.Write(b)
log.Printf("Response:%v", string(b))
cursor++
if 100 == cursor {
cursor = 0
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}