-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
64 lines (51 loc) · 1.3 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
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
)
var (
store PersistJsonnet
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
memFlag := flag.Bool("in-memory", false, "start up the daemon with an in-memory db (test only)")
sqlFlag := flag.Bool("sql", false, "start up the daemon with a sql db (JSONNET_MYSQL_CONN) env for conn string")
flag.Parse()
// Add in toggle for the different types of backends
if *memFlag {
store = InMemory{
store: map[string]string{},
}
}
if *sqlFlag {
// EXAMPLE: "user:password@tcp(localhost:3306)/dbname"
db, err := sql.Open("mysql", os.Getenv("JSONNET_MYSQL_CONN"))
if err != nil {
panic(err)
}
store = NewJSQL(db)
}
if store == nil {
fmt.Println("A storage driver must be provided")
os.Exit(1)
}
server := &http.Server{
Handler: http.TimeoutHandler(http.DefaultServeMux, 7*time.Second, ""),
Addr: ":" + port,
}
http.HandleFunc("/", HandleEditor)
http.HandleFunc("/backend/share", HandleShare)
http.HandleFunc("/backend/compile", HandleCompile)
fileServer := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fileServer))
log.Fatalf("Error listening on :%v: %v", port, server.ListenAndServe())
}