diff --git a/src/controllers/post/post.go b/src/controllers/post/post.go index 7e757e9..88b8e84 100644 --- a/src/controllers/post/post.go +++ b/src/controllers/post/post.go @@ -30,6 +30,42 @@ func Retrieve(name string) (*Post, error) { return &val, nil } + // Add the post to the database + db, err := database.GetConn() + if err != nil { + return nil, err + } + + defer db.Close() + + sql := `SELECT p.name, p.content FROM freepad.t_posts p WHERE p.name = ? LIMIT 1;` + s, err := db.Prepare(sql) + if err != nil { + return nil, err + } + + rows, err := s.Query(name) + if err != nil { + return nil, err + } + defer rows.Close() + + anyLeft := rows.Next() + if !anyLeft { + return nil, errors.New("could not find the requested post") + } + + foundPost := Post{ + Name: "", + Content: "", + } + + err = rows.Scan(&foundPost.Name, &foundPost.Content) + if err != nil { + return nil, err + } + + return &foundPost, nil } func Create(name string, content string) (*Post, error) { diff --git a/src/main.go b/src/main.go index 48d04d9..9ea3f44 100644 --- a/src/main.go +++ b/src/main.go @@ -14,11 +14,16 @@ func main() { router.LoadHTMLGlob("templates/*") // Add Routes - // Bind health checks - routes.HealthRoutes(router) + routes.HomeRoutes(router) // Bind /api routes.ApiRoutes(router.Group("/api")) + router.GET("/index", func(c *gin.Context) { + c.HTML(200, "index.html", gin.H{ + "title": "Main website", + }) + }) + router.Run(":8080") } diff --git a/src/routes/routes_api.go b/src/routes/routes_api.go index 7035c1a..e9e3d6d 100644 --- a/src/routes/routes_api.go +++ b/src/routes/routes_api.go @@ -12,13 +12,6 @@ import ( func ApiRoutes(route *gin.RouterGroup) { - // Add in - route.GET("/", func(ctx *gin.Context) { - ctx.JSON(200, gin.H{ - "message": "Ok!", - }) - }) - route.POST("/post", func(ctx *gin.Context) { // Get the name of the post postName := ctx.PostForm("name") @@ -42,9 +35,33 @@ func ApiRoutes(route *gin.RouterGroup) { }) + route.GET("/post", func(ctx *gin.Context) { + // Get the name of the post + postName := ctx.PostForm("name") + + myPost, err := post.Retrieve(postName) + if err != nil { + ctx.JSON(400, types.FreeError{ + Error: err.Error(), + Message: "There has been an error processing your request", + }) + } + + // Return the post list + ctx.JSON(200, myPost) + }) + route.GET("/posts", func(ctx *gin.Context) { // Return the post list ctx.JSON(200, post.GetPostList()) }) + // Add in health checks + route.GET("/health", healthCheck) +} + +func healthCheck(ctx *gin.Context) { + ctx.JSON(200, gin.H{ + "message": "Healthy", + }) } diff --git a/src/routes/routes_health.go b/src/routes/routes_health.go deleted file mode 100644 index a707ba5..0000000 --- a/src/routes/routes_health.go +++ /dev/null @@ -1,18 +0,0 @@ -package routes - -import "github.com/gin-gonic/gin" - -func HealthRoutes(route *gin.Engine) { - - // Add in - route.GET("/health", healthCheck) - -} - -func healthCheck(ctx *gin.Context) { - - ctx.JSON(200, gin.H{ - "message": "Healthy", - }) - -} diff --git a/src/routes/routes_main.go b/src/routes/routes_main.go new file mode 100644 index 0000000..e88749f --- /dev/null +++ b/src/routes/routes_main.go @@ -0,0 +1,7 @@ +package routes + +import "github.com/gin-gonic/gin" + +func HomeRoutes(route *gin.Engine) { + +} diff --git a/src/static/css/.keep b/src/static/css/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/static/img/.keep b/src/static/img/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/static/js/.keep b/src/static/js/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/static/vendor/.keep b/src/static/vendor/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/templates/inc/footer.html b/src/templates/inc/footer.html new file mode 100644 index 0000000..ac083bd --- /dev/null +++ b/src/templates/inc/footer.html @@ -0,0 +1,5 @@ +{{ define "inc/header.html"}} + +{{ end }} \ No newline at end of file diff --git a/src/templates/inc/header.html b/src/templates/inc/header.html new file mode 100644 index 0000000..9550ac2 --- /dev/null +++ b/src/templates/inc/header.html @@ -0,0 +1,8 @@ +{{ define "inc/header.html"}} + + + + + {{.title}} + +{{ end }} \ No newline at end of file diff --git a/src/templates/index.html b/src/templates/index.html index e69de29..b189b89 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -0,0 +1,12 @@ + + + + + + + FreePad + + + {{ .title }} + + \ No newline at end of file diff --git a/src/templates/page.html b/src/templates/page.html new file mode 100644 index 0000000..e69de29