-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
executable file
·155 lines (131 loc) · 4.5 KB
/
app.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"embed"
"flag"
"github.com/gorilla/mux"
"html/template"
"jinya-fonts/admin"
"jinya-fonts/api"
"jinya-fonts/config"
"jinya-fonts/fontsync"
http2 "jinya-fonts/http"
"jinya-fonts/utils"
"log"
"mime"
"net/http"
"os"
"strings"
)
var (
//go:embed frontend
frontend embed.FS
//go:embed angular/frontend/dist/browser
angular embed.FS
//go:embed static
static embed.FS
//go:embed admin/static
adminStatic embed.FS
pages = map[string]string{
"/": "frontend/index.gohtml",
"/font": "frontend/font.gohtml",
}
)
func getWebApp(w http.ResponseWriter, r *http.Request) {
page, ok := pages[r.URL.Path]
if !ok {
w.WriteHeader(http.StatusNotFound)
return
}
tpl, err := template.New("layout").ParseFS(frontend, "frontend/layout.gohtml", page)
if err != nil {
log.Printf("page %s not found in pages cache...", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
if err := tpl.Execute(w, nil); err != nil {
return
}
}
func getAngularStatic(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/v3/")
var data []byte
var err error
if strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".css") || strings.HasSuffix(path, ".png") || strings.HasSuffix(path, ".ico") {
path = strings.TrimPrefix(path, "static/")
data, err = angular.ReadFile("angular/frontend/dist/browser/" + path)
if err != nil {
log.Printf("page %s not found in pages cache...", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
} else {
data, err = angular.ReadFile("angular/frontend/dist/browser/index.html")
if err != nil {
log.Printf("page %s not found in pages cache...", r.RequestURI)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
lastIdx := strings.LastIndex(path, ".")
extension := "text/html"
if lastIdx > 0 {
extension = path[lastIdx:len(path)]
}
w.Header().Set("Content-Type", mime.TypeByExtension(extension))
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func main() {
configFileFlag := flag.String("config-file", "./config.yaml", "The config file, check the sample for the structure")
flag.Parse()
configuration, err := config.LoadConfiguration(*configFileFlag)
if err != nil {
panic(err)
}
if utils.ContainsString(os.Args, "sync") {
err = fontsync.Sync(configuration)
if err != nil {
panic(err)
}
}
if utils.ContainsString(os.Args, "serve") {
router := mux.NewRouter()
router.PathPrefix("/fonts/").Handler(http.StripPrefix("/fonts", http.FileServer(http.Dir(configuration.FontFileFolder))))
router.HandleFunc("/css2", http2.GetCss2)
if configuration.AdminPassword != "" {
router.HandleFunc("/login", admin.Login)
router.HandleFunc("/logout", admin.Logout)
router.HandleFunc("/admin", admin.CheckAuthCookie(admin.AllFonts))
router.HandleFunc("/admin/add", admin.CheckAuthCookie(admin.AddFont))
router.HandleFunc("/admin/edit", admin.CheckAuthCookie(admin.EditFont))
router.HandleFunc("/admin/delete", admin.CheckAuthCookie(admin.DeleteFont))
router.HandleFunc("/admin/sync", admin.CheckAuthCookie(admin.TriggerSync))
router.HandleFunc("/admin/synced", admin.CheckAuthCookie(admin.SyncedFonts))
router.HandleFunc("/admin/custom", admin.CheckAuthCookie(admin.CustomFonts))
router.HandleFunc("/admin/designers", admin.CheckAuthCookie(admin.DesignersIndex))
router.HandleFunc("/admin/designers/delete", admin.CheckAuthCookie(admin.DeleteDesigner))
router.HandleFunc("/admin/designers/add", admin.CheckAuthCookie(admin.AddDesigner))
router.HandleFunc("/admin/designers/edit", admin.CheckAuthCookie(admin.EditDesigner))
router.HandleFunc("/admin/files", admin.CheckAuthCookie(admin.FilesIndex))
router.HandleFunc("/admin/files/delete", admin.CheckAuthCookie(admin.DeleteFile))
router.HandleFunc("/admin/files/add", admin.CheckAuthCookie(admin.AddFile))
router.HandleFunc("/admin/files/edit", admin.CheckAuthCookie(admin.EditFile))
router.PathPrefix("/admin/static/").Handler(http.FileServer(http.FS(adminStatic)))
}
if configuration.ServeWebsite {
api.SetupApiRouter(router)
router.HandleFunc("/download", http2.DownloadFont)
router.HandleFunc("/v3/", getAngularStatic)
router.PathPrefix("/static/").Handler(http.FileServer(http.FS(static)))
router.PathPrefix("/").HandlerFunc(getWebApp)
}
http.Handle("/", router)
log.Println("Serving at localhost:8090...")
err = http.ListenAndServe(":8090", nil)
if err != nil {
panic(err)
}
}
}