This is a basic blog application built using Go for the backend and Alpine.js, HTMX, and Tailwind CSS for the frontend (I am very bad at frontend, so it is what it is). The application allows users to create, update, delete, and view blog posts. The idea was to create a simple restful CRUD API with Go and add some frontend features without using Gin or other frameworks. Later version may be rewritten to use Gin, though. The blog app can be stateless or stateful with SQLite.
The default username and password are admin
and password
in this demo
- Authorise: Login page to authorise the user, just the default admin account works at the moment.
- Create Post: Create new blog posts by providing a title and content.
- View Posts: View a list of all blog posts.
- Update Post: Update the title and content of existing blog posts.
- Delete Post: Delete blog posts.
- Toggle Posts: Toggle the visibility of the list of all blog posts.
- Go: Backend server to handle API requests.
- Alpine.js: Lightweight JavaScript framework for reactive data binding.
- HTMX: Library to extend HTML with AJAX capabilities.
- Tailwind CSS: Utility-first CSS framework for styling.
- Go installed on your local machine.
- Clone the repository:
git clone https://github.com/Vadoid/go-blog.git
- Navigate to the project directory:
cd go-blog
- If you want to run SQLite persistent version, set value to
true
in main.govar persistent = false
- Start the Go server:
go run .
- Open your web browser and navigate to:
http://localhost:8080
- Fill in the "Title" and "Content" fields in the "Create Post" form.
- Click the "Create Post" button to submit the form.
- Click the "Show All Posts" button to toggle the visibility of the posts list.
- The button text will change to "Hide All Posts" and the list of posts will be displayed.
- Click the "Edit" button under a post to switch to the editing mode.
- Modify the "Title" and "Content" fields.
- Click the "Update" button to save the changes.
- Click the "Cancel" button to exit the editing mode without saving changes.
- Click the "Delete" button under a post to remove it from the list.
- Issue
TOKEN=$(curl -X POST http://localhost:8080/login -d '{"username":"admin","password":"password"}' -H "Content-Type: application/json" | jq -r .token)
to authorise. - To test API use
curl -X GET http://localhost:8080/posts -H "Authorization: Bearer $TOKEN"
- Install Delve -
go install github.com/go-delve/delve/cmd/[email protected]
- Start debuggung in VSCode.
- main.go: Go server code that handles API requests for creating, reading, updating, and deleting posts.
- main_test.go: Test for
main.go
triggered automatically via Github Actions (for local test dogo test -v
). - db.go: Handles DB operations, only kicks in when the persistent var is set to true.
- db_test.go: Test of DB deployment and CRUD operations. Triggers only if
persistent = true
inmain.go
. - local_test_and_run.sh: Bash script to test and run locally.
- app.yaml: One liner for Google App Engine deployment.
- templates/index.html: HTML file with Alpine.js and HTMX for the frontend functionality.
- launch.json: JSON setup file for debugging.