-
Notifications
You must be signed in to change notification settings - Fork 13
/
worker.go
executable file
·92 lines (80 loc) · 2.06 KB
/
worker.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
package main
import (
"net"
"github.com/go-playground/pure"
mw "github.com/go-playground/pure/_examples/middleware/logging-recovery"
// "github.com/go-playground/pure/middleware"
"log"
"net/http"
"os"
"strconv"
"github.com/tectiv3/standardfile/db"
)
func worker() {
db.Init(cfg.DB)
log.Println("Started StandardFile Server", Version)
log.Println("Loaded config:", loadedConfig)
if cfg.Debug {
log.Println("Debug on")
}
r := pure.New()
if cfg.UseCORS {
r.Use(mw.LoggingAndRecovery(true), cors)
r.RegisterAutomaticOPTIONS(cors)
} else {
r.Use(mw.LoggingAndRecovery(true))
}
r.Get("/", Dashboard)
r.Post("/api/items/sync", SyncItems)
r.Post("/api/items/backup", BackupItems)
// r.DELETE("/api/items", DeleteItems)
if !cfg.NoReg {
r.Post("/api/auth", Registration)
}
r.Patch("/api/auth", ChangePassword)
r.Post("/api/auth/update", UpdateUser)
r.Post("/api/auth/change_pw", ChangePassword)
r.Post("/api/auth/sign_in", Login)
r.Post("/api/auth/sign_in.json", Login)
r.Get("/api/auth/params", GetParams)
defer removeSock()
go listen(r)
<-run
log.Println("Server stopped")
os.Exit(0)
}
func listen(r *pure.Mux) {
if len(cfg.Socket) != 0 {
os.Remove(cfg.Socket)
unixListener, err := net.Listen("unix", cfg.Socket)
if err != nil {
panic(err)
}
server := http.Server{
Handler: r.Serve(),
}
log.Println("Listening on socket " + cfg.Socket)
server.Serve(unixListener)
} else {
log.Println("Listening on port " + strconv.Itoa(cfg.Port))
err := http.ListenAndServe(":"+strconv.Itoa(cfg.Port), r.Serve())
if err != nil {
log.Println(err)
}
}
}
func cors(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "authorization,content-type")
}
next(w, r)
}
}
func removeSock() {
if len(cfg.Socket) != 0 {
os.Remove(cfg.Socket)
}
}