aboutsummaryrefslogtreecommitdiff
path: root/space.go
blob: 94ddeb9e66a4b23e80f4963074d546753554a023 (plain) (blame)
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
// Copyright (C) 2021-2021 Fuwn
// SPDX-License-Identifier: GPL-3.0-only

package main

import (
	"embed"
	"io/fs"
	"log"
	"strings"
	"text/template"

	"github.com/fuwn/space/pkg/utilities"
	"github.com/pitr/gig"
	"github.com/spf13/viper"
)

//go:embed content
var contentFilesystem embed.FS

var g = gig.Default()

var hitsTracker = make(map[string]int)

// var startTime = time.Now()

// Initialize templates
func init() {
	templates, _ := fs.Sub(contentFilesystem, "content/templates")
	g.Renderer = &Template{template.Must(template.New("").ParseFS(templates, "*.gmi"))}
}

// Initialize configuration system
func init() {
	viper.SetConfigName("config.yml")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(".space/")
	viper.AddConfigPath(".space-data/")
	viper.AddConfigPath("/app/.space/")

	if err := viper.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			log.Panicln("Cannot read configuration file:", err)
		} else {
			log.Panicln("Read configuration file but an error occurred anyway:", err)
		}
	}

	viper.WatchConfig()
}

func main() {
	// Route handler
	handle()

	// Certificate check
	nonExistent := utilities.DoesFilesExist([]string{
		".space/.certificates/space.crt",
		".space/.certificates/space.key",
	})
	if len(nonExistent) != 0 {
		panic("The following files crucial to execution DO NOT exist: " + strings.Join(nonExistent, ", "))
	}

	// Start
	g.Run(
		":"+viper.GetString("space.port"),
		".space/.certificates/space.crt",
		".space/.certificates/space.key",
	)
}