feat: add tls server binder

This commit is contained in:
WH64 2025-04-26 15:19:03 +09:00
parent 3f8590d96c
commit 982182d231
5 changed files with 44 additions and 7 deletions

4
.gitignore vendored
View file

@ -5,3 +5,7 @@ public/
config.toml
!config.sample.toml
*.key
*.pem
*.csr

26
app.go
View file

@ -31,11 +31,29 @@ func daemon(n *commando.Node) error {
return fmt.Errorf("unknown environment: %s", cnf.Environment)
}
app := gin.Default()
app.StaticFS("/", gin.Dir("public/", false))
fmt.Printf("Current Environment: %s\n", cnf.Environment)
fmt.Printf("web server binding at: http://%s:%d\n", cnf.Service.Host, cnf.Service.Port)
if err = app.Run(fmt.Sprintf("%s:%d", cnf.Service.Host, cnf.Service.Port)); err != nil {
app := gin.Default()
app.StaticFS("/", gin.Dir(cnf.Service.Index, false))
if !cnf.Service.TLS {
fmt.Printf("web server binding at: http://%s:%d\n", cnf.Service.Host, cnf.Service.Port)
if err = app.Run(fmt.Sprintf("%s:%d", cnf.Service.Host, cnf.Service.Port)); err != nil {
return err
}
return nil
}
key := cnf.Service.TLSOpt.KeyFile
cert := cnf.Service.TLSOpt.CertFile
fmt.Printf("Key File: %s\n", cnf.Service.TLSOpt.KeyFile)
fmt.Printf("Cert File: %s\n", cnf.Service.TLSOpt.CertFile)
fmt.Printf("web server binding TLS at: https://%s:%d\n", cnf.Service.Host, cnf.Service.Port)
if err = app.RunTLS(
fmt.Sprintf("%s:%d", cnf.Service.Host, cnf.Service.Port), cert, key); err != nil {
return err
}

View file

@ -2,5 +2,12 @@
environment = "development"
[service]
index = "public/"
host = "0.0.0.0"
port = 8080
tls = false
[service.tls-opt]
key_file = ""
cert_file = ""

View file

@ -13,8 +13,16 @@ type Config struct {
}
type ServiceConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
Index string `toml:"index"`
Host string `toml:"host"`
Port int `toml:"port"`
TLS bool `toml:"tls"`
TLSOpt TLSOption `toml:"tls-opt"`
}
type TLSOption struct {
KeyFile string `toml:"key-file"`
CertFile string `toml:"cert-file"`
}
var version *string

View file

@ -1,3 +1,3 @@
{
"version": "0.1.0-alpha"
"version": "0.2.0-alpha"
}