-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
97 lines (87 loc) · 2.71 KB
/
web.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
package web
import (
"context"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/csrf"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/shaninalex/homefilestorage/pkg/config"
"github.com/shaninalex/homefilestorage/pkg/database"
"github.com/shaninalex/homefilestorage/pkg/filemanager"
"github.com/shaninalex/homefilestorage/web/templates"
)
type WebApp struct {
Context context.Context
Config *config.Config
Database database.Repository
Router *mux.Router
State *templates.State
Store *sessions.CookieStore
FileManager *filemanager.FileManager
}
func InitializeWebApp(conf *config.Config, db database.Repository) (*WebApp, error) {
fm, err := filemanager.InitFileManager(conf.Storage.Path)
if err != nil {
return nil, err
}
webapp := &WebApp{
Context: context.TODO(),
Config: conf,
Database: db,
Router: mux.NewRouter(),
State: &templates.State{LoggedIn: false},
Store: sessions.NewCookieStore([]byte(conf.Web.SecretKey)),
FileManager: fm,
}
err = db.Migrate()
if err != nil {
return nil, err
}
go webapp.createAdmin()
go webapp.createStorageFolder()
webapp.initializeRoutes()
return webapp, nil
}
func (web *WebApp) Run(port int64) {
csrfMiddleware := csrf.Protect(
[]byte(web.Config.CSRF.CsrfString),
csrf.RequestHeader("Authenticity-Token"),
csrf.FieldName("authenticity_token"),
csrf.SameSite(csrf.SameSiteStrictMode),
)
web.Router.Use(csrfMiddleware)
log.Printf("App started on port %d\n", port)
http.ListenAndServe(fmt.Sprintf(":%d", port), web.Router)
}
func (web *WebApp) initializeRoutes() {
web.Router.HandleFunc("/", web.homeHandler).Methods("GET")
web.Router.HandleFunc("/login", web.loginHandler).Methods("POST")
web.Router.HandleFunc("/logout", web.logoutHandler).Methods("GET")
web.Router.HandleFunc("/files/upload", web.filesUploadHandler).Methods("POST")
web.Router.HandleFunc("/files/{id:[0-9]+}/download", web.fileDownloadHandler).Methods("GET")
web.Router.HandleFunc("/files/{id:[0-9]+}/preview", web.filePreviewHandler).Methods("GET")
web.Router.HandleFunc("/files/{id:[0-9]+}/delete", web.fileDeleteHandler).Methods("POST")
}
func (web *WebApp) createAdmin() error {
_, err := web.Database.GetAccountByEmail(web.Config.Admin.Email)
if err == nil {
// Account existss
return nil
}
account := &database.Account{Name: web.Config.Admin.Name, Email: web.Config.Admin.Email}
account.HashPassword(web.Config.Admin.Password)
_, err = web.Database.CreateAccount(account.Email, account.Name, account.PasswordHash)
if err != nil {
return err
}
return nil
}
func (web *WebApp) createStorageFolder() {
err := os.MkdirAll(web.Config.Storage.Path, os.ModePerm)
if err != nil {
panic(err)
}
}