-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
130 lines (107 loc) · 3.21 KB
/
main.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"embed"
"flag"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"os"
"path"
"log/slog"
"github.com/go-chi/chi"
auth "github.com/joshuaschlichting/gocms/auth/cognito"
"github.com/joshuaschlichting/gocms/auth/kratos"
"github.com/joshuaschlichting/gocms/config"
"github.com/joshuaschlichting/gocms/filesystem"
"github.com/joshuaschlichting/gocms/internal/apps/cms/admin"
"github.com/joshuaschlichting/gocms/internal/apps/cms/api"
"github.com/joshuaschlichting/gocms/internal/apps/cms/blog"
"github.com/joshuaschlichting/gocms/manager"
"github.com/joshuaschlichting/gocms/middleware"
_ "github.com/lib/pq"
)
//go:embed static
var fileSystem embed.FS
//go:embed internal/apps
var templateFS embed.FS
//go:embed config.yml
var configYml []byte
//go:embed internal/apps/cms/data/db/sql
var sqlFS embed.FS
var logger *slog.Logger
func init() {
}
func main() {
var (
host = flag.String("host", "", "host http address to listen on")
port = flag.String("port", "8000", "port number for http listener")
debugFlag = flag.Bool("debug", false, "debug logging mode")
)
// Set up logging
flag.Parse()
var programLevel = new(slog.LevelVar)
if *debugFlag {
programLevel.Set(slog.LevelDebug)
} else {
programLevel.Set(slog.LevelInfo)
}
// Set up logger
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: programLevel}))
api.SetLogger(logger)
admin.SetLogger(logger)
blog.SetLogger(logger)
auth.SetLogger(logger)
kratos.SetLogger(logger)
config := config.LoadConfig(readConfigFile())
if manager.HandleIfManagerCall(*config, sqlFS) {
logger.Info("Manager program call finished...")
// This was a call to the manager program, not the web site executable
return
}
// Add template functions
funcMap := commonFuncMap
// Load templates
logger.Info("Loading templates...")
templ, err := parseTemplateDir("internal/apps", templateFS, funcMap)
if err != nil {
errMsg := fmt.Sprintf("error parsing templates: %v", err)
logger.Error(errMsg)
log.Fatalf(errMsg)
}
logger.Info(fmt.Sprintf("Starting server on port: %v", *port))
// Create the router
r := chi.NewRouter()
// Init Kratos auth service
cmsConfig := (*config)["cms"]
kratos.InitKratos(&cmsConfig)
// Create file system for content delivery
homeDir, _ := os.UserHomeDir()
gocmsPath := path.Join(homeDir, "gocms")
logger.Info(fmt.Sprintf("Using the following gocmsPath for local filesystem: %s", gocmsPath))
localFS := filesystem.NewLocalFilesystem(gocmsPath)
// Register common middleware with the router
r.Use(middleware.LogAllButStaticRequests)
registerApps(r, templ, *config, *localFS)
// Register static file serve
staticFS, _ := fs.Sub(fileSystem, "static")
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
// Start server
addr := net.JoinHostPort(*host, *port)
if err := listenServe(addr, r); err != nil {
log.Fatal(err)
}
}
func listenServe(listenAddr string, handler http.Handler) error {
s := http.Server{
Addr: listenAddr,
Handler: handler, // Our own instance of servemux
}
logger.Debug(fmt.Sprintf("Starting HTTP listener at %s", listenAddr))
return s.ListenAndServe()
}
func readConfigFile() []byte {
// read config.yml
return configYml
}