-
Notifications
You must be signed in to change notification settings - Fork 0
/
ihfs.go
93 lines (75 loc) · 2.38 KB
/
ihfs.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
/*
Copyright © 2021-2022 Infinite Devices GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/infinimesh/http-fs/pkg/io/fs"
"github.com/infinimesh/http-fs/pkg/mw"
"github.com/infinimesh/http-fs/pkg/router"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
log *zap.Logger
addr string
repo string
limit int
)
func init() {
atom := zap.NewAtomicLevel()
encoderCfg := zap.NewProductionEncoderConfig()
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
log = zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(encoderCfg),
zapcore.Lock(os.Stdout),
atom,
))
viper.AutomaticEnv()
viper.SetDefault("LOG_LEVEL", 0)
atom.SetLevel(
zapcore.Level(viper.GetInt("LOG_LEVEL")),
)
viper.SetDefault("ADDR", ":8000")
addr = viper.GetString("addr")
viper.SetDefault("REPO", "repo:8000")
repo = viper.GetString("repo")
viper.SetDefault("UPLOAD_LIMIT", 10485760)
limit = viper.GetInt("UPLOAD_LIMIT")
}
func main() {
defer log.Sync()
log.Info("Starting infinimesh HTTP FileServer")
log.Debug("Debug mode enabled")
viper.SetDefault("STATIC_DIR", "static")
static := viper.GetString("STATIC_DIR")
handler := fs.NewFileSystemHandler(log, static)
handler.SetLimit(limit)
mux := router.NewRouter(handler)
// Uncomment this line and comment the next one to enable the ReadOnlyMiddleware
// mux.Use(mw.ReadOnlyMiddleware)
mux.Use(mw.InfinimeshMiddleware(log.Named("middleware"), repo))
http_handler := handlers.CORS(
handlers.AllowedOrigins([]string{"*"}),
handlers.AllowedHeaders([]string{"Content-Type", "Authorization"}),
handlers.AllowedMethods([]string{"GET", "POST", "DELETE", "OPTIONS", "HEAD"}),
)(mux)
srv := &http.Server{
Handler: http_handler,
Addr: addr,
}
log.Info("Starting HTTP Server", zap.String("addr", addr))
log.Fatal("Failed to serve", zap.Error(srv.ListenAndServe()))
}