Merge pull request #2 from Nucrea/implement-args-parser
Implement args parser
This commit is contained in:
commit
ae1e69a6f9
32
args_parser/args.go
Normal file
32
args_parser/args.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package args_parser
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/akamensky/argparse"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Args interface {
|
||||||
|
GetConfigPath() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Parse(osArgs []string) (Args, error) {
|
||||||
|
parser := argparse.NewParser("backend", "runs backend")
|
||||||
|
|
||||||
|
s := parser.String("c", "config", &argparse.Options{Required: true, Help: "Path to a config file"})
|
||||||
|
|
||||||
|
err := parser.Parse(osArgs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &args{
|
||||||
|
ConfigPath: *s,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
ConfigPath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) GetConfigPath() string {
|
||||||
|
return a.ConfigPath
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module backend
|
|||||||
go 1.22.5
|
go 1.22.5
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/akamensky/argparse v1.4.0
|
||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/go-playground/validator/v10 v10.22.0
|
github.com/go-playground/validator/v10 v10.22.0
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1
|
github.com/golang-jwt/jwt/v5 v5.2.1
|
||||||
|
|||||||
2
go.sum
2
go.sum
@ -1,3 +1,5 @@
|
|||||||
|
github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc=
|
||||||
|
github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
|
||||||
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
|
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
|
||||||
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
|
|||||||
42
main.go
42
main.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"backend/args_parser"
|
||||||
"backend/config"
|
"backend/config"
|
||||||
"backend/src/handlers"
|
"backend/src/handlers"
|
||||||
"backend/src/middleware"
|
"backend/src/middleware"
|
||||||
@ -8,7 +9,9 @@ import (
|
|||||||
"backend/src/repo"
|
"backend/src/repo"
|
||||||
"backend/src/services"
|
"backend/src/services"
|
||||||
"backend/src/utils"
|
"backend/src/utils"
|
||||||
|
"crypto/rsa"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"database/sql"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -19,31 +22,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conf, err := config.NewFromFile("./config_example/config.yaml")
|
args, err := args_parser.Parse(os.Args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyRawBytes, err := os.ReadFile(conf.GetJwtSigningKey())
|
conf, err := config.NewFromFile(args.GetConfigPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyPem, _ := pem.Decode(keyRawBytes)
|
var key *rsa.PrivateKey
|
||||||
key, err := x509.ParsePKCS1PrivateKey(keyPem.Bytes)
|
{
|
||||||
if err != nil {
|
keyRawBytes, err := os.ReadFile(conf.GetJwtSigningKey())
|
||||||
panic(err)
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyPem, _ := pem.Decode(keyRawBytes)
|
||||||
|
key, err = x509.ParsePKCS1PrivateKey(keyPem.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pgConnStr := conf.GetPostgresUrl()
|
var sqlDb *sql.DB
|
||||||
connConf, err := pgx.ParseConnectionString(pgConnStr)
|
{
|
||||||
if err != nil {
|
pgConnStr := conf.GetPostgresUrl()
|
||||||
panic(err)
|
connConf, err := pgx.ParseConnectionString(pgConnStr)
|
||||||
}
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
sqlDb := stdlib.OpenDB(connConf)
|
sqlDb := stdlib.OpenDB(connConf)
|
||||||
if err := sqlDb.Ping(); err != nil {
|
if err := sqlDb.Ping(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jwtUtil := utils.NewJwtUtil(key)
|
jwtUtil := utils.NewJwtUtil(key)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user