-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-restsrv.go
95 lines (73 loc) · 1.95 KB
/
go-restsrv.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
package main
import (
"fmt"
"github.com/jgoney/go-rest/orm"
"io/ioutil"
"log"
"net/http"
"os"
)
// Utility function to view available header members
func enumHeader(w *http.ResponseWriter, r *http.Request) {
for k, v := range r.Header {
fmt.Fprint(*w, k, v, "\n")
}
}
func createEntry(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Creating an entry:\n\n")
fmt.Fprint(w, "\n")
if r.Method == "POST" {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Fprint(w, r.PostForm)
model := orm.ExampleModel{}
m := orm.NewModel(model)
//model.SetFieldsFromPOST(r.PostForm)
orm.InsertDB(m)
}
}
func viewEntry(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path[len("/view/"):])
fmt.Fprint(w, "Viewing entry:", r.URL.Path[len("/edit/"):])
}
func siteRoot(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadFile("html/index.html")
if err != nil {
log.Fatal(err)
}
w.Write(body)
}
func main() {
myModel := orm.ExampleModel{Firstname: "Justin",
Lastname: "Goney",
Email: "[email protected]",
Gender: "Male"}
m := orm.NewModel(myModel)
a := []*orm.Model{orm.NewModel(orm.ExampleModel{Firstname: "Bernice", Lastname: "Smith", Email: "[email protected]", Gender: "Female"}),
orm.NewModel(orm.ExampleModel{Firstname: "McLovin", Lastname: "", Email: "[email protected]", Gender: "Male"}),
}
aModel := orm.AnotherModel{Fee: "Fee",
Fi: "Fi",
Fo: "Fo",
Fum: 3.14}
ma := orm.NewModel(aModel)
// Create and initialize DB only if it doesn't exist
if _, err := os.Stat(orm.DB_NAME); err != nil {
orm.InitDB(m, ma)
}
// Insert ExampleModel and array of ExampleModels
orm.InsertDB(m)
orm.InsertDB(a...)
// Insert AnotherModel
orm.InsertDB(ma)
list := orm.GetResultsDB(m)
for _, v := range list {
fmt.Println(v)
}
http.HandleFunc("/create", createEntry)
http.HandleFunc("/view/", viewEntry)
http.HandleFunc("/", siteRoot)
http.ListenAndServe("localhost:4000", nil)
}