瀏覽代碼

Use environment variables instead of config file

Using a config file is not necessary and may be confusing.
Also add address and phone number of User and Branch structs.
master
Immanuel Onyeka 1 年之前
父節點
當前提交
e9547b9df8
共有 1 個檔案被更改,包括 49 行新增16 行删除
  1. +49
    -16
      skouter.go

+ 49
- 16
skouter.go 查看文件

@@ -1,6 +1,7 @@
package main

import (
"os"
"net/http"
"net/mail"
"log"
@@ -26,16 +27,28 @@ import (
_ "image/jpeg"
)

type User struct {
type Address struct {
Id int `json:"id"`
Email string `json:"email"`
Street string `json:"street"`
City string `json:"city"`
Region string `json:"region"`
Country string `json:"country"`
Zip string `json:"zip"`
}


type User struct {
Id int `json:"id"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
BranchId int `json:"branchId"`
LastName string `json:"lastName"`
Phone string `json:"phone"`
Address Address `json:"address"`
BranchId int `json:"branchId"`
Status string `json:"status"`
Country string `json:"country"`
Title string `json:"title"`
Verified bool `json:"verified"`
Verified bool `json:"verified"`
Role string `json:"role"`
Password string `json:"password,omitempty"`
}
@@ -603,7 +616,7 @@ func fetchMi(db *sql.DB, estimate *Estimate, pos int) []MI {

req.AddCookie(&http.Cookie{
Name: "nmirategps_email",
Value: config["NationalMIEmail"]})
Value: os.Getenv("NationalMIEmail")})

resp, err := http.DefaultClient.Do(req)
var res map[string]interface{}
@@ -647,7 +660,7 @@ func login(w http.ResponseWriter, db *sql.DB, r *http.Request) {
UserClaims{ Id: id, Role: role,
Exp: time.Now().Add(time.Minute * 30).Format(time.UnixDate)})

tokenStr, err := token.SignedString([]byte(config["JWT_SECRET"]))
tokenStr, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
if err != nil {
log.Println("Token could not be signed: ", err, tokenStr)
http.Error(w, "Token generation error.", http.StatusInternalServerError)
@@ -673,7 +686,7 @@ func getToken(w http.ResponseWriter, db *sql.DB, r *http.Request) {
UserClaims{ Id: claims.Id, Role: claims.Role,
Exp: time.Now().Add(time.Minute * 30).Format(time.UnixDate)})

tokenStr, err := token.SignedString([]byte(config["JWT_SECRET"]))
tokenStr, err := token.SignedString([]byte(os.Getenv("JWT_SECRET")))
if err != nil {
log.Println("Token could not be signed: ", err, tokenStr)
http.Error(w, "Token generation error.", http.StatusInternalServerError)
@@ -703,7 +716,7 @@ func getClaims(r *http.Request) (UserClaims, error) {
// Pull token payload into UserClaims
_, err := jwt.ParseWithClaims(tokenStr, claims,
func(token *jwt.Token) (any, error) {
return []byte(config["JWT_SECRET"]), nil
return []byte(os.Getenv("JWT_SECRET")), nil
})

if err != nil {
@@ -1699,8 +1712,8 @@ func showPDF(w http.ResponseWriter, r *http.Request) {
// p := r.URL.Path
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/skouter",
config["DBUser"],
config["DBPass"]))
os.Getenv("DBUser"),
os.Getenv("DBPass")))

// w.Header().Set("Content-Type", "application/json; charset=UTF-8")
err = db.Ping()
@@ -1764,10 +1777,11 @@ func api(w http.ResponseWriter, r *http.Request) {
var args []string

p := r.URL.Path
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/skouter",
config["DBUser"],
config["DBPass"]))
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/%s",
os.Getenv("DBUser"),
os.Getenv("DBPass"),
os.Getenv("DBName"),
))

w.Header().Set("Content-Type", "application/json; charset=UTF-8")

@@ -1888,7 +1902,7 @@ func route(w http.ResponseWriter, r *http.Request) {
page.Render(w)
}

func main() {
func serve() {
files := http.FileServer(http.Dir(""))

http.Handle("/assets/", files)
@@ -1896,3 +1910,22 @@ func main() {
http.HandleFunc("/", route)
log.Fatal(http.ListenAndServe(address, nil))
}

func dev(args []string) {
if len(args) == 0 {
os.Setenv("DBName", "skouter_dev")
os.Setenv("DBUser", "tester")
os.Setenv("DBPass", "test123")
serve()
}
}

func main() {
if len(os.Args) <= 1 {
serve()
}
if os.Args[1] == "dev" {
dev(os.Args[2:])
}
}

Loading…
取消
儲存