Push keys and db config to server package.
This commit is contained in:
parent
33fdec9f28
commit
ae49f30c96
3 changed files with 51 additions and 24 deletions
|
|
@ -4,24 +4,12 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/config"
|
||||
"github.com/AYM1607/ccclip/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
privateKeyPath := os.Getenv("CCCLIP_PRIVATE_KEY")
|
||||
publicKeyPath := os.Getenv("CCCLIP_PUBLIC_KEY")
|
||||
databaseLocation := os.Getenv("CCCLIP_DATABASE_LOCATION")
|
||||
port := os.Getenv("CCCLIP_PORT")
|
||||
|
||||
if publicKeyPath == "" || privateKeyPath == "" {
|
||||
log.Fatalf("database location and public and privae keys must be provided")
|
||||
}
|
||||
|
||||
config.Default.PrivateKeyPath = privateKeyPath
|
||||
config.Default.PublicKeyPath = publicKeyPath
|
||||
config.Default.DatabaseLocation = databaseLocation
|
||||
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
|
|
|||
40
internal/server/keys.go
Normal file
40
internal/server/keys.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"crypto/ecdh"
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"github.com/AYM1607/ccclip/pkg/crypto"
|
||||
)
|
||||
|
||||
const (
|
||||
privateKeyEnv = "CCCLIP_PRIVATE_KEY"
|
||||
publicKeyEnv = "CCCLIP_PUBLIC_KEY"
|
||||
privateKeyPathEnv = "CCCLIP_PRIVATE_KEY_PATH"
|
||||
publicKeyPathEnv = "CCCLIP_PUBLIC_KEY_PATH"
|
||||
)
|
||||
|
||||
func loadKeys() (*ecdh.PrivateKey, *ecdh.PublicKey, error) {
|
||||
// Prioritize explicit keys over files.
|
||||
var pvk *ecdh.PrivateKey
|
||||
var pbk *ecdh.PublicKey
|
||||
|
||||
if b64PrivateKey := os.Getenv(privateKeyEnv); b64PrivateKey != "" {
|
||||
pvk = crypto.PrivateKeyFromB64([]byte(b64PrivateKey))
|
||||
} else if privateKeyPath := os.Getenv(privateKeyPathEnv); privateKeyPath != "" {
|
||||
pvk = crypto.LoadPrivateKeyFromFile(privateKeyPath)
|
||||
} else {
|
||||
return nil, nil, errors.New("no private key was found")
|
||||
}
|
||||
|
||||
if b64PublicKey := os.Getenv(publicKeyEnv); b64PublicKey != "" {
|
||||
pbk = crypto.PublicKeyFromB64([]byte(b64PublicKey))
|
||||
} else if publicKeyPath := os.Getenv(publicKeyPathEnv); publicKeyPath != "" {
|
||||
pbk = crypto.LoadPublicKeyFromFile(publicKeyPath)
|
||||
} else {
|
||||
return nil, nil, errors.New("to public key was found")
|
||||
}
|
||||
|
||||
return pvk, pbk, nil
|
||||
}
|
||||
|
|
@ -3,16 +3,16 @@ package server
|
|||
import (
|
||||
"crypto/ecdh"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/AYM1607/ccclip/internal/config"
|
||||
"github.com/AYM1607/ccclip/internal/db"
|
||||
"github.com/AYM1607/ccclip/pkg/api"
|
||||
"github.com/AYM1607/ccclip/pkg/crypto"
|
||||
)
|
||||
|
||||
func New(addr string) *http.Server {
|
||||
|
|
@ -24,7 +24,10 @@ func New(addr string) *http.Server {
|
|||
}
|
||||
}
|
||||
|
||||
const minPasswordWork = 12
|
||||
const (
|
||||
minPasswordWork = 12
|
||||
dbLocationEnv = "CCCLIP_DATABASE_LOCATION"
|
||||
)
|
||||
|
||||
type controller struct {
|
||||
store db.DB
|
||||
|
|
@ -37,20 +40,16 @@ type controller struct {
|
|||
func newHttpHandler() http.Handler {
|
||||
r := mux.NewRouter()
|
||||
|
||||
pbk, err := crypto.LoadPublicKeyFromFile(config.Default.PublicKeyPath)
|
||||
pvk, pbk, err := loadKeys()
|
||||
if err != nil {
|
||||
panic("could not load server's public key")
|
||||
}
|
||||
pvk, err := crypto.LoadPrivateKeyFromFile(config.Default.PrivateKeyPath)
|
||||
if err != nil {
|
||||
panic("could not load server's private key")
|
||||
panic(fmt.Errorf("could not load keys for the server: %w", err))
|
||||
}
|
||||
|
||||
var store db.DB
|
||||
if config.Default.DatabaseLocation == "" {
|
||||
store = db.NewLocalDB()
|
||||
if dbLocation := os.Getenv(dbLocationEnv); dbLocation != "" {
|
||||
store = db.NewSQLiteDB(dbLocation)
|
||||
} else {
|
||||
store = db.NewSQLiteDB(config.Default.DatabaseLocation)
|
||||
store = db.NewLocalDB()
|
||||
}
|
||||
|
||||
c := &controller{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue