Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
broemp committed Aug 16, 2023
1 parent b6af485 commit 248bc54
Show file tree
Hide file tree
Showing 34 changed files with 1,947 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#API Settings
DISCORD_TOKEN=CHANGE_ME
TELEGRAM_TOKEN=CHANGE_ME

#DB Settings
DB_DRIVER=postgres
DB_SOURCE=postgresql://{USER}:{PASSWORD}@{IP}:{PORT}/broempSignal?sslmode=disable

#Server Settings
SERVER_ADDRESS=0.0.0.0:8080
50 changes: 50 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: ci-test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

test:
name: Test
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: broempSignal
POSTGRES_USER: broempSignal
POSTGRES_DB: broempSignal
ports:
- 5432:5432
options: >-
--health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3

- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.16.2/migrate.linux-amd64.tar.gz | tar xvz
sudo mv migrate /usr/bin/
- name: Run migrations
run: make migrateup
env:
POSTGRES_HOST: postgres

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Test
run: make test
env:
POSTGRES_HOST: postgres
89 changes: 89 additions & 0 deletions .github/workflows/docker-image-multiarch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: ci

on:
push:
branches:
- "main"

env:
REGISTRY_IMAGE: broemp/broemp-signal

jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
platform:
- linux/amd64
- linux/arm/v6
- linux/arm/v7
- linux/arm64
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v4
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1

merge:
runs-on: ubuntu-latest
needs:
- build
steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY_IMAGE }}
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM golang:latest AS BUILD

WORKDIR /build

COPY go.mod .
RUN go mod download

COPY . .

RUN CGO_ENABLED=0 go build -o /broempSignal main.go

FROM alpine
WORKDIR /app
COPY --from=BUILD /broempSignal .
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
postgres:
@echo "Setting up postgres..."
docker run --name broempSignalDB -e POSTGRES_USER=broempSignal -e POSTGRES_PASSWORD=broempSignal -p 5432:5432 -d postgres:alpine

createdb:
@echo "Creating database..."
docker exec -it broempSignalDB createdb --username=broempSignal --owner=broempSignal broempSignal

dropdb:
@echo "Dropping database..."
docker rm -f broempSignalDB

migrateup:
migrate -path db/migration -database "postgresql://broempSignal:broempSignal@$(DB_URL):5432/broempSignal?sslmode=disable" -verbose up

migratedown:
migrate -path db/migration -database "postgresql://broempSignal:broempSignal@$(DB_URL):5432/broempSignal?sslmode=disable" -verbose down

sqlc:
sqlc generate

test:
go test -v -cover ./...

server:
go run main.go

.PHONY: createdb dropdb postgres migrateup migratedown sqlc test server
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# BroempSignal

A Discord and Telegram Bot that messages your friends for you!

## Running the Program

### Locally

Create an .env file or set the Envs according to the [.env.example](.env.example) and
Create an sqlite Database
Then run the following Commands

```sh
git clone https://github.com/broemp/broempSignal
go mod install
go run BroempSignal.go
```

### Docker
[Docker Hub](https://hub.docker.com/r/broemp/broemp_signal)

Use the [Docker Compose File](docker-compose.yml)
```yaml
version: '3.8'
services:
broempSignal:
image: broemp/broemp_signal:latest
container_name: broempSignal
restart: always
environment:
# Discord Bot Token https://discord.com/developers/applications
- DISCORD_TOKEN=<token>
# Discord Server ID
- GUILD_ID=<guild_id>
# Telegram Bot Token https://telegram.me/BotFather
- TELEGRAM_TOKEN=<token>
# or use env_file
#env_file:
# - ".env"
volumes:
# Make DB persistent
- ./data.db:/app/data.db
```
or run
```
docker run \
-e DISCORD_TOKEN=<token> \
-e GUILD_ID=<guild_id> \
-e TELEGRAM_TOKEN=<token> \
-v ./data.db:/app/data.db \
broemp/broemp_signal
```
35 changes: 35 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package api

import (
"github.com/gin-gonic/gin"

db "github.com/broemp/broempSignal/db/sqlc"
)

type Server struct {
store *db.Store
router *gin.Engine
}

// NewServer creates a new HTTP server and setups routing
func NewServer(store *db.Store) *Server {
server := &Server{store: store}
router := gin.Default()

router.POST("/users", server.createUser)
router.GET("/users/:id", server.getUser)
router.GET("/users/", server.listUser)
// router.GET("/users/discord/:discordid", server.getDiscordUser)

server.router = router
return server
}

// Start running the HTTP server on address
func (server *Server) Start(address string) error {
return server.router.Run(address)
}

func errorResponse(err error) gin.H {
return gin.H{"error": err.Error()}
}
85 changes: 85 additions & 0 deletions api/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package api

import (
"database/sql"
"net/http"

"github.com/gin-gonic/gin"

db "github.com/broemp/broempSignal/db/sqlc"
)

type createUserRequest struct {
Username string `json:"username" binding:"required"`
DiscordId int64 `json:"discordid" binding:"required"`
}

func (server *Server) createUser(ctx *gin.Context) {
var req createUserRequest
err := ctx.ShouldBindJSON(&req)
if err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

arg := db.CreateUserParams{
Username: req.Username,
Discordid: req.DiscordId,
}

user, err := server.store.CreateUser(ctx, arg)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
}

ctx.JSON(http.StatusOK, user)
}

type getUserRequest struct {
ID int64 `uri:"id" binding:"required,min=1"`
}

func (server *Server) getUser(ctx *gin.Context) {
var req getUserRequest
if err := ctx.ShouldBindUri(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

user, err := server.store.GetUser(ctx, req.ID)
if err != nil {
if err == sql.ErrNoRows {
ctx.JSON(http.StatusNotFound, errorResponse(err))
return
}
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, user)
}

type listUserRequest struct {
PageId int32 `form:"page_id" binding:"required,min=1"`
PageSize int32 `form:"page_size" binding:"required,min=5,max=10"`
}

func (server *Server) listUser(ctx *gin.Context) {
var req listUserRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
ctx.JSON(http.StatusBadRequest, errorResponse(err))
return
}

arg := db.ListUsersParams{
Limit: req.PageSize,
Offset: (req.PageId - 1) * req.PageSize,
}
users, err := server.store.ListUsers(ctx, arg)
if err != nil {
ctx.JSON(http.StatusInternalServerError, errorResponse(err))
return
}

ctx.JSON(http.StatusOK, users)
}
13 changes: 13 additions & 0 deletions broempSignal/friends.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package broempsignal

func addFriend(userid, friendid string) error {
return nil
}

func deleteFriend(userid, friendid string) error {
return nil
}

func listFriends(userid string) error {
return nil
}
Loading

0 comments on commit 248bc54

Please sign in to comment.