Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: experimental/webtry/webtry.go

Issue 235373002: Add design for sql storage, implementation of db that stores the data, not retrieving yet. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remove exe Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « experimental/webtry/setup/continue_install ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 package main 1 package main
2 2
3 import ( 3 import (
4 "bytes" 4 "bytes"
5 "crypto/md5" 5 "crypto/md5"
6 "database/sql"
6 "encoding/base64" 7 "encoding/base64"
7 "encoding/json" 8 "encoding/json"
8 "flag" 9 "flag"
9 "fmt" 10 "fmt"
11 _ "github.com/go-sql-driver/mysql"
10 "io/ioutil" 12 "io/ioutil"
11 "log" 13 "log"
12 "net/http" 14 "net/http"
13 "os" 15 "os"
14 "os/exec" 16 "os/exec"
15 "path/filepath" 17 "path/filepath"
16 "strings" 18 "strings"
17 "text/template" 19 "text/template"
18 ) 20 )
19 21
20 const ( 22 const (
21 RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR _TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -D SK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_C AN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_ SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I ../../tools/flags -I../../include/config -I../../include/core -I../../include/pa thops -I../../include/pipe -I../../include/effects -I../../include/ports -I../.. /src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno -exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unu sed-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o` 23 RESULT_COMPILE = `c++ -DSK_GAMMA_SRGB -DSK_GAMMA_APPLY_TO_A8 -DSK_SCALAR _TO_FLOAT_EXCLUDED -DSK_ALLOW_STATIC_GLOBAL_INITIALIZERS=1 -DSK_SUPPORT_GPU=0 -D SK_SUPPORT_OPENCL=0 -DSK_FORCE_DISTANCEFIELD_FONTS=0 -DSK_SCALAR_IS_FLOAT -DSK_C AN_USE_FLOAT -DSK_SAMPLES_FOR_X -DSK_BUILD_FOR_UNIX -DSK_USE_POSIX_THREADS -DSK_ SYSTEM_ZLIB=1 -DSK_DEBUG -DSK_DEVELOPER=1 -I../../src/core -I../../src/images -I ../../tools/flags -I../../include/config -I../../include/core -I../../include/pa thops -I../../include/pipe -I../../include/effects -I../../include/ports -I../.. /src/sfnt -I../../include/utils -I../../src/utils -I../../include/images -g -fno -exceptions -fstrict-aliasing -Wall -Wextra -Winit-self -Wpointer-arith -Wno-unu sed-parameter -Wno-c++11-extensions -Werror -m64 -fno-rtti -Wnon-virtual-dtor -c ../../../cache/%s.cpp -o ../../../cache/%s.o`
22 LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start -group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflag s.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/ libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc. a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a li bskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontcon fig -ldl -lfreetype` 24 LINK = `c++ -m64 -lstdc++ -lm -o ../../../inout/%s -Wl,--start -group ../../../cache/%s.o obj/experimental/webtry/webtry.main.o obj/gyp/libflag s.a libskia_images.a libskia_core.a libskia_effects.a obj/gyp/libjpeg.a obj/gyp/ libwebp_dec.a obj/gyp/libwebp_demux.a obj/gyp/libwebp_dsp.a obj/gyp/libwebp_enc. a obj/gyp/libwebp_utils.a libskia_utils.a libskia_opts.a libskia_opts_ssse3.a li bskia_ports.a libskia_sfnt.a -Wl,--end-group -lpng -lz -lgif -lpthread -lfontcon fig -ldl -lfreetype`
23 ) 25 )
24 26
25 var ( 27 var (
26 // codeTemplate is the cpp code template the user's code is copied into. 28 // codeTemplate is the cpp code template the user's code is copied into.
27 codeTemplate *template.Template = nil 29 codeTemplate *template.Template = nil
28 30
29 // index is the main index.html page we serve. 31 // index is the main index.html page we serve.
30 index []byte 32 index []byte
33
34 // db is the database, nil if we don't have an SQL database to store dat a into.
35 db *sql.DB = nil
31 ) 36 )
32 37
33 // flags 38 // flags
34 var ( 39 var (
35 useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.") 40 useChroot = flag.Bool("use_chroot", false, "Run the compiled code in the schroot jail.")
41 port = flag.String("port", ":8000", "HTTP service address (e.g., ': 8000')")
36 ) 42 )
37 43
38 // lineNumbers adds #line numbering to the user's code. 44 // lineNumbers adds #line numbering to the user's code.
39 func LineNumbers(c string) string { 45 func LineNumbers(c string) string {
40 lines := strings.Split(c, "\n") 46 lines := strings.Split(c, "\n")
41 ret := []string{} 47 ret := []string{}
42 for i, line := range lines { 48 for i, line := range lines {
43 ret = append(ret, fmt.Sprintf("#line %d", i+1)) 49 ret = append(ret, fmt.Sprintf("#line %d", i+1))
44 ret = append(ret, line) 50 ret = append(ret, line)
45 } 51 }
(...skipping 10 matching lines...) Expand all
56 os.Chdir(cwd) 62 os.Chdir(cwd)
57 63
58 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te mplate.cpp")) 64 codeTemplate, err = template.ParseFiles(filepath.Join(cwd, "templates/te mplate.cpp"))
59 if err != nil { 65 if err != nil {
60 panic(err) 66 panic(err)
61 } 67 }
62 index, err = ioutil.ReadFile(filepath.Join(cwd, "templates/index.html")) 68 index, err = ioutil.ReadFile(filepath.Join(cwd, "templates/index.html"))
63 if err != nil { 69 if err != nil {
64 panic(err) 70 panic(err)
65 } 71 }
72
73 // Connect to MySQL server. First, get the password from the metadata se rver.
74 // See https://developers.google.com/compute/docs/metadata#custom.
75 req, err := http.NewRequest("GET", "http://metadata/computeMetadata/v1/i nstance/attributes/password", nil)
76 if err != nil {
77 panic(err)
78 }
79 client := http.Client{}
80 req.Header.Add("X-Google-Metadata-Request", "True")
81 if resp, err := client.Do(req); err == nil {
82 password, err := ioutil.ReadAll(resp.Body)
83 if err != nil {
84 log.Printf("ERROR: Failed to read password from metadata server: %q\n", err)
85 panic(err)
86 }
87 // The IP address of the database is found here:
88 // https://console.developers.google.com/project/31977622648/ sql/instances/webtry/overview
89 // And 3306 is the default port for MySQL.
90 db, err = sql.Open("mysql", fmt.Sprintf("webtry:%s@tcp(173.194.8 3.52:3306)/webtry", password))
91 if err != nil {
92 log.Printf("ERROR: Failed to open connection to SQL serv er: %q\n", err)
93 panic(err)
94 }
95 } else {
96 log.Printf("INFO: Failed to find metadata, unable to connect to MySQL server (Expected when running locally): %q\n", err)
97 }
66 } 98 }
67 99
68 // userCode is used in template expansion. 100 // userCode is used in template expansion.
69 type userCode struct { 101 type userCode struct {
70 UserCode string 102 UserCode string
71 } 103 }
72 104
73 // expandToFile expands the template and writes the result to the file. 105 // expandToFile expands the template and writes the result to the file.
74 func expandToFile(filename string, code string, t *template.Template) error { 106 func expandToFile(filename string, code string, t *template.Template) error {
75 f, err := os.Create(filename) 107 f, err := os.Create(filename)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 } 180 }
149 log.Printf("Error: %s\n%s", message, err.Error()) 181 log.Printf("Error: %s\n%s", message, err.Error())
150 resp, err := json.Marshal(m) 182 resp, err := json.Marshal(m)
151 if err != nil { 183 if err != nil {
152 http.Error(w, "Failed to serialize a response", 500) 184 http.Error(w, "Failed to serialize a response", 500)
153 return 185 return
154 } 186 }
155 w.Write(resp) 187 w.Write(resp)
156 } 188 }
157 189
190 func writeToDatabase(hash string, code string) {
191 if db == nil {
192 return
193 }
194 if _, err := db.Exec("INSERT INTO webtry (code, hash) VALUES(?, ?)", cod e, hash); err != nil {
195 log.Printf("ERROR: Failed to insert code into database: %q\n", e rr)
196 }
197 }
198
158 // mainHandler handles the GET and POST of the main page. 199 // mainHandler handles the GET and POST of the main page.
159 func mainHandler(w http.ResponseWriter, r *http.Request) { 200 func mainHandler(w http.ResponseWriter, r *http.Request) {
160 if r.Method == "GET" { 201 if r.Method == "GET" {
161 w.Write(index) 202 w.Write(index)
162 } else if r.Method == "POST" { 203 } else if r.Method == "POST" {
163 w.Header().Set("Content-Type", "application/json") 204 w.Header().Set("Content-Type", "application/json")
164 b, err := ioutil.ReadAll(r.Body) 205 b, err := ioutil.ReadAll(r.Body)
165 if err != nil { 206 if err != nil {
166 reportError(w, r, err, "Failed to read a request body.") 207 reportError(w, r, err, "Failed to read a request body.")
167 return 208 return
168 } 209 }
169 » » hash, err := expandCode(LineNumbers(string(b))) 210 » » code := string(b)
211 » » hash, err := expandCode(LineNumbers(code))
170 if err != nil { 212 if err != nil {
171 reportError(w, r, err, "Failed to write the code to comp ile.") 213 reportError(w, r, err, "Failed to write the code to comp ile.")
172 return 214 return
173 } 215 }
216 writeToDatabase(hash, code)
174 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t rue) 217 message, err := doCmd(fmt.Sprintf(RESULT_COMPILE, hash, hash), t rue)
175 if err != nil { 218 if err != nil {
176 reportError(w, r, err, "Failed to compile the code:\n"+m essage) 219 reportError(w, r, err, "Failed to compile the code:\n"+m essage)
177 return 220 return
178 } 221 }
179 linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true) 222 linkMessage, err := doCmd(fmt.Sprintf(LINK, hash, hash), true)
180 if err != nil { 223 if err != nil {
181 reportError(w, r, err, "Failed to link the code:\n"+link Message) 224 reportError(w, r, err, "Failed to link the code:\n"+link Message)
182 return 225 return
183 } 226 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 return 258 return
216 } 259 }
217 w.Write(resp) 260 w.Write(resp)
218 } 261 }
219 } 262 }
220 263
221 func main() { 264 func main() {
222 flag.Parse() 265 flag.Parse()
223 266
224 http.HandleFunc("/", mainHandler) 267 http.HandleFunc("/", mainHandler)
225 » log.Fatal(http.ListenAndServe(":8000", nil)) 268 » log.Fatal(http.ListenAndServe(*port, nil))
226 } 269 }
OLDNEW
« no previous file with comments | « experimental/webtry/setup/continue_install ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698