-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
148 lines (118 loc) · 3.79 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"embed"
"fmt"
"io/fs"
"log"
"net/http"
"os"
"strings"
"text/template"
"github.com/flxy0/wakeru/config"
"github.com/flxy0/wakeru/files"
"github.com/flxy0/wakeru/hashgen"
"github.com/flxy0/wakeru/helpers"
)
// Generate a copy of the templates dir in helplers/ for compiling.
//go:generate rm -rf ./helpers/templates
//go:generate cp -r ./templates/ ./helpers/templates
// Type for Template Files
type templateFile struct {
name string
contents string
}
//go:embed templates/style.css
var cssFile embed.FS
func renderIndexPage(w http.ResponseWriter, r *http.Request) {
// With the host being able to disable the hash generation route and functionality, we need to parse the argvs and not render the <a> element in the nav if the generation is disabled.
// Two step process due to go:embed
baseTmplRender := template.Must(template.ParseFS(helpers.TemplateDir, "templates/base.gohtml"))
indexTmplRender, err := template.Must(baseTmplRender.Clone()).ParseFS(helpers.TemplateDir, "templates/index.gohtml")
helpers.LogErr(err)
data := struct {
InstanceName string
AllowGenPage bool
Error string
}{
InstanceName: config.ConfigInstanceName,
AllowGenPage: config.ConfigAllowGeneration,
Error: "",
}
tmplErr := indexTmplRender.Execute(w, data)
helpers.LogErr(tmplErr)
}
func embedFsSub() http.FileSystem {
subFs, err := fs.Sub(cssFile, "templates")
helpers.LogErr(err)
return http.FS(subFs)
}
func genRedirect(w http.ResponseWriter, r *http.Request) {
// In case the Generate is set to false in the config, navigating to /generate manually will redirect back to the main page.
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func handleServeContent(w http.ResponseWriter, r *http.Request) {
// This function takes care of all the content serving
// It checks whether the URL has the first 20 digits of an existing string
// and whether the file exists in the corresponding full hash named directory
urlParts := strings.Split(r.URL.Path, "/")
userHash := urlParts[2]
filename := urlParts[3]
var dirMatch string
for _, v := range helpers.ServeDirs {
if userHash == v[:20] {
dirMatch = v
break
}
}
// If dirMatch doesn't get a value assigned to it, the hash doesn't exist so we need to inform the user
if dirMatch == "" {
log.Println(w, "Hash is wrong or doesn't exist")
return
}
filePath := fmt.Sprintf("uploads/%s/%s", dirMatch, filename)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
fmt.Fprintf(w, "File does not exist")
return
}
http.ServeFile(w, r, filePath)
}
func handleArgs() {
if len(os.Args) > 1 {
switch os.Args[1] {
case "init":
handleInitArg()
}
}
}
func handleInitArg() {
config.GenerateDefaultConfigInCurrentDir()
fmt.Println("config created in current directory!")
fmt.Println("feel free to edit the parameters and start the application by omiting the init")
// Exit application because init is not supposed to run the server right away.
os.Exit(0)
}
func main() {
handleArgs()
config.ParseConfig()
mux := http.NewServeMux()
// index route
go mux.HandleFunc("/", renderIndexPage)
// hash generation related routes
// can be disabled by setting Generate to false in config.toml
if config.ConfigAllowGeneration {
go mux.HandleFunc("/generate", hashgen.Generate)
} else {
go mux.HandleFunc("/generate", genRedirect)
}
// upload form and handling of post errors
go mux.HandleFunc("/upload", files.Upload)
// serve uploads
go mux.HandleFunc("/uploads/", handleServeContent)
// view files corresponding to hash
go mux.HandleFunc("/viewfiles/", files.ViewFiles)
// serve static file(s) if need be
go mux.Handle("/style.css", http.FileServer(embedFsSub()))
fmt.Println("----- server starting -----")
fmt.Println("now running app on http://localhost:5050")
http.ListenAndServe(":5050", mux)
}