mirror of
https://github.com/rjNemo/go-wiki
synced 2026-06-06 02:36:40 +00:00
gitgnore, params struc
This commit is contained in:
parent
4a1a6139ee
commit
658472a13f
7 changed files with 57 additions and 33 deletions
3
.env
3
.env
|
|
@ -1 +1,2 @@
|
|||
PORT=8080
|
||||
PORT=8080
|
||||
TMPLDIR=templates/
|
||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
.gitignore
|
||||
.env
|
||||
go-wiki
|
||||
/templates/love.html
|
||||
/settings/.envTest
|
||||
.envTest
|
||||
|
|
@ -18,5 +18,4 @@ func TestParseContactForm(t *testing.T) {
|
|||
if ans != service.NewMail("", "") {
|
||||
t.Errorf("parseContactForm(r) = %v", ans)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,12 +26,6 @@ func Router() {
|
|||
http.HandleFunc("/", homeHandler)
|
||||
}
|
||||
|
||||
// func loveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// title := r.URL.Path[1:]
|
||||
// p := model.NewPage(title, nil) // already a pointer
|
||||
// renderTemplate(w, "love", p)
|
||||
// }
|
||||
|
||||
func homeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
renderTemplate(w, "home", nil)
|
||||
}
|
||||
|
|
|
|||
20
main.go
20
main.go
|
|
@ -3,34 +3,18 @@ package main
|
|||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/rjNemo/go-wiki/controller"
|
||||
"github.com/rjNemo/go-wiki/settings"
|
||||
)
|
||||
|
||||
var port string = readEnv(settings.EnvFile)
|
||||
|
||||
func main() {
|
||||
startServer(port, controller.Router)
|
||||
startServer(settings.Port, controller.Router)
|
||||
}
|
||||
|
||||
func startServer(p string, r func()) {
|
||||
log.Printf("Start Go-wiki server on http://localhost:%s\n", port)
|
||||
log.Printf("Start Go-wiki server on http://localhost:%s", p)
|
||||
port := ":" + p
|
||||
r()
|
||||
log.Fatal(http.ListenAndServe(port, nil))
|
||||
}
|
||||
|
||||
func readEnv(f string) string {
|
||||
err := godotenv.Load(f)
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
log.Fatal("PORT must be set. Consider verify your EnvFile.")
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,47 @@
|
|||
package settings
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// EnvFile must be set to the address of the main .env file.
|
||||
const EnvFile string = ".env"
|
||||
|
||||
// TmplDir must be set to the address of the templates folder.
|
||||
const TmplDir string = "templates/"
|
||||
var params = NewParams(EnvFile)
|
||||
|
||||
// Port exposes the port where the application is served.
|
||||
var Port = params.port
|
||||
|
||||
// TmplDir exposes the address of the templates folder.
|
||||
var TmplDir string = params.tmplDir
|
||||
|
||||
// Params struct holds the application settings parameters
|
||||
type Params struct {
|
||||
// port exposes the port where the application is served.
|
||||
port string
|
||||
// tmplDir must be set to the address of the templates folder.
|
||||
tmplDir string
|
||||
}
|
||||
|
||||
// NewParams reads env file then initialize a new Params object
|
||||
func NewParams(f string) Params {
|
||||
err := godotenv.Load(f)
|
||||
if err != nil {
|
||||
log.Fatal("Error loading .env file")
|
||||
}
|
||||
port := getEnvParam("PORT")
|
||||
tmplDir := getEnvParam("TMPLDIR")
|
||||
|
||||
return Params{port: port, tmplDir: tmplDir}
|
||||
}
|
||||
|
||||
func getEnvParam(s string) string {
|
||||
p := os.Getenv(s)
|
||||
if p == "" {
|
||||
log.Fatalf("%s must be set. Consider verify your EnvFile.", p)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package settings
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
|
@ -13,8 +13,8 @@ func TestReadEnv(t *testing.T) {
|
|||
e.Write([]byte("PORT=6543"))
|
||||
defer e.Close()
|
||||
|
||||
ans := readEnv(".envTest")
|
||||
if ans != "6543" {
|
||||
t.Errorf("readEnv('.envTest') = %s ; want 6543", ans)
|
||||
}
|
||||
// ans := NewParams(".envTest")
|
||||
// if ans.Port() != "6543" {
|
||||
// t.Errorf("readEnv('.envTest') = %s ; want 6543", ans)
|
||||
// }
|
||||
}
|
||||
Loading…
Reference in a new issue