-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
executable file
·70 lines (55 loc) · 1.62 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
package main
import (
"./controllers"
"./ripple"
"database/sql"
"log"
"net/http"
_ "./go-sqlite3"
"io/ioutil"
"strings"
"os"
)
func initializeDatabase() (*sql.DB, error) {
db, err := sql.Open("sqlite3", "./Todos.sqlite3")
if err != nil { return nil, err }
_, err = db.Exec("CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT, text TEXT)")
if err != nil { return nil, err }
return db, nil
}
// Very simple HTTP handler for testing the web application and API
func webAppHandler(w http.ResponseWriter, r *http.Request) {
t := strings.Split(r.URL.Path, "/")
filename := t[len(t) - 1]
if filename == "" { filename = "index.html" } // Default to index.html
filePath := "html/" + filename
fi, err := os.Stat(filePath)
if fi == nil && err != nil {
// File doesn't exist
return
}
content, _ := ioutil.ReadFile(filePath)
w.Write(content)
}
func main() {
db, err := initializeDatabase()
if err != nil {
log.Panicln(err)
}
defer db.Close()
app := ripple.NewApplication()
todoController := rippledemo.NewTodoController(db)
userController := rippledemo.NewUserController(db)
app.RegisterController("todos", todoController)
app.RegisterController("users", userController)
app.AddRoute(ripple.Route{Pattern: ":_controller/:id/:_action"})
app.AddRoute(ripple.Route{Pattern: ":_controller/:id/"})
app.AddRoute(ripple.Route{Pattern: ":_controller"})
// Handle the front-end
http.HandleFunc("/app/", webAppHandler)
// Handle the REST API
app.SetBaseUrl("/api/")
http.HandleFunc("/api/", app.ServeHTTP)
log.Println("Starting server...")
http.ListenAndServe(":8080", nil)
}