-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (128 loc) · 3.89 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
149
150
151
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
* File Name : main.go
* Purpose :
* Creation Date : 08-27-2017
* Last Modified : Mon 21 May 2018 04:19:32 AM UTC
* Created By : Kiyor
_._._._._._._._._._._._._._._._._._._._._.*/
package main
import (
"compress/gzip"
"flag"
"github.com/NYTimes/gziphandler"
// "github.com/aws/aws-sdk-go/aws"
"log"
"net/http"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/signer/v4"
minio "github.com/minio/minio-go"
"github.com/minio/minio-go/pkg/credentials"
)
var (
stop bool
wg = new(sync.WaitGroup)
listen = flag.String("l", ":8080", "listen interface")
rootDir = flag.String("root", ".", "root dir")
gzipTypes = flag.String("gzip-types", "text/html text/plain text/css text/javascript text/xml application/json application/javascript application/x-javascript application/xml application/atom+xml application/rss+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype font/x-woff", "gzip type")
trashPath, crtPath, keyPath string
s3proxy = flag.Bool("s3proxy", false, "use as s3 proxy, require key")
awsProfile = flag.String("profile", "dos", "aws profile store the key")
s3endpoint = flag.String("s3endpoint", "sfo2.digitaloceanspaces.com", "s3 endpoint")
s3region = flag.String("region", "sfo2", "s3 region")
s3bucket = flag.String("s3bucket", "dos", "s3 bucket")
s3client *minio.Client
signer *v4.Signer
)
func init() {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
if *s3proxy {
cuser, err := user.Current()
if err != nil {
cuser, _ = user.LookupId("0")
}
authfile := filepath.Join(cuser.HomeDir, ".aws/credentials")
credential := credentials.NewFileAWSCredentials(authfile, *awsProfile)
s3client, err = minio.NewWithCredentials(*s3endpoint, credential, true, *s3region)
if err != nil {
panic(err)
}
awsCredential := awscredentials.NewSharedCredentials(authfile, *awsProfile)
signer = v4.NewSigner(awsCredential)
signer.DisableHeaderHoisting = true
} else {
trashPath = filepath.Join(*rootDir, "/.Trash")
if _, err := os.Stat(trashPath); err != nil {
os.Mkdir(trashPath, 0744)
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
mux := http.NewServeMux()
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if stop {
return
}
wg.Add(1)
defer wg.Done()
// if req.URL.Path != KFS_CRT && req.URL.Path != KFS_KEY {
w.Header().Add("Connection", "Keep-Alive")
w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate")
if req.Method == "GET" {
f := &fileHandler{Dir(*rootDir)}
f.ServeHTTP(w, req)
}
// }
})
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
mux.Handle("/", handler)
log.Println("Listen:", *listen, "Root:", *rootDir)
// if enableTLS {
// log.Println("Listen TLS:", *listenTLS, "Root:", *rootDir)
// }
go func() {
typ := strings.Split(*gzipTypes, " ")
for _, v := range typ {
typ = append(typ, v+"; charset=utf-8")
}
gzipHandler, err := gziphandler.GzipHandlerWithOpts(gziphandler.MinSize(512), gziphandler.CompressionLevel(gzip.DefaultCompression), gziphandler.ContentTypes(typ))
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
err = http.ListenAndServe(*listen, LogHandler(gzipHandler(mux)))
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
/*
if enableTLS {
err = h2quic.ListenAndServeQUIC(*listen, crtPath, keyPath, LogHandler(gzipHandler(mux)))
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
}
*/
}()
forever:
for {
select {
case s := <-sig:
log.Printf("Signal (%d) received, stopping\n", s)
stop = true
log.Println("stopped")
break forever
}
}
}