feat: implement legacy config auth storage and core api
This commit is contained in:
@@ -1,14 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"chfs-ng/internal/api"
|
||||
"chfs-ng/internal/storage"
|
||||
"flag"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"chfs-ng/internal/api"
|
||||
"chfs-ng/internal/auth"
|
||||
"chfs-ng/internal/config"
|
||||
"chfs-ng/internal/storage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := &api.Server{Store: storage.Local{Root: "/data"}}
|
||||
log.Println("chfs-ng development server listening on :80")
|
||||
log.Fatal(http.ListenAndServe(":80", s.Handler()))
|
||||
file := flag.String("file", "", "configuration file")
|
||||
pathFlag := flag.String("path", "/data", "shared path")
|
||||
port := flag.Int("port", 80, "listen port")
|
||||
flag.Parse()
|
||||
cfg := config.Default()
|
||||
if *file != "" {
|
||||
var err error
|
||||
cfg, err = config.Load(*file)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
if *pathFlag != "/data" || *file == "" {
|
||||
if *file == "" {
|
||||
cfg.Path = []string{*pathFlag}
|
||||
cfg.Port = *port
|
||||
}
|
||||
}
|
||||
root := cfg.Path[0]
|
||||
if e := os.MkdirAll(root, 0755); e != nil {
|
||||
log.Fatal(e)
|
||||
}
|
||||
mgr, _ := auth.ParseRules(cfg.Rules)
|
||||
srv := &api.Server{Store: storage.Local{Root: root}, Auth: mgr, Sessions: map[string]*auth.Account{}}
|
||||
addr := ":" + itoa(cfg.Port)
|
||||
if cfg.Port == 0 {
|
||||
addr = ":" + itoa(*port)
|
||||
}
|
||||
log.Printf("chfs-ng listening on %s root=%s", addr, filepath.Clean(root))
|
||||
if cfg.SSLCert != "" && cfg.SSLKey != "" {
|
||||
log.Fatal(http.ListenAndServeTLS(addr, cfg.SSLCert, cfg.SSLKey, srv.Handler()))
|
||||
}
|
||||
log.Fatal(http.ListenAndServe(addr, srv.Handler()))
|
||||
}
|
||||
func itoa(n int) string {
|
||||
const d = "0123456789"
|
||||
if n == 0 {
|
||||
return "0"
|
||||
}
|
||||
b := make([]byte, 0, 6)
|
||||
for n > 0 {
|
||||
b = append([]byte{d[n%10]}, b...)
|
||||
n /= 10
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user