-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add API key check * fix: make CA certificate optional * fix: add `db` dir to gitignore
- Loading branch information
Showing
3 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,49 @@ | ||
package config | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"log" | ||
"os" | ||
|
||
"github.com/Netflix/go-env" | ||
) | ||
|
||
type Config struct { | ||
GrpcListenAddress string `env:"GRPC_LISTEN_ADDRESS,default=0.0.0.0:8080"` | ||
SQLiteDirPath string `env:"SQLITE_DIR_PATH,default=db"` | ||
PgDatabaseUrl string `env:"DATABASE_URL"` | ||
GrpcListenAddress string `env:"GRPC_LISTEN_ADDRESS,default=0.0.0.0:8080"` | ||
SQLiteDirPath string `env:"SQLITE_DIR_PATH,default=db"` | ||
PgDatabaseUrl string `env:"DATABASE_URL"` | ||
CACertPath *string `env:"CA_CERT_PATH"` | ||
CACert *x509.Certificate | ||
} | ||
|
||
func initializeCACert(certPath string) *x509.Certificate { | ||
certData, err := os.ReadFile(certPath) | ||
if err != nil { | ||
log.Fatal("CA certificate not found") | ||
} | ||
|
||
CACertBlock, _ := pem.Decode(certData) | ||
if CACertBlock == nil { | ||
log.Fatal("CA certificate is invalid") | ||
} | ||
|
||
CACert, err := x509.ParseCertificate(CACertBlock.Bytes) | ||
if err != nil { | ||
log.Fatal("Could not parse CA cert:", err) | ||
} | ||
|
||
return CACert | ||
} | ||
|
||
func NewConfig() (*Config, error) { | ||
var config Config | ||
if _, err := env.UnmarshalFromEnviron(&config); err != nil { | ||
return nil, err | ||
} | ||
if config.CACertPath != nil { | ||
config.CACert = initializeCACert(*config.CACertPath) | ||
} | ||
|
||
return &config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters