summaryrefslogtreecommitdiff
path: root/main.go
blob: 2a1a46b57e6446cc831fa6c3d9e3aa095bf50fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main

import (
	"flag"
	"log"
	"math/rand"
	"os"
	"os/signal"
	"random.chars.jp/git/image-board/v2/backend/filesystem"
	"random.chars.jp/git/image-board/v2/store"
	"syscall"
	"time"
)

var instance store.Store

func init() {
	rand.Seed(time.Now().UnixNano())
}

func main() {
	flag.Parse()

	confLoad()

	// TODO: support more backends
	instance = filesystem.New(config.System.Store, config.System.Verbose)
	if err := instance.Open(); err != nil {
		log.Printf("error opening store: %s", err)
		return
	} else {
		log.Printf("store path %s revision %v compat %v",
			config.System.Store, instance.(*filesystem.Store).Revision, instance.(*filesystem.Store).Compat)
	}

	if info, err := instance.User(instance.UserInitial()); err == nil {
		log.Printf("initial user ID %s secret %s.", info.Snowflake, info.Secret)

		var p bool
		if p, err = instance.UserPasswordValidate(&info.Snowflake, nil, store.InitialPassword); err != nil {
			log.Fatalf("error validating password of initial user: %s", err)
		} else if p {
			log.Printf("warning: initial user still has the password \"%s\"", store.InitialPassword)
		}
	} else {
		if config.System.SingleUser {
			log.Fatal("no initial user found, single user mode unavailable")
		}
	}
	if config.System.SingleUser {
		log.Print("server running single-user, all operations performed as initial user")
	} else if config.System.Private {
		log.Print("server in private mode, all operations require authentication")
	}

	webSetup()

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
	go func() {
		defer func() { cleanup() }()
		for {
			s := <-sig
			switch s {
			case os.Interrupt:
				println()
				log.Print("shutting down")
				return
			default:
				log.Print("shutting down")
				return
			}
		}
	}()

	serve()
}