mirror of
https://git.mills.io/saltyim/saltyim.git
synced 2024-06-30 18:51:03 +00:00
![James Mills](/assets/img/avatar_default.png)
Co-authored-by: James Mills <prologic@shortcircuit.net.au> Reviewed-on: https://git.mills.io/prologic/saltyim/pulls/43
48 lines
950 B
Go
48 lines
950 B
Go
package internal
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.mills.io/saltyim"
|
|
)
|
|
|
|
const (
|
|
wellknownPath = ".well-known/salty"
|
|
)
|
|
|
|
func CreateConfig(conf *Config, addr saltyim.Addr, key string) error {
|
|
p := filepath.Join(conf.Data, wellknownPath)
|
|
fn := filepath.Join(p, fmt.Sprintf("%s.json", addr.Hash()))
|
|
|
|
if err := os.MkdirAll(p, 0755); err != nil {
|
|
return fmt.Errorf("error creating config paths %s: %w", p, err)
|
|
}
|
|
|
|
ulid, err := saltyim.GenerateULID()
|
|
if err != nil {
|
|
return fmt.Errorf("error generating ulid")
|
|
}
|
|
|
|
endpointURL := *conf.baseURL
|
|
endpointURL.Path = fmt.Sprintf("/inbox/%s", ulid)
|
|
|
|
config := saltyim.Config{
|
|
Endpoint: endpointURL.String(),
|
|
Key: key,
|
|
}
|
|
|
|
data, err := json.Marshal(config)
|
|
if err != nil {
|
|
return fmt.Errorf("error serializing config")
|
|
}
|
|
|
|
if err := os.WriteFile(fn, data, 0644); err != nil {
|
|
return fmt.Errorf("error writing config to %s: %w", fn, err)
|
|
}
|
|
|
|
return nil
|
|
}
|