This repository has been archived by the owner on Sep 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.go
executable file
·162 lines (143 loc) · 4.03 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
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/alecthomas/kingpin"
"github.com/codeskyblue/gohttp/modules"
"github.com/codeskyblue/gohttp/routers"
"github.com/go-macaron/auth"
"github.com/go-macaron/gzip"
"github.com/rs/cors"
"gopkg.in/macaron.v1"
)
const VERSION = "0.1.3"
type Configure struct {
port int
root string
private bool
httpauth string
cert string
key string
gzip bool
ftp bool
ftpPort int
ftpAuth string
upload bool
zipable bool
}
var gcfg = Configure{}
var m *macaron.Macaron
func init() {
m = macaron.Classic()
m.Use(modules.Public)
if _, err := os.Stat("templates"); err == nil {
m.Use(macaron.Renderer())
} else {
m.Use(modules.Renderer)
}
kingpin.HelpFlag.Short('h')
kingpin.Version(VERSION)
kingpin.Flag("port", "Port to listen").Default("8000").IntVar(&gcfg.port)
kingpin.Flag("root", "File root directory").Default(".").StringVar(&gcfg.root)
kingpin.Flag("private", "Only listen on loopback address").BoolVar(&gcfg.private)
kingpin.Flag("httpauth", "HTTP basic auth (ex: user:pass)").Default("").StringVar(&gcfg.httpauth)
kingpin.Flag("cert", "TLS cert.pem").StringVar(&gcfg.cert)
kingpin.Flag("key", "TLS key.pem").StringVar(&gcfg.key)
kingpin.Flag("gzip", "Enable Gzip support").BoolVar(&gcfg.gzip)
//kingpin.Flag("ftp", "Enable FTP support").BoolVar(&gcfg.ftp)
//kingpin.Flag("ftp-port", "FTP listen port").Default("2121").IntVar(&gcfg.ftpPort)
//kingpin.Flag("ftp-auth", "FTP auth (ex: user:pass)").Default("admin:123456").StringVar(&gcfg.ftpAuth)
kingpin.Flag("upload", "Enable upload support").BoolVar(&gcfg.upload)
kingpin.Flag("zipable", "Enable archieve folder into zip").BoolVar(&gcfg.zipable)
}
func initRouters() {
c := cors.New(cors.Options{})
m.Use(c.HandlerFunc)
staticHandler := routers.NewStaticHandler(routers.IndexOptions{
Root: gcfg.root,
Upload: gcfg.upload,
Zipable: gcfg.zipable,
})
m.Get("/*", staticHandler)
m.Head("/*", staticHandler)
m.Get("/$qrcode", routers.Qrcode)
m.Get("/$plist/*", routers.NewPlistHandler(gcfg.root))
m.Get("/$ipaicon/*", routers.NewIpaIconHandler(gcfg.root))
m.Get("/$ipa/*", routers.IPAHandler)
if gcfg.upload {
m.Post("/*", routers.NewUploadHandler(gcfg.root))
}
if gcfg.zipable {
m.Get("/$zip/*", routers.NewZipDownloadHandler(gcfg.root))
}
ReloadProxy := func(w http.ResponseWriter, r *http.Request) {
log.Println("Debug, Hot reload", r.Host)
resp, err := http.Get("http://localhost:3000" + r.RequestURI)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer resp.Body.Close()
io.Copy(w, resp.Body)
}
// HTTP Basic Authentication
userpass := strings.SplitN(gcfg.httpauth, ":", 2)
if len(userpass) == 2 {
user, pass := userpass[0], userpass[1]
m.Use(auth.Basic(user, pass))
}
if gcfg.gzip {
m.Use(gzip.Gziper())
}
m.Get("/-/:rand(.*).hot-update.:ext(.*)", ReloadProxy)
m.Get("/-/:name(.*).bundle.js", ReloadProxy)
}
type FTPAuth struct {
Username string
Password string
}
func (this *FTPAuth) CheckPasswd(user, pass string) (bool, error) {
ok := (this.Username == user && this.Password == pass)
return ok, nil
}
func main() {
kingpin.Parse()
initRouters()
http.Handle("/", m)
addr := ":" + strconv.Itoa(gcfg.port)
p := strconv.Itoa(gcfg.port)
//mesg := "; please visit http://127.0.0.1:" + p
if gcfg.private {
addr = "localhost" + addr
log.Printf("listens on 127.0.0.1@" + p) // + mesg)
} else {
log.Printf("listens on 0.0.0.0@" + p) // + mesg)
}
/*
if gcfg.ftp {
//log.Println("Enable FTP")
auths := strings.SplitN(gcfg.ftpAuth, ":", 2)
if len(auths) != 2 {
log.Fatal("ftp auth format error")
}
auth := FTPAuth{auths[0], auths[1]}
ftpserv := goftp.NewServer(&goftp.ServerOpts{
Port: gcfg.ftpPort,
Factory: posixfs.NewPosixFSFactory(gcfg.root),
Auth: &auth,
})
go ftpserv.ListenAndServe()
}
*/
var err error
if gcfg.key != "" && gcfg.cert != "" {
err = http.ListenAndServeTLS(addr, gcfg.cert, gcfg.key, nil)
} else {
err = http.ListenAndServe(addr, nil)
}
log.Fatal(err)
}