diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bdf995 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +bin/ +src/ diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..8ed18a2 --- /dev/null +++ b/app_test.go @@ -0,0 +1,40 @@ +package main + +import ( + . "github.com/franela/goblin" + "net/http" + "net/http/httptest" + "testing" +) + +func performRequest(method, path string) *httptest.ResponseRecorder { + app := NewApp() + req, _ := http.NewRequest(method, path, nil) + w := httptest.NewRecorder() + app.ServeHTTP(w, req) + return w +} + +func Test(t *testing.T) { + g := Goblin(t) + g.Describe("App api", func() { + + g.It("Should return 200 on / ", func() { + w := performRequest("GET", "/") + g.Assert(w.Code).Equal(200) + }) + + g.It("Should return 200 on /slides.md ", func() { + w := performRequest("GET", "/slides.md") + g.Assert(w.Code).Equal(200) + }) + + g.It("Should return 200 on PUT /slides.md ", func() { + w := performRequest("PUT", "/slides.md") + g.Assert(w.Code).Equal(200) + }) + + g.It("Should works") + + }) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..14e9f9d --- /dev/null +++ b/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "io/ioutil" +) + +var DB = make(map[string]string) + +func NewApp() *gin.Engine { + + r := gin.Default() + + r.GET("/slides.md", func(c *gin.Context) { + body, err := ioutil.ReadFile("initial-slides.md") + if err != nil { + panic(err) + } + c.String(200, string(body)) + }) + + r.GET("/", func(c *gin.Context) { + c.HTML(200, "index.tmpl", gin.H{ + "pubTo": "Users", + }) + }) + + // Get user value + r.GET("/user/:name", func(c *gin.Context) { + user := c.Params.ByName("name") + value, ok := DB[user] + if ok { + c.JSON(200, gin.H{"user": user, "value": value}) + } else { + c.JSON(200, gin.H{"user": user, "status": "no value"}) + } + }) + + return r + +} + +func main() { + r := NewApp() + r.Run(":8080") +} diff --git a/templates/index.tmpl b/templates/index.tmpl new file mode 100644 index 0000000..c9c3948 --- /dev/null +++ b/templates/index.tmpl @@ -0,0 +1,34 @@ +{{ define "users/index.tmpl" }} + + + + + + + + Hacker Slides + + + + + +
+
+ Present +
+
+
+ +
+ +
+ + + + + + + + + +{{ end }}