forked from niveklabs/go-uikit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (107 loc) · 2.77 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
//go:build !wasm
// +build !wasm
package main
import (
"context"
"fmt"
"net/http"
"os"
"syscall"
"github.com/maxence-charriere/go-app/v9/pkg/app"
"github.com/maxence-charriere/go-app/v9/pkg/cli"
"github.com/maxence-charriere/go-app/v9/pkg/errors"
"github.com/maxence-charriere/go-app/v9/pkg/logs"
)
type localOptions struct {
Port int `cli:"p" env:"GO_UIKIT_PORT" help:"The port used by the server that serves the PWA."`
}
type githubOptions struct {
Output string `cli:"o" env:"-" help:"The directory where static resources are saved."`
}
func main() {
ctx, cancel := cli.ContextWithSignals(context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer cancel()
defer exit()
localOpts := localOptions{Port: 7777}
cli.Register("local").
Help(`Launches a server that serves the app in a local environment.`).
Options(&localOpts)
githubOpts := githubOptions{}
cli.Register("github").
Help(`Generates the required resources to run the app on GitHub Pages.`).
Options(&githubOpts)
backgroundColor := "#ffffff"
h := app.Handler{
Author: "Nivek Labs",
BackgroundColor: backgroundColor,
Description: "A example application.",
Keywords: []string{
"go-app",
"uikit",
"pwa",
},
LoadingLabel: "Loading Go-UIkit...",
Name: "GO-UIkit",
Icon: app.Icon{
Default: "/web/images/android-chrome-192x192.png",
Large: "/web/images/android-chrome-512x512.png",
AppleTouch: "/web/images/apple-touch-icon.png",
},
Scripts: []string{
"/web/js/uikit.min.js",
"/web/js/uikit-icons.min.js",
"/web/js/prism.js",
},
Styles: []string{
"https://fonts.googleapis.com/css2?family=Fira+Code:wght@300;400;700&family=Montserrat:wght@300;400;700&display=swap",
"/web/css/uikit.min.css",
"/web/css/prism.css",
"/web/css/app.css",
},
ThemeColor: backgroundColor,
Title: "GO-UIkit",
}
switch cli.Load() {
case "local":
runLocal(ctx, &h, localOpts)
case "github":
generateGitHubPages(ctx, &h, githubOpts)
}
}
func runLocal(ctx context.Context, h *app.Handler, opts localOptions) {
app.Log("%s", logs.New("starting GO-UIkit service").
WithTag("port", opts.Port).
WithTag("version", h.Version),
)
s := http.Server{
Addr: fmt.Sprintf(":%v", opts.Port),
Handler: h,
}
go func() {
<-ctx.Done()
s.Shutdown(context.Background())
}()
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}
func generateGitHubPages(ctx context.Context, h *app.Handler, opts githubOptions) {
pages := pages()
p := make([]string, 0, len(pages))
for path := range pages {
p = append(p, path)
}
if err := app.GenerateStaticWebsite(opts.Output, h, p...); err != nil {
panic(err)
}
}
func exit() {
err := recover()
if err != nil {
app.Log("command failed: %s", errors.Newf("%v", err))
os.Exit(-1)
}
}