OLD | NEW |
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 "database/sql" |
7 "encoding/base64" | 7 "encoding/base64" |
8 "encoding/binary" | 8 "encoding/binary" |
9 "encoding/json" | 9 "encoding/json" |
10 "flag" | 10 "flag" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 panic(err) | 231 panic(err) |
232 } | 232 } |
233 } else { | 233 } else { |
234 log.Printf("INFO: Failed to find metadata, unable to connect to
MySQL server (Expected when running locally): %q\n", err) | 234 log.Printf("INFO: Failed to find metadata, unable to connect to
MySQL server (Expected when running locally): %q\n", err) |
235 // Fallback to sqlite for local use. | 235 // Fallback to sqlite for local use. |
236 db, err = sql.Open("sqlite3", "./webtry.db") | 236 db, err = sql.Open("sqlite3", "./webtry.db") |
237 if err != nil { | 237 if err != nil { |
238 log.Printf("ERROR: Failed to open: %q\n", err) | 238 log.Printf("ERROR: Failed to open: %q\n", err) |
239 panic(err) | 239 panic(err) |
240 } | 240 } |
241 » » sql := `CREATE TABLE source_images ( | 241 » » sql := `CREATE TABLE IF NOT EXISTS source_images ( |
242 id INTEGER PRIMARY KEY NOT NULL, | 242 id INTEGER PRIMARY KEY NOT NULL, |
243 image MEDIUMBLOB DEFAULT '' NOT NULL, -- forma
tted as a PNG. | 243 image MEDIUMBLOB DEFAULT '' NOT NULL, -- forma
tted as a PNG. |
244 width INTEGER DEFAULT 0 NOT NULL, | 244 width INTEGER DEFAULT 0 NOT NULL, |
245 height INTEGER DEFAULT 0 NOT NULL, | 245 height INTEGER DEFAULT 0 NOT NULL, |
246 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 246 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
247 hidden INTEGER DEFAULT 0 NOT NULL | 247 hidden INTEGER DEFAULT 0 NOT NULL |
248 )` | 248 )` |
249 _, err = db.Exec(sql) | 249 _, err = db.Exec(sql) |
250 » » log.Printf("Info: status creating sqlite table for sources: %q\n
", err) | 250 » » if err != nil { |
| 251 » » » log.Printf("Info: status creating sqlite table for sourc
es: %q\n", err) |
| 252 » » } |
251 | 253 |
252 » » sql = `CREATE TABLE webtry ( | 254 » » sql = `CREATE TABLE IF NOT EXISTS webtry ( |
253 code TEXT DEFAULT '' NOT NULL, | 255 code TEXT DEFAULT '' NOT NULL, |
254 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 256 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
255 hash CHAR(64) DEFAULT '' NOT NULL, | 257 hash CHAR(64) DEFAULT '' NOT NULL, |
256 source_image_id INTEGER DEFAULT 0 NOT NULL, | 258 source_image_id INTEGER DEFAULT 0 NOT NULL, |
257 | 259 |
258 PRIMARY KEY(hash) | 260 PRIMARY KEY(hash) |
259 )` | 261 )` |
260 _, err = db.Exec(sql) | 262 _, err = db.Exec(sql) |
261 » » log.Printf("Info: status creating sqlite table for webtry: %q\n"
, err) | 263 » » if err != nil { |
| 264 » » » log.Printf("Info: status creating sqlite table for webtr
y: %q\n", err) |
| 265 » » } |
262 | 266 |
263 » » sql = `CREATE TABLE workspace ( | 267 » » sql = `CREATE TABLE IF NOT EXISTS workspace ( |
264 name CHAR(64) DEFAULT '' NOT NULL, | 268 name CHAR(64) DEFAULT '' NOT NULL, |
265 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 269 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
266 PRIMARY KEY(name) | 270 PRIMARY KEY(name) |
267 )` | 271 )` |
268 _, err = db.Exec(sql) | 272 _, err = db.Exec(sql) |
269 » » log.Printf("Info: status creating sqlite table for workspace: %q
\n", err) | 273 » » if err != nil { |
| 274 » » » log.Printf("Info: status creating sqlite table for works
pace: %q\n", err) |
| 275 » » } |
270 | 276 |
271 » » sql = `CREATE TABLE workspacetry ( | 277 » » sql = `CREATE TABLE IF NOT EXISTS workspacetry ( |
272 name CHAR(64) DEFAULT '' NOT NULL, | 278 name CHAR(64) DEFAULT '' NOT NULL, |
273 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, | 279 create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL, |
274 hash CHAR(64) DEFAULT '' NOT NULL, | 280 hash CHAR(64) DEFAULT '' NOT NULL, |
275 hidden INTEGER DEFAULT 0 NOT NULL, | 281 hidden INTEGER DEFAULT 0 NOT NULL, |
276 source_image_id INTEGER DEFAULT 0 NOT NULL, | 282 source_image_id INTEGER DEFAULT 0 NOT NULL, |
277 | 283 |
278 FOREIGN KEY (name) REFERENCES workspace(name) | 284 FOREIGN KEY (name) REFERENCES workspace(name) |
279 )` | 285 )` |
280 _, err = db.Exec(sql) | 286 _, err = db.Exec(sql) |
281 » » log.Printf("Info: status creating sqlite table for workspace try
: %q\n", err) | 287 » » if err != nil { |
| 288 » » » log.Printf("Info: status creating sqlite table for works
pace try: %q\n", err) |
| 289 » » } |
282 } | 290 } |
283 | 291 |
284 // Ping the database to keep the connection fresh. | 292 // Ping the database to keep the connection fresh. |
285 go func() { | 293 go func() { |
286 c := time.Tick(1 * time.Minute) | 294 c := time.Tick(1 * time.Minute) |
287 for _ = range c { | 295 for _ = range c { |
288 if err := db.Ping(); err != nil { | 296 if err := db.Ping(); err != nil { |
289 log.Printf("ERROR: Database failed to respond: %
q\n", err) | 297 log.Printf("ERROR: Database failed to respond: %
q\n", err) |
290 } | 298 } |
291 } | 299 } |
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
886 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) | 894 http.HandleFunc("/sources/", autogzip.HandleFunc(sourcesHandler)) |
887 | 895 |
888 // Resources are served directly | 896 // Resources are served directly |
889 // TODO add support for caching/etags/gzip | 897 // TODO add support for caching/etags/gzip |
890 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) | 898 http.Handle("/res/", autogzip.Handle(http.FileServer(http.Dir("./")))) |
891 | 899 |
892 // TODO Break out /c/ as it's own handler. | 900 // TODO Break out /c/ as it's own handler. |
893 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) | 901 http.HandleFunc("/", autogzip.HandleFunc(mainHandler)) |
894 log.Fatal(http.ListenAndServe(*port, nil)) | 902 log.Fatal(http.ListenAndServe(*port, nil)) |
895 } | 903 } |
OLD | NEW |