Certainly! Below is a basic outline for a journal app backend using Go, including a project structure and essential components. This example will cover user authentication, creating journal entries, and allowing other users to comment, like, and share those entries.
Project Structure
journal-app/
├── cmd/
│ └── journal-app/
│ └── main.go
├── internal/
│ ├── handler/
│ │ ├── auth.go
│ │ ├── entry.go
│ │ └── comment.go
│ ├── model/
│ │ ├── user.go
│ │ ├── entry.go
│ │ └── comment.go
│ ├── service/
│ │ ├── auth_service.go
│ │ ├── entry_service.go
│ │ └── comment_service.go
│ └── repository/
│ ├── user_repo.go
│ ├── entry_repo.go
│ └── comment_repo.go
├── pkg/
│ ├── auth/
│ │ └── jwt.go
│ └── database/
│ ├── db.go
│ └── migrations/
│ └── init.sql
├── go.mod
└── go.sum
Dependencies
You'll need some dependencies for this project. You can install them using go get.
go get github.com/go-sql-driver/mysql
go get github.com/jmoiron/sqlx
go get github.com/dgrijalva/jwt-go
go get github.com/go-playground/validator/v10
Main Application (cmd/journal-app/main.go)
package main
import (
"journal-app/internal/handler"
"journal-app/internal/repository"
"journal-app/internal/service"
"journal-app/pkg/database"
"log"
"net/http"
)
func main() {
db, err := database.NewDB()
if err != nil {
log.Fatalf("Failed to connect to database: %v", err)
}
userRepo := repository.NewUserRepository(db)
entryRepo := repository.NewEntryRepository(db)
commentRepo := repository.NewCommentRepository(db)
authService := service.NewAuthService(userRepo)
entryService := service.NewEntryService(entryRepo)
commentService := service.NewCommentService(commentRepo)
authHandler := handler.NewAuthHandler(authService)
entryHandler := handler.NewEntryHandler(entryService)
commentHandler := handler.NewCommentHandler(commentService)
http.HandleFunc("/auth/register", authHandler.Register)
http.HandleFunc("/auth/login", authHandler.Login)
http.HandleFunc("/entries", entryHandler.CreateEntry)
http.HandleFunc("/entries/", entryHandler.GetEntry) // Note the trailing slash
http.HandleFunc("/entries/like", entryHandler.LikeEntry)
http.HandleFunc("/entries/comment", commentHandler.CreateComment)
log.Println("Server started on port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
Database Connection (pkg/database/db.go)
package database
import (
"database/sql"
"log"
_ "github.com/go-sql-driver/mysql"
)
const (
dbUser = "root"
dbPassword = "password"
dbName = "journal_app"
)
func NewDB() (*sql.DB, error) {
db, err := sql.Open("mysql", dbUser+":"+dbPassword+"@/"+dbName)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
return db, nil
}
User Model (internal/model/user.go)
package model
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Password string `json:"password"`
}
Entry Model (internal/model/entry.go)
package model
type Entry struct {
ID int `json:"id"`
UserID int `json:"user_id"`
Content string `json:"content"`
CreatedAt string `json:"created_at"`
}
Comment Model (internal/model/comment.go)
package model
type Comment struct {
ID int `json:"id"`
EntryID int `json:"entry_id"`
UserID int `json:"user_id"`
Content string `json:"content"`
CreatedAt string `json:"created_at"`
}
Auth Handler (internal/handler/auth.go)
package handler
import (
"encoding/json"
"net/http"
"journal-app/internal/service"
"journal-app/internal/model"
)
type AuthHandler struct {
service service.AuthService
}
func NewAuthHandler(service service.AuthService) *AuthHandler {
return &AuthHandler{service: service}
}
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
var user model.User
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := h.service.Register(user); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
var creds struct {
Username string `json:"username"`
Password string `json:"password"`
}
if err := json.NewDecoder(r.Body).Decode(&creds); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
token, err := h.service.Login(creds.Username, creds.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
json.NewEncoder(w).Encode(map[string]string{"token": token})
}
Entry Handler (internal/handler/entry.go)
package handler
import (
"encoding/json"
"net/http"
"journal-app/internal/service"
"journal-app/internal/model"
)
type EntryHandler struct {
service service.EntryService
}
func NewEntryHandler(service service.EntryService) *EntryHandler {
return &EntryHandler{service: service}
}
func (h *EntryHandler) CreateEntry(w http.ResponseWriter, r *http.Request) {
var entry model.Entry
if err := json.NewDecoder(r.Body).Decode(&entry); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := h.service.CreateEntry(entry); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func (h *EntryHandler) GetEntry(w http.ResponseWriter, r *http.Request) {
// Implement fetching an entry by ID
}
func (h *EntryHandler) LikeEntry(w http.ResponseWriter, r *http.Request) {
// Implement liking an entry
}
Comment Handler (internal/handler/comment.go)
package handler
import (
"encoding/json"
"net/http"
"journal-app/internal/service"
"journal-app/internal/model"
)
type CommentHandler struct {
service service.CommentService
}
func NewCommentHandler(service service.CommentService) *CommentHandler {
return &CommentHandler{service: service}
}
func (h *CommentHandler) CreateComment(w http.ResponseWriter, r *http.Request) {
var comment model.Comment
if err := json.NewDecoder(r.Body).Decode(&comment); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := h.service.CreateComment(comment); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}