From 9a8ac55819c0cdb5e9c7c4ba72699d842a4892e9 Mon Sep 17 00:00:00 2001 From: lucy Date: Wed, 24 Apr 2024 11:10:38 +0200 Subject: [PATCH] add project skeleton example --- .gitignore | 2 + go.mod | 5 +++ go.sum | 20 ++++++++++ main.go | 26 +++++++++++++ src/config/config.go | 72 ++++++++++++++++++++++++++++++++++++ src/database/db.go | 1 + src/database/migrations.go | 1 + src/database/objects.go | 1 + src/encryption/encryption.go | 1 + src/event/scheduler.go | 1 + src/script/generic.go | 1 + src/script/prolog.go | 1 + src/web/ap.go | 1 + src/web/mastoapi.go | 1 + src/web/router.go | 1 + src/web/webfinger.go | 1 + 16 files changed, 136 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 src/config/config.go create mode 100644 src/database/db.go create mode 100644 src/database/migrations.go create mode 100644 src/database/objects.go create mode 100644 src/encryption/encryption.go create mode 100644 src/event/scheduler.go create mode 100644 src/script/generic.go create mode 100644 src/script/prolog.go create mode 100644 src/web/ap.go create mode 100644 src/web/mastoapi.go create mode 100644 src/web/router.go create mode 100644 src/web/webfinger.go diff --git a/.gitignore b/.gitignore index 6d13ad4..f4b3de7 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ go.work *.kdev4 .kdev4/ +# the linux executable +server diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2a15a97 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.0x0.st/ap/server + +go 1.22.2 + +require github.com/pelletier/go-toml/v2 v2.2.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..02a16d6 --- /dev/null +++ b/go.sum @@ -0,0 +1,20 @@ +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/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg= +github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +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= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..357ec2e --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "git.0x0.st/ap/server/src/config" + _"git.0x0.st/ap/server/src/database" + _"git.0x0.st/ap/server/src/event" + _"git.0x0.st/ap/server/src/script" + _"git.0x0.st/ap/server/src/encryption" + _"git.0x0.st/ap/server/src/web" + "fmt" + "log" + "os" +) + + +/* +this file acts as a bootstrapper for the server. +load config, init stuff, start webrouter etc etc. +*/ +func main() { + cfg, err := config.LoadConfig(os.Getenv("HOME") + "/.ap/config.toml") + if err!= nil { + log.Fatal("failed to load config") + } + fmt.Println("Starting", cfg.Instance.Name, ", your personal", cfg.Instance.Software, "instance") +} diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 0000000..65d3ccc --- /dev/null +++ b/src/config/config.go @@ -0,0 +1,72 @@ +package config + +import ( + "github.com/pelletier/go-toml/v2" + "os" + "io/ioutil" + "log" +) + +type InstanceConfig struct { + Name string `toml:"name"` + Description string `toml:"description"` + Domain string `toml:"domain"` + Software string `toml:"software"` +} +type DatabaseConfig struct { + Host string `toml:"host"` + User string `toml:"user"` + Pass string `toml:"pass"` + DB string `toml:"db"` +} + +type Config struct { + IsDefault bool `toml:"-"` + Instance InstanceConfig `toml:"instance"` + Database DatabaseConfig `toml:"database"` +} + +/* +load config by path (filename), +if specified file does not exist, return the default config as hardcoded below. +*/ +func LoadConfig(path string) (Config, error) { + // always have default config! + cfg := Config{ + IsDefault : true, + Instance : InstanceConfig{ + Name : "my-instance", + Description : "let's fucking goooo!", + Domain : "my.instance", + Software : "my-software", + }, + Database : DatabaseConfig{ + Host : "localhost", + User : "user", + Pass : "pass", + DB : "my-db", + }, + } + if _, err := os.Stat(path); os.IsNotExist(err) { + // return default config file + log.Println("WARNING: no config file. using defaults only") + return cfg, nil + } else { + // load existing config file + file, err := os.Open(path) + if err!=nil { + return cfg, err + } + defer file.Close() + content, err := ioutil.ReadAll(file) + if err!=nil { + return cfg, err + } + err = toml.Unmarshal(content, &cfg) + if err!=nil { + cfg.IsDefault = false + return cfg, err + } + return cfg, nil + } +} diff --git a/src/database/db.go b/src/database/db.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/src/database/db.go @@ -0,0 +1 @@ +package database diff --git a/src/database/migrations.go b/src/database/migrations.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/src/database/migrations.go @@ -0,0 +1 @@ +package database diff --git a/src/database/objects.go b/src/database/objects.go new file mode 100644 index 0000000..636bab8 --- /dev/null +++ b/src/database/objects.go @@ -0,0 +1 @@ +package database diff --git a/src/encryption/encryption.go b/src/encryption/encryption.go new file mode 100644 index 0000000..909a131 --- /dev/null +++ b/src/encryption/encryption.go @@ -0,0 +1 @@ +package encryption diff --git a/src/event/scheduler.go b/src/event/scheduler.go new file mode 100644 index 0000000..0e4b82e --- /dev/null +++ b/src/event/scheduler.go @@ -0,0 +1 @@ +package event diff --git a/src/script/generic.go b/src/script/generic.go new file mode 100644 index 0000000..4759160 --- /dev/null +++ b/src/script/generic.go @@ -0,0 +1 @@ +package script diff --git a/src/script/prolog.go b/src/script/prolog.go new file mode 100644 index 0000000..4759160 --- /dev/null +++ b/src/script/prolog.go @@ -0,0 +1 @@ +package script diff --git a/src/web/ap.go b/src/web/ap.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/src/web/ap.go @@ -0,0 +1 @@ +package web diff --git a/src/web/mastoapi.go b/src/web/mastoapi.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/src/web/mastoapi.go @@ -0,0 +1 @@ +package web diff --git a/src/web/router.go b/src/web/router.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/src/web/router.go @@ -0,0 +1 @@ +package web diff --git a/src/web/webfinger.go b/src/web/webfinger.go new file mode 100644 index 0000000..efb3895 --- /dev/null +++ b/src/web/webfinger.go @@ -0,0 +1 @@ +package web