-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (69 loc) · 1.93 KB
/
main.go
File metadata and controls
86 lines (69 loc) · 1.93 KB
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/arunsworld/go-app/handlers"
"github.com/unrolled/secure"
"github.com/NYTimes/gziphandler"
"github.com/gorilla/mux"
"github.com/rs/cors"
)
func main() {
mux := mux.NewRouter()
indexHandler := handlers.IndexHandler()
mux.HandleFunc("/", indexHandler)
mux.HandleFunc("/form", indexHandler)
mux.HandleFunc("/chat", indexHandler)
mux.HandleFunc("/qr", indexHandler)
mux.HandleFunc("/chatws", handlers.ChatWebSocketHandler)
api := mux.PathPrefix("/api").Subrouter()
api.HandleFunc("/choices", handlers.ChoicesHandler).Methods("GET")
api.HandleFunc("/form-submit", handlers.FormHandler).Methods("POST")
api.HandleFunc("/upload", handlers.UploadHandler).Methods("POST")
createUploadDir()
mux.PathPrefix("/uploads/").Handler(http.StripPrefix("/uploads/", http.FileServer(http.Dir("/tmp/uploads"))))
handlers.SetupStatic(mux)
port, ok := os.LookupEnv("PORT")
if !ok {
port = "80"
}
fmt.Printf("Serving on port %s...\n", port)
serve(secureMux(mux), fmt.Sprintf(":%s", port))
}
func createUploadDir() {
if _, err := os.Stat("/tmp/uploads"); os.IsNotExist(err) {
err = os.Mkdir("/tmp/uploads", 0755)
if err != nil {
log.Fatal("Could not create uploads folder:", err)
}
return
}
info, _ := os.Stat("/tmp/uploads")
if !info.IsDir() {
log.Fatal("/tmp/uploads is not a directory...")
}
}
func secureMux(mux *mux.Router) http.Handler {
c := cors.New(cors.Options{})
secureMiddleware := secure.New(secure.Options{
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
})
handler := c.Handler(mux)
handler = secureMiddleware.Handler(handler)
handler = gziphandler.GzipHandler(handler)
return handler
}
func serve(handler http.Handler, address string) {
srv := http.Server{
Addr: address,
Handler: handler,
ReadTimeout: time.Minute * 3,
WriteTimeout: time.Minute * 3,
}
log.Fatal(srv.ListenAndServe())
}