97 lines
1.7 KiB
Go
97 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
//go:embed html/index.tmpl
|
|
var indexTemplate string
|
|
|
|
//go:embed html/entry.tmpl
|
|
var entryTemplate string
|
|
|
|
//go:embed html/styles.css
|
|
var nativeCSS string
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
var configuration BlogConfiguration
|
|
args := os.Args[1:]
|
|
|
|
home := os.Getenv("HOME")
|
|
err = os.MkdirAll(fmt.Sprintf("%s/.config/", home), 0600)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
configurationFileName := fmt.Sprintf("%s/.config/mailblog.json", home)
|
|
|
|
file, err := os.Open(configurationFileName)
|
|
if err == nil {
|
|
defer file.Close()
|
|
decoder := json.NewDecoder(file)
|
|
err = decoder.Decode(&configuration)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
if len(args) > 0 {
|
|
switch args[0] {
|
|
case "configure":
|
|
configuration.Prompt()
|
|
file, err = os.Create(fmt.Sprintf("%s/.config/mailblog.json", home))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
encoder := json.NewEncoder(file)
|
|
err = encoder.Encode(configuration)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Printf("Configuration save in %s/.config/mailblob.json\n", home)
|
|
return
|
|
}
|
|
}
|
|
|
|
var blog Blog
|
|
blog.Lang = "fr-FR"
|
|
blog.Title = configuration.Title
|
|
go MailboxFetcher(configuration, &blog)
|
|
StartServer(configuration, &blog)
|
|
}
|
|
|
|
func MailboxFetcher(configuration BlogConfiguration, blog *Blog) {
|
|
|
|
var mb MailBox
|
|
mb.Configure(&configuration.MailBox)
|
|
|
|
for {
|
|
err := mb.Connect()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
posts, err := mb.GetMessages()
|
|
if err != nil {
|
|
log.Println(err)
|
|
mb.Close()
|
|
time.Sleep(10 * time.Second)
|
|
continue
|
|
}
|
|
mb.Close()
|
|
|
|
blog.mutex.Lock()
|
|
blog.Posts = posts
|
|
blog.mutex.Unlock()
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
}
|