Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Social Network

A Facebook-style social network. It's split into two parts: a Go backend (REST API + WebSockets) and a Next.js frontend, with SQLite for storage. You can follow people, keep your profile public or private, write posts with different privacy levels, comment and like, create groups with events, chat in real time (private and group), and get live notifications.


Features

  • Auth – register and log in. Sessions are kept in a cookie, and there's a "remember me" option.
  • Profiles – public or private, with an avatar, cover photo and bio. You can edit your profile and change your password.
  • Followers – follow and unfollow people. Private accounts get a follow request they can accept or decline. There's also a small "who to follow" list.
  • Posts – text with an optional image. Each post has one of three privacy levels:
    • public – anyone can see it
    • almost_private – only your accepted followers
    • private – only the specific people you pick
  • Comments and likes on posts.
  • Groups – create a group, invite people or let them request to join, post inside the group, and create events people can say "going" / "not going" to.
  • Chat – real-time private and group messages over WebSockets. Supports images, emojis, message requests, and shows who's online.
  • Notifications – for follow requests, group invites, join requests, events, and so on.

Tech Stack

Layer What we used
Backend Go 1.25, standard library net/http (with the Go 1.22 method+path routing)
Database SQLite (mattn/go-sqlite3), migrations with golang-migrate
Realtime WebSockets (gorilla/websocket)
Auth Session cookies, passwords hashed with bcrypt (golang.org/x/crypto)
Frontend Next.js 16 / React 19 / TypeScript

Project Structure

social-network/
├── docker-compose.yml        # runs backend + frontend together
├── run.sh                    # shortcut for docker compose up --build
├── backend/
│   ├── server.go             # entry point, wires up all the routes
│   ├── go.mod
│   └── pkg/
│       ├── auth/             # password hashing, session/auth middleware
│       ├── chat/             # websocket hub
│       ├── db/
│       │   ├── migrations/   # the SQL migration files
│       │   ├── seed/         # demo data added on first run
│       │   └── sqlite/       # opening the db + running migrations
│       ├── handlers/         # http handlers (auth, posts, groups, chat...)
│       ├── middleware/       # CORS
│       └── models/           # db queries + rules like CanViewPost
└── frontend/
    └── src/
        ├── app/              # next.js routes (login, feed, chat, groups...)
        ├── components/       # shared UI
        ├── services/         # talks to the backend on :8080
        ├── lib/              # small helpers (toast, time, lightbox)
        └── types/

Database

How it gets built

We're using SQLite, and the schema is built from migration files instead of by hand. That means you don't have to set anything up: the tables get created for you the first time the server runs.

When the backend starts, server.go does three things in this order:

  1. Opens the SQLite file (sqlite.Open). We connect with ?_foreign_keys=on because SQLite ignores foreign keys by default, and we rely on ON DELETE CASCADE to clean up related rows.
  2. Runs any migrations that haven't run yet (sqlite.Migrate, which calls golang-migrate's m.Up()). It keeps track of the current version in a schema_migrations table, so running it again does nothing. That's why it's safe to run on every startup.
  3. Seeds some demo data, but only if the users table is empty.

So on a fresh clone you just run go run . (or docker compose up) and it creates social.db, runs migrations 1 through 15, and fills in the demo users.

The migration files

They live in backend/pkg/db/migrations/sqlite/. Every change is a pair of files, one to apply it and one to undo it:

000001_create_users.up.sql        # creates the table / index / trigger
000001_create_users.down.sql      # drops it again
000002_create_sessions.up.sql
000002_create_sessions.down.sql
...
000015_create_post_likes.up.sql
000015_create_post_likes.down.sql

The number at the front is the version. golang-migrate only runs the files that are newer than what's already been applied. The .up.sql file moves the schema forward and the .down.sql is the reverse, in case you need to roll back.

One rule we stuck to: don't edit a migration once it's been applied. If we need to change something we add a new, higher-numbered migration instead, otherwise different machines end up with different schemas.

Adding a migration later

  1. Make the next pair of files, e.g. 000016_add_bookmarks.up.sql and 000016_add_bookmarks.down.sql.
  2. Write the change in the .up.sql and the undo (a DROP, usually) in the .down.sql.
  3. Restart the backend and it picks it up automatically.

ER diagram

erDiagram
    users {
        int id PK
        string email UK
        string password_hash
        string first_name
        string last_name
        date date_of_birth
        string nickname
        string about_me
        string avatar
        string cover_photo
        bool is_public
        bool show_dob
        bool allow_message_requests
        datetime created_at
    }
    sessions {
        string id PK
        int user_id FK
        datetime expires_at
    }
    followers {
        int id PK
        int follower_id FK
        int followee_id FK
        string status
    }
    groups {
        int id PK
        int creator_id FK
        string title
        string description
        string image
    }
    group_members {
        int id PK
        int group_id FK
        int user_id FK
        int invited_by FK
        string role
        string status
    }
    posts {
        int id PK
        int user_id FK
        int group_id FK
        string content
        string image
        string privacy
    }
    post_viewers {
        int post_id PK
        int user_id PK
    }
    post_likes {
        int post_id PK
        int user_id PK
    }
    comments {
        int id PK
        int post_id FK
        int user_id FK
        string content
        string image
    }
    events {
        int id PK
        int group_id FK
        int creator_id FK
        string title
        string description
        datetime event_time
    }
    event_responses {
        int id PK
        int event_id FK
        int user_id FK
        string response
    }
    messages {
        int id PK
        int sender_id FK
        int receiver_id FK
        int group_id FK
        string content
        string image
        bool is_read
    }
    message_reads {
        int message_id PK
        int user_id PK
        datetime read_at
    }
    chat_requests {
        int id PK
        int from_user_id FK
        int to_user_id FK
        string status
    }
    notifications {
        int id PK
        int user_id FK
        string type
        int reference_id
        int actor_id FK
        bool is_read
    }

    users           ||--o{ sessions        : "has"
    users           ||--o{ followers        : "follower / followee"
    users           ||--o{ groups           : "creates"
    groups          ||--o{ group_members    : "has"
    users           ||--o{ group_members    : "joins"
    users           ||--o{ posts            : "writes"
    groups          ||--o{ posts            : "contains"
    posts           ||--o{ comments         : "has"
    users           ||--o{ comments         : "writes"
    posts           ||--o{ post_viewers     : "restricted to"
    users           ||--o{ post_viewers     : "can view"
    posts           ||--o{ post_likes       : "receives"
    users           ||--o{ post_likes       : "gives"
    groups          ||--o{ events           : "hosts"
    users           ||--o{ events           : "creates"
    events          ||--o{ event_responses  : "gets"
    users           ||--o{ event_responses  : "answers"
    users           ||--o{ messages         : "sends"
    groups          ||--o{ messages         : "channels"
    messages        ||--o{ message_reads    : "tracked by"
    users           ||--o{ chat_requests    : "sends / receives"
    users           ||--o{ notifications    : "receives"
Loading

Quick table reference

Table What it's for Main columns
users Accounts and profile info email (unique), password_hash, is_public
sessions Login sessions (cookie to user) id (the cookie value), user_id, expires_at
followers Who follows who, plus pending requests follower_id, followee_id, status
groups Groups creator_id, title, description
group_members Membership, invites and join requests group_id, user_id, role, status
posts User and group posts user_id, group_id, privacy
post_viewers The chosen viewers for a private post post_id, user_id
post_likes Likes post_id, user_id
comments Comments on posts post_id, user_id
events Group events group_id, creator_id, event_time
event_responses Going / not going event_id, user_id, response
messages Private and group chat messages sender_id, receiver_id, group_id
message_reads Read receipts for group messages message_id, user_id
chat_requests Message requests between people who don't follow each other from_user_id, to_user_id, status
notifications In-app notifications user_id, type, reference_id, actor_id

How we built it

This was a group project, so we split the work between us and worked through it roughly in this order:

  1. Read the requirements together and made a list of the features (auth, followers, profiles, posts with the three privacy levels, groups, events, chat, notifications) and the rules tying them together, like figuring out who's allowed to see a given post.
  2. Designed the database first. We sketched out the tables and how they relate before writing much code (that's the ER diagram above). The many-to-many stuff, like which users can see a private post or who's in a group, goes into join tables such as post_viewers, post_likes, group_members and event_responses. We also used CHECK constraints so things like post privacy and follow status can only hold valid values.
  3. Wrote the migrations so the schema is repeatable and nobody has to remember any manual setup. Foreign keys with ON DELETE CASCADE handle cleanup.
  4. Built the backend in layers. server.go just maps routes to handlers. The handlers in pkg/handlers deal with the request and response, and pkg/models holds the actual SQL and the rules (things like CanViewPost and ChatPermission). Auth is a RequireAuth middleware that checks the session cookie.
  5. Added the real-time part. There's a WebSocket hub in pkg/chat that pushes chat messages and notifications out to connected clients.
  6. Did the frontend in Next.js. Components don't call the backend directly, they go through a services/ layer, which kept things tidier.
  7. Containerized it with a Dockerfile each for the backend and frontend, plus docker-compose.yml so everything comes up with one command.
  8. Tested as we went with Postman for the API and by clicking through the running app.

Running it

Option A – Docker (easiest)

You need Docker with Compose.

./run.sh

or just:

docker compose up --build -d

Then open:

Handy commands:

docker compose logs -f     # watch the logs
docker compose down        # stop everything

The database and uploaded files are kept on your machine through the backend/data and backend/uploads volumes.

Option B – running it yourself (no Docker)

You'll need Go 1.25+ and Node 20+. The backend uses go-sqlite3, which is cgo, so you also need a C compiler (gcc, or MinGW on Windows) and CGO_ENABLED=1.

First, the backend. Run it from inside backend/, since it looks for the migrations relative to that folder:

cd backend
go run .

It listens on http://localhost:8080. The first run creates social.db, applies the migrations and seeds the demo data.

Then the frontend, in another terminal:

cd frontend
npm install --legacy-peer-deps
npm run dev

That runs on http://localhost:3000 and points at the backend on :8080 (set in frontend/src/services/api.ts).


Demo accounts

The first run seeds a few users you can log in with:

Email Password
layla@test.com password123
omar@test.com password123
karim@test.com password123

They all use password123. There are a few more (fatima@test.com, ahmed@test.com, maya@test.com and others, all in backend/pkg/db/seed/seed.go). Or just register a new account from the Register page.


API at a glance

Everything is under /api, and apart from register/login/logout it needs a valid session cookie. Some of the main ones:

Method & path What it does
POST /api/register make an account
POST /api/login / logout log in / out
GET /api/me the current user
GET /api/posts / POST /api/posts feed / create a post
POST /api/posts/{id}/comments comment on a post
POST /api/posts/{id}/like like a post
GET /api/groups / POST /api/groups list / create groups
POST /api/groups/{id}/events create a group event
GET /api/notifications list notifications
GET /api/chat chat list
GET /ws the websocket
POST /api/upload upload an image
GET /health health check

The full list is in backend/server.go.


Trying the API in Postman

If you want to hit the backend directly:

  1. Register – POST http://localhost:8080/api/register with a JSON body. You need email, password (at least 6 chars), first_name, last_name and date_of_birth (YYYY-MM-DD). nickname, about_me and is_public are optional:
    {
      "email": "newuser@test.com",
      "password": "password123",
      "first_name": "New",
      "last_name": "User",
      "date_of_birth": "1998-05-20",
      "nickname": "newbie",
      "about_me": "Hello!",
      "is_public": true
    }
    To register with an avatar, send the same fields as multipart/form-data with an avatar file part instead of JSON.
  2. Log in – POST http://localhost:8080/api/login:
    { "email": "layla@test.com", "password": "password123" }
    Turn on "Send cookies" so Postman keeps the session cookie.
  3. Call the authenticated endpoints – like GET http://localhost:8080/api/me or GET http://localhost:8080/api/posts. The cookie from logging in goes along automatically.
  4. Create a post – POST http://localhost:8080/api/posts with content and privacy (public, almost_private or private).

Notes

  • The *.db file and uploaded images are runtime stuff, so they're git-ignored. The demo seed images are kept though, so a fresh clone still has working avatars and covers.
  • The backend gets its database path from the DB_PATH env var. It defaults to ./social.db, and Docker sets it to /app/data/social.db.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages