Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mytja committed Nov 6, 2024
0 parents commit f3c7ea7
Show file tree
Hide file tree
Showing 14 changed files with 1,043 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Server
on:
- push

jobs:
build-linux:
name: Linux build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '^1.23'
- name: Get current Go version
run: go version
- name: Get Go dependencies
run: go mod download
- name: Set env
run: go env -w GOFLAGS=-mod=mod
- name: Go get
run: go get -v .
- name: Build app
run: go build -v -o backend main.go
- uses: actions/upload-artifact@v4
with:
name: backend-linux
path: backend
docker:
name: Docker build
needs: build-linux
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Get Docker version
run: docker --version
- name: Docker Login
uses: docker/login-action@v3
with:
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}
registry: "ghcr.io"
- name: Downcase repository owner
run: |
echo REPO=$(echo ${{github.repository_owner}} | tr '[:upper:]' '[:lower:]') >> $GITHUB_ENV
- name: Build Docker image
uses: docker/build-push-action@v3
with:
context: "."
file: "./Dockerfile"
tags: ghcr.io/${{env.REPO}}/sharepoint-bot:latest
push: true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
database.sqlite3
database
.idea
config.json
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM golang:1.23-alpine AS builder

COPY . /app

WORKDIR /app

# Add gcc
RUN apk add build-base tzdata

RUN go mod download && \
go env -w GOFLAGS=-mod=mod && \
go get . && \
go build -v -o backend .

FROM alpine:latest

WORKDIR /app
COPY --from=builder /app/backend ./backend

CMD [ "./backend" ]
1 change: 1 addition & 0 deletions config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"database_name":"sqlite3","database_config":"database/database.sqlite3","debug":true,"ms_oauth2_client_id":"","ms_oauth2_secret":"","ms_oauth2_refresh_token":"","webhooks":["https://discord.com/api/webhooks/channelId/botToken"]}
67 changes: 67 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package config

import (
"encoding/json"
"os"
)

type Config struct {
DatabaseName string `json:"database_name"`
DatabaseConfig string `json:"database_config"`
Debug bool `json:"debug"`
MicrosoftOAUTH2ClientID string `json:"ms_oauth2_client_id"`
MicrosoftOAUTH2Secret string `json:"ms_oauth2_secret"`
MicrosoftOAUTH2RefreshToken string `json:"ms_oauth2_refresh_token"`
Webhooks []string `json:"webhooks"`
}

func GetConfig() (Config, error) {
var config Config
file, err := os.ReadFile("config.json")
if err != nil {
marshal, err := json.Marshal(Config{
DatabaseName: "sqlite3",
DatabaseConfig: "database.sqlite3",
Debug: true,
MicrosoftOAUTH2ClientID: "",
MicrosoftOAUTH2RefreshToken: "",
MicrosoftOAUTH2Secret: "",
Webhooks: make([]string, 0),
})
if err != nil {
return config, err
}
err = os.WriteFile("config.json", marshal, 0600)
if err != nil {
return config, err
}
file, err = os.ReadFile("config.json")
if err != nil {
return config, err
}
}
err = json.Unmarshal(file, &config)
if err != nil {
return config, err
}
return config, err
}

func SaveConfig(config Config) error {
marshal, err := json.Marshal(config)
if err != nil {
return err
}
f, err := os.Create("config.json")
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
err = os.WriteFile("config.json", marshal, 0600)
if err != nil {
return err
}
return nil
}
16 changes: 16 additions & 0 deletions db/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package db

const schema string = `
CREATE TABLE IF NOT EXISTS sharepoint_notifications (
id VARCHAR(60) PRIMARY KEY,
name VARCHAR,
description VARCHAR,
created_on INTEGER,
modified_on INTEGER,
message_ids JSON,
created_by VARCHAR(100),
modified_by VARCHAR(100),
expires_on INTEGER,
has_attachments BOOLEAN
);
`
71 changes: 71 additions & 0 deletions db/sharepoint_notifications.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package db

type SharepointNotification struct {
ID string `db:"id"`
Name string `db:"name"`
Description string `db:"description"`
CreatedOn int `db:"created_on"`
ModifiedOn int `db:"modified_on"`
CreatedBy string `db:"created_by"`
ModifiedBy string `db:"modified_by"`
MessageIDs string `db:"message_ids"`
ExpiresOn int `db:"expires_on"`
HasAttachments bool `db:"has_attachments"`
}

func (db *sqlImpl) GetSharepointNotification(id string) (notification SharepointNotification, err error) {
err = db.db.Get(&notification, "SELECT * FROM sharepoint_notifications WHERE id=$1", id)
return notification, err
}

func (db *sqlImpl) GetSharepointNotifications() (notification []SharepointNotification, err error) {
err = db.db.Select(&notification, "SELECT * FROM sharepoint_notifications ORDER BY modified_on ASC")
return notification, err
}

func (db *sqlImpl) InsertSharepointNotification(notification SharepointNotification) (err error) {
_, err = db.db.NamedExec(
`INSERT INTO sharepoint_notifications
(id,
name,
description,
created_on,
modified_on,
created_by,
modified_by,
message_ids,
expires_on,
has_attachments)
VALUES (:id,
:name,
:description,
:created_on,
:modified_on,
:created_by,
:modified_by,
:message_ids,
:expires_on,
:has_attachments)
`, notification)
return err
}

func (db *sqlImpl) UpdateSharepointNotification(notification SharepointNotification) error {
_, err := db.db.NamedExec(
`UPDATE sharepoint_notifications SET
name=:name,
description=:description,
modified_on=:modified_on,
modified_by=:modified_by,
message_ids=:message_ids,
expires_on=:expires_on,
has_attachments=:has_attachments
WHERE id=:id`,
notification)
return err
}

func (db *sqlImpl) DeleteSharepointNotification(id string) error {
_, err := db.db.Exec(`DELETE FROM sharepoint_notifications WHERE id=$1`, id)
return err
}
35 changes: 35 additions & 0 deletions db/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package db

import (
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
)

type sqlImpl struct {
db *sqlx.DB
logger *zap.SugaredLogger
}

func (db *sqlImpl) Init() {
db.db.MustExec(schema)
}

type SQL interface {
Init()

GetSharepointNotification(id string) (notification SharepointNotification, err error)
GetSharepointNotifications() (notification []SharepointNotification, err error)
InsertSharepointNotification(notification SharepointNotification) (err error)
UpdateSharepointNotification(notification SharepointNotification) error
DeleteSharepointNotification(id string) error
}

func NewSQL(driver string, drivername string, logger *zap.SugaredLogger) (SQL, error) {
db, err := sqlx.Connect(driver, drivername)
return &sqlImpl{
db: db,
logger: logger,
}, err
}
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'
services:
backend:
image: ghcr.io/bezidev/sharepoint-bot
volumes:
- ./config.json:/app/config.json
- ./database:/app/database
environment:
- TZ=Europe/Ljubljana
restart: always
extra_hosts:
- "host.docker.internal:host-gateway"
35 changes: 35 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module SharepointBot

go 1.23.2

require (
github.com/JohannesKaufmann/html-to-markdown v1.6.0 // indirect
github.com/PuerkitoBio/goquery v1.9.2 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/cloudflare/circl v1.4.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/pprof v0.0.0-20240910150728-a0b0bb1d4134 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/imroc/req/v3 v3.48.0 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/onsi/ginkgo/v2 v2.20.2 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.47.0 // indirect
github.com/refraction-networking/utls v1.6.7 // indirect
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/tools v0.25.0 // indirect
)
Loading

0 comments on commit f3c7ea7

Please sign in to comment.