mirror of https://github.com/JustKato/FreePad.git
				
				
				
			Cleanup
This commit is contained in:
		
							parent
							
								
									962dce2c2a
								
							
						
					
					
						commit
						01c193ceef
					
				| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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",
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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",
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,7 @@
 | 
			
		|||
package routes
 | 
			
		||||
 | 
			
		||||
import "github.com/gin-gonic/gin"
 | 
			
		||||
 | 
			
		||||
func HomeRoutes(route *gin.Engine) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
{{ define "inc/header.html"}}
 | 
			
		||||
<footer>
 | 
			
		||||
    
 | 
			
		||||
</footer>
 | 
			
		||||
{{ end }}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,8 @@
 | 
			
		|||
{{ define "inc/header.html"}}
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    <title>{{.title}}</title>
 | 
			
		||||
</head>
 | 
			
		||||
{{ end }}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,12 @@
 | 
			
		|||
<!DOCTYPE html>
 | 
			
		||||
<html lang="en">
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="UTF-8">
 | 
			
		||||
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
    <title>FreePad</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
    {{ .title }}
 | 
			
		||||
</body>
 | 
			
		||||
</html>
 | 
			
		||||
		Loading…
	
		Reference in New Issue