94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
restful "github.com/emicklei/go-restful"
|
|
swaggerui "github.com/mcluseau/go-swagger-ui"
|
|
"m.cluseau.fr/go/watchable/streamsse"
|
|
|
|
"novit.tech/direktil/pkg/cas"
|
|
|
|
dlshtml "novit.tech/direktil/local-server/html"
|
|
"novit.tech/direktil/local-server/pkg/apiutils"
|
|
)
|
|
|
|
const (
|
|
etcDir = "/etc/direktil"
|
|
)
|
|
|
|
var (
|
|
address = flag.String("address", ":7606", "HTTP listen address")
|
|
tlsAddress = flag.String("tls-address", "", "HTTPS listen address")
|
|
certFile = flag.String("tls-cert", etcDir+"/server.crt", "Server TLS certificate")
|
|
keyFile = flag.String("tls-key", etcDir+"/server.key", "Server TLS key")
|
|
|
|
autoUnlock = flag.String("auto-unlock", "", "Auto-unlock store (testing only!)")
|
|
|
|
casStore cas.Store
|
|
)
|
|
|
|
func main() {
|
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
|
|
|
flag.Parse()
|
|
|
|
if *address == "" && *tlsAddress == "" {
|
|
log.Fatal("no listen address given")
|
|
}
|
|
|
|
computeUIHash()
|
|
|
|
openSecretStore()
|
|
|
|
{
|
|
autoUnlock := *autoUnlock
|
|
if autoUnlock == "" {
|
|
autoUnlock = os.Getenv("DLS_AUTO_UNLOCK")
|
|
}
|
|
if autoUnlock != "" {
|
|
log.Printf("auto-unlocking the store")
|
|
err := unlockSecretStore([]byte(autoUnlock))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Print("store auto-unlocked, admin token is ", *adminToken)
|
|
}
|
|
|
|
os.Setenv("DLS_AUTO_UNLOCK", "")
|
|
}
|
|
|
|
casStore = cas.NewDir(filepath.Join(*dataDir, "cache"))
|
|
go casCleaner()
|
|
|
|
apiutils.Setup(func() {
|
|
registerWS(restful.DefaultContainer)
|
|
})
|
|
|
|
swaggerui.HandleAt("/swagger-ui/")
|
|
|
|
staticHandler := http.FileServer(http.FS(dlshtml.FS))
|
|
http.Handle("/favicon.ico", staticHandler)
|
|
http.Handle("/ui/", staticHandler)
|
|
|
|
http.Handle("/public-state", streamsse.StreamHandler(wPublicState))
|
|
http.Handle("/state", requireAdmin(streamsse.StreamHandler(wState)))
|
|
|
|
if *address != "" {
|
|
log.Print("HTTP listening on ", *address)
|
|
go log.Fatal(http.ListenAndServe(*address, nil))
|
|
}
|
|
|
|
if *tlsAddress != "" {
|
|
log.Print("HTTPS listening on ", *tlsAddress)
|
|
go log.Fatal(http.ListenAndServeTLS(*tlsAddress, *certFile, *keyFile, nil))
|
|
}
|
|
|
|
select {}
|
|
}
|