diff --git a/src/main.go b/src/main.go index 9ea3f44..dad054d 100644 --- a/src/main.go +++ b/src/main.go @@ -11,19 +11,16 @@ func main() { router := gin.Default() // Read HTML Templates - router.LoadHTMLGlob("templates/*") + router.LoadHTMLGlob("templates/**/*.html") + + // Load in static path + router.Static("/static", "static/") // Add Routes 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_main.go b/src/routes/routes_main.go index e88749f..c12a84d 100644 --- a/src/routes/routes_main.go +++ b/src/routes/routes_main.go @@ -1,7 +1,38 @@ package routes -import "github.com/gin-gonic/gin" +import ( + "github.com/JustKato/FreePad/controllers/post" + "github.com/JustKato/FreePad/helper" + "github.com/gin-gonic/gin" +) -func HomeRoutes(route *gin.Engine) { +func HomeRoutes(router *gin.Engine) { + + router.GET("/", func(c *gin.Context) { + c.HTML(200, "index.html", gin.H{ + "title": "HomePage", + "domain_base": helper.GetDomainBase(), + }) + }) + + router.GET("/:post", func(c *gin.Context) { + // Get the post we are looking for. + postName := c.Param("post") + + // Try and get this post's data + postData, err := post.Retrieve(postName) + if err != nil { + postData = &post.Post{ + Name: postName, + Content: "", + } + } + + c.HTML(200, "page.html", gin.H{ + "title": postName, + "post_content": postData.Content, + "domain_base": helper.GetDomainBase(), + }) + }) } diff --git a/src/static/css/main.css b/src/static/css/main.css new file mode 100644 index 0000000..155f560 --- /dev/null +++ b/src/static/css/main.css @@ -0,0 +1,3 @@ +#post_content { + height: calc(100vh - 35rem); +} \ No newline at end of file diff --git a/src/static/img/gopher.png b/src/static/img/gopher.png new file mode 100644 index 0000000..71913a8 Binary files /dev/null and b/src/static/img/gopher.png differ diff --git a/src/static/js/main.js b/src/static/js/main.js new file mode 100644 index 0000000..5c81d82 --- /dev/null +++ b/src/static/js/main.js @@ -0,0 +1,71 @@ + +function setStatus(text, className ) { + // Show loading + const statusIndicator = document.getElementById(`status-indicator`); + + if ( !!statusIndicator ) { + // Clear all previous status-es + for ( let [x, k] of statusIndicator.classList.entries() ) { + statusIndicator.classList.remove(k); + } + + // Mark as loading + statusIndicator.textContent = text; + statusIndicator.classList.add(className); + } + +} + +function updatePost(postName) { + + const postContentElement = document.getElementById(`post_content`); + + if ( !!postContentElement && !!postContentElement.value ) { + const postContent = String(postContentElement.value); + if ( !!postContent && postContent.length > 0 ) { + + setStatus(`Loading...`, `has-text-warning`); + + // Generate the form data + let formData = new FormData(); + formData.append('name', postName); + formData.append('content', postContent); + + // Send out a fetch request + fetch("/api/post", { + method: "post", + body: formData, + }) + .then( result => { + console.log(result); + setStatus(`Saved`, `has-text-success`); + }) + .catch( error => { + console.error(error); + alert(error); + setStatus(`Failed to Save`, `has-text-danger`); + }) + } + } + +} + +/** + * @location / + * @role Searching + */ +function goToPost() { + // Get the post name element + const postNameElement = document.getElementById(`postName`); + + // Check if the element exists + if ( !!postNameElement ) { + // Get the post name string + const postName = String(postNameElement.value); + // Check if the post name is valid + if ( !!postName && postName.length > 0 && postName.length <= 256 ) { + // Change the location + window.location.href = `/${postName}`; + } + } +} diff --git a/src/templates/inc/footer.html b/src/templates/inc/footer.html index ac083bd..0293f9f 100644 --- a/src/templates/inc/footer.html +++ b/src/templates/inc/footer.html @@ -1,5 +1,20 @@ -{{ define "inc/header.html"}} -