Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# fmsgd Environment Variables

# Required
FMSG_DATA_DIR=/opt/fmsg/data
FMSG_DOMAIN=example.com
FMSG_ID_URL=http://127.0.0.1:8080

# Optional
FMSG_MAX_MSG_SIZE=10240
FMSG_PORT=36900
FMSG_MAX_PAST_TIME_DELTA=604800
FMSG_MAX_FUTURE_TIME_DELTA=300
FMSG_MIN_DOWNLOAD_RATE=5000
FMSG_MIN_UPLOAD_RATE=5000
FMSG_READ_BUFFER_SIZE=1600

# PostgreSQL connection variables (see https://www.postgresql.org/docs/current/libpq-envars.html)
PGHOST=
PGPORT=
PGUSER=
PGPASSWORD=
PGDATABASE=
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: go 1.19
name: Go 1.25 Build and Test

on:
push:
Expand All @@ -19,7 +19,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.25'

- name: Build
run: go build -v .
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
.claude/
*.exe
.env
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fmsgd is still in development and hasn't reached v1.0.0 yet, PRs are welcome!
PRs are welcome!

Please see TODOs in code for known issues.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# fmsgd

[![go 1.19](https://github.com/markmnl/fmsgd/actions/workflows/go1.19.yml/badge.svg)](https://github.com/markmnl/fmsgd/actions/workflows/go1.19.yml)
[![Go 1.25](https://github.com/markmnl/fmsgd/actions/workflows/go1.25.yml/badge.svg)](https://github.com/markmnl/fmsgd/actions/workflows/go1.25.yml)


Implementation of [fmsg](https://github.com/markmnl/fmsg) host written in go! Uses local filesystem and PostgreSQL database to store messages per the [fmsg-store-postgres standard](https://github.com/markmnl/fmsg/blob/main/STANDARDS.md).

## Building from source

Tested with go 1.18, 1.19 and 1.20 on Linux (AMD64, ARM64) and Windows AMD64
Tested with go 1.25 on Linux (AMD64, ARM64) and Windows AMD64

1. Clone this repository
2. Navigate to src/
Expand Down
80 changes: 40 additions & 40 deletions src/dd.sql → dd.sql
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
/****************************************************************
*
* PostgreSQL database objects data definition for fmsgd
*
****************************************************************/
-- database with encoding UTF8 should already be created and connected
create table if not exists msg (
id bigserial primary key,
version int not null,
pid bigint references msg (id),
flags int not null,
time_sent double precision not null, -- time sending host recieved message for sending, message timestamp field
time_recv double precision not null, -- time recieving host downloaded the message
from_addr varchar(255) not null,
topic varchar(255) not null,
type varchar(255) not null,
sha256 bytea unique not null,
psha256 bytea,
size int not null, -- spec allows uint32 but we don't enforced by FMSG_MAX_MSG_SIZE
filepath text not null
);
create index on msg ((lower(from_addr)));
create table if not exists msg_to (
msg_id bigint not null,
addr varchar(255) not null,
status shortint
primary key (msg_id, addr)
);
create index on msg_to ((lower(addr)));
create table if not exists msg_attachment (
msg_id bigint references msg (id),
filename varchar(255) not null,
filesize int not null,
filepath text not null,
primary key (msg_id, filename)
);
/****************************************************************
*
* PostgreSQL database objects data definition for fmsgd
*
****************************************************************/

-- database with encoding UTF8 should already be created and connected

create table if not exists msg (
id bigserial primary key,
version int not null,
pid bigint references msg (id),
flags int not null,
time_sent double precision not null, -- time sending host recieved message for sending, message timestamp field
time_recv double precision not null, -- time recieving host downloaded the message
from_addr varchar(255) not null,
topic varchar(255) not null,
type varchar(255) not null,
sha256 bytea unique not null,
psha256 bytea,
size int not null, -- spec allows uint32 but we don't enforced by FMSG_MAX_MSG_SIZE
filepath text not null
);
create index on msg ((lower(from_addr)));

create table if not exists msg_to (
msg_id bigint references msg (id),
addr varchar(255) not null,
status smallint,
primary key (msg_id, addr)
);
create index on msg_to ((lower(addr)));

create table if not exists msg_attachment (
msg_id bigint references msg (id),
filename varchar(255) not null,
filesize int not null,
filepath text not null,
primary key (msg_id, filename)
);
5 changes: 3 additions & 2 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/markmnl/fmsgd

go 1.18
go 1.25

require (
github.com/caitlinelfring/go-env-default v1.1.0
github.com/joho/godotenv v1.5.1
github.com/levenlabs/golib v0.0.0-20180911183212-0f8974794783
github.com/lib/pq v1.10.7
github.com/lib/pq v1.10.9
)

require (
Expand Down
6 changes: 4 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ github.com/caitlinelfring/go-env-default v1.1.0/go.mod h1:tESXPr8zFPP/cRy3cwxrHB
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/levenlabs/golib v0.0.0-20180911183212-0f8974794783 h1:ErBsqZpyadTBr2zGsgMau0JdyghyLdwRRHAKJukexrQ=
github.com/levenlabs/golib v0.0.0-20180911183212-0f8974794783/go.mod h1:zw8z7nRRkGDZHexz1aMbZGtwxli5so0CBVZeIa3G+RE=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
18 changes: 18 additions & 0 deletions src/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"unicode/utf8"

env "github.com/caitlinelfring/go-env-default"
"github.com/joho/godotenv"
"github.com/levenlabs/golib/timeutil"
)

Expand Down Expand Up @@ -119,6 +120,18 @@ func calcNetIODuration(sizeInBytes int, bytesPerSecond float64) time.Duration {
return d
}

// getDbConnString returns a PostgreSQL connection string using environment variables
// with sslmode=disable to support non-SSL connections
func getDbConnString() string {
connStr := "host=" + os.Getenv("PGHOST") +
" port=" + os.Getenv("PGPORT") +
" user=" + os.Getenv("PGUSER") +
" password=" + os.Getenv("PGPASSWORD") +
" dbname=" + os.Getenv("PGDATABASE") +
" sslmode=disable"
return connStr
}

func parseAddress(b []byte) (*FMsgAddress, error) {
// TODO validate length
var addr = &FMsgAddress{}
Expand Down Expand Up @@ -530,6 +543,11 @@ func main() {

log.SetPrefix("fmsgd: ")

// load environment variables from .env file if present
if err := godotenv.Load(); err != nil {
log.Printf("INFO: Could not load .env file: %v", err)
}

// get listen address from args
if len(os.Args) < 2 {
log.Fatalf("ERROR: Listen address is required as an argument, e.g.: \"0.0.0.0\"\n")
Expand Down
4 changes: 2 additions & 2 deletions src/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func testDb() error {
db, err := sql.Open("postgres", "")
db, err := sql.Open("postgres", getDbConnString())
if err != nil {
return err
}
Expand All @@ -23,7 +23,7 @@ func testDb() error {

func storeMsgDetail(msg *FMsgHeader) error {

db, err := sql.Open("postgres", "")
db, err := sql.Open("postgres", getDbConnString())
if err != nil {
return err
}
Expand Down