Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
f2f2187872 | |||
112a549162 | |||
1360f93ac3 |
@ -3,10 +3,12 @@ package app
|
|||||||
import (
|
import (
|
||||||
"GoWeb/config"
|
"GoWeb/config"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"embed"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App contains and supplies available configurations and connections
|
// App contains and supplies available configurations and connections
|
||||||
type App struct {
|
type App struct {
|
||||||
Config config.Configuration
|
Config config.Configuration // Configuration file
|
||||||
Db *sql.DB
|
Db *sql.DB // Database connection
|
||||||
|
Res *embed.FS // Resources from the embedded filesystem
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module GoWeb
|
module GoWeb
|
||||||
|
|
||||||
go 1.19
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/lib/pq v1.10.7
|
github.com/lib/pq v1.10.7
|
||||||
|
11
main.go
11
main.go
@ -5,12 +5,16 @@ import (
|
|||||||
"GoWeb/config"
|
"GoWeb/config"
|
||||||
"GoWeb/database"
|
"GoWeb/database"
|
||||||
"GoWeb/routes"
|
"GoWeb/routes"
|
||||||
|
"embed"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates static
|
||||||
|
var res embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Create instance of App
|
// Create instance of App
|
||||||
appLoaded := app.App{}
|
appLoaded := app.App{}
|
||||||
@ -18,6 +22,9 @@ func main() {
|
|||||||
// Load config file to application
|
// Load config file to application
|
||||||
appLoaded.Config = config.LoadConfig()
|
appLoaded.Config = config.LoadConfig()
|
||||||
|
|
||||||
|
// Load templates
|
||||||
|
appLoaded.Res = &res
|
||||||
|
|
||||||
// Create logs directory if it doesn't exist
|
// Create logs directory if it doesn't exist
|
||||||
if _, err := os.Stat("logs"); os.IsNotExist(err) {
|
if _, err := os.Stat("logs"); os.IsNotExist(err) {
|
||||||
err := os.Mkdir("logs", 0755)
|
err := os.Mkdir("logs", 0755)
|
||||||
@ -27,7 +34,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create log file and set output
|
// Create log file and set output
|
||||||
file, _ := os.Create("logs/log-" + time.Now().String() + ".log")
|
file, err := os.OpenFile("logs/"+time.Now().Format("2006-01-02")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||||
log.SetOutput(file)
|
log.SetOutput(file)
|
||||||
|
|
||||||
// Connect to database
|
// Connect to database
|
||||||
@ -39,7 +46,7 @@ func main() {
|
|||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
log.Println("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
|
log.Println("Starting server and listening on " + appLoaded.Config.Listen.Ip + ":" + appLoaded.Config.Listen.Port)
|
||||||
err := http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
|
err = http.ListenAndServe(appLoaded.Config.Listen.Ip+":"+appLoaded.Config.Listen.Port, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
|
@ -7,11 +7,39 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RenderTemplate renders and serves a template from the embedded filesystem optionally with given data
|
||||||
func RenderTemplate(app *app.App, w http.ResponseWriter, contentPath string, data any) {
|
func RenderTemplate(app *app.App, w http.ResponseWriter, contentPath string, data any) {
|
||||||
templatePath := app.Config.Template.BaseName
|
templatePath := app.Config.Template.BaseName
|
||||||
|
|
||||||
t, _ := template.ParseFiles(templatePath, contentPath)
|
templateContent, err := app.Res.ReadFile(templatePath)
|
||||||
err := t.Execute(w, data)
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err := template.New(templatePath).Parse(string(templateContent))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
content, err := app.Res.ReadFile(contentPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t, err = t.Parse(string(content))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
|
Reference in New Issue
Block a user