/
main.go
47 lines (41 loc) · 1.01 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
package main
import (
"encoding/json"
"github.com/codegangsta/martini-contrib/render"
"github.com/go-martini/martini"
"io/ioutil"
)
type Tutorial struct {
Steps []Step `json:"steps"`
ID string `json:"id"`
Title string `json:"title"`
Language string `json:"language"`
}
type Step struct {
Instruction string `json:"instruction"`
Spec string `json:"spec"`
Source string `json:"source"`
}
func renderTutorialIndex(ren render.Render) {
ren.JSON(200, []map[string]string{{"id": "1", "title": "FizzBuzz"}})
}
func renderTutorial(ren render.Render) {
var tutorial Tutorial
file, err := ioutil.ReadFile("neutrino.json")
if err != nil {
panic(err)
}
json.Unmarshal(file, &tutorial)
ren.JSON(200, tutorial)
}
func main() {
m := martini.Classic()
renderOptions := render.Options{
Directory: "public/templates",
Extensions: []string{".tmpl", ".html"},
}
m.Use(render.Renderer(renderOptions))
m.Get("/tutorials/:id", renderTutorial)
m.Get("/tutorials", renderTutorialIndex)
m.Run()
}