local-server/cmd/dkl-local-server/main.go

94 lines
2.1 KiB
Go
Raw Normal View History

2018-06-12 10:09:47 +00:00
package main
import (
"flag"
"log"
"net/http"
2023-02-07 20:29:19 +00:00
"os"
2018-06-12 10:09:47 +00:00
"path/filepath"
2019-02-01 07:28:08 +00:00
restful "github.com/emicklei/go-restful"
swaggerui "github.com/mcluseau/go-swagger-ui"
2023-02-07 20:29:19 +00:00
"m.cluseau.fr/go/watchable/streamsse"
2019-02-01 07:28:08 +00:00
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/pkg/cas"
2023-02-07 20:29:19 +00:00
dlshtml "novit.tech/direktil/local-server/html"
2022-04-28 01:33:19 +00:00
"novit.tech/direktil/local-server/pkg/apiutils"
2018-06-12 10:09:47 +00:00
)
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")
2023-02-07 20:29:19 +00:00
autoUnlock = flag.String("auto-unlock", "", "Auto-unlock store (testing only!)")
2018-06-12 10:09:47 +00:00
casStore cas.Store
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
2018-06-12 10:09:47 +00:00
flag.Parse()
if *address == "" && *tlsAddress == "" {
log.Fatal("no listen address given")
}
2023-02-07 20:29:19 +00:00
computeUIHash()
openSecretStore()
{
autoUnlock := *autoUnlock
if autoUnlock == "" {
autoUnlock = os.Getenv("DLS_AUTO_UNLOCK")
}
if autoUnlock != "" {
log.Printf("auto-unlocking the store")
err := unlockSecretStore([]byte(autoUnlock))
2023-02-13 12:03:42 +00:00
if err.Any() {
2023-02-07 20:29:19 +00:00
log.Fatal(err)
}
log.Print("store auto-unlocked, admin token is ", *adminToken)
}
os.Setenv("DLS_AUTO_UNLOCK", "")
}
2018-06-12 10:09:47 +00:00
casStore = cas.NewDir(filepath.Join(*dataDir, "cache"))
go casCleaner()
2019-02-01 07:28:08 +00:00
apiutils.Setup(func() {
2019-04-15 17:56:31 +00:00
registerWS(restful.DefaultContainer)
2019-02-01 07:28:08 +00:00
})
2019-02-04 02:56:43 +00:00
swaggerui.HandleAt("/swagger-ui/")
2018-07-07 01:22:35 +00:00
2023-02-07 20:29:19 +00:00
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)))
2018-06-12 10:09:47 +00:00
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 {}
}