This commit is contained in:
74
main.go
74
main.go
@@ -1,8 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -23,10 +25,61 @@ import (
|
||||
var sha1ver string // sha1 revision used to build the program
|
||||
var buildTime string // when the executable was built
|
||||
|
||||
var keyString string
|
||||
type Replacements map[string]string
|
||||
|
||||
var replacements = make(Replacements)
|
||||
|
||||
//go:embed index.htm
|
||||
var staticContent embed.FS
|
||||
|
||||
// staticFileServer serves files from the provided fs.FS
|
||||
func staticFileServer(content embed.FS) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Get the requested file name
|
||||
fileName := c.Request.URL.Path
|
||||
|
||||
// Root request should load index.htm
|
||||
if len(fileName) == 1 {
|
||||
log.Printf("staticFileServer replacing root request with index.htm")
|
||||
fileName = "index.htm"
|
||||
}
|
||||
|
||||
//log.Printf("staticFileServer attempting to load filename '%s'\n", fileName)
|
||||
|
||||
// Try to open the file from the embedded FS
|
||||
file, err := content.Open(fileName)
|
||||
if err != nil {
|
||||
c.String(http.StatusNotFound, "File not found")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Serve the file contents
|
||||
fileStat, _ := file.Stat()
|
||||
data := make([]byte, fileStat.Size())
|
||||
_, err = file.Read(data)
|
||||
if err != nil {
|
||||
c.String(http.StatusInternalServerError, "Error reading file")
|
||||
return
|
||||
}
|
||||
|
||||
// parse the file and perform text replacements as necessary
|
||||
for key, element := range replacements {
|
||||
log.Printf("Searching for '%s' to replace\n", key)
|
||||
data = bytes.Replace(data, []byte(key), []byte(element), -1)
|
||||
}
|
||||
|
||||
// Set the proper Content-Type header based on file extension
|
||||
c.Header("Content-Type", http.DetectContentType(data))
|
||||
c.Data(http.StatusOK, "", data)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
replacements["{SHA1VER}"] = sha1ver
|
||||
replacements["{BUILDTIME}"] = buildTime
|
||||
|
||||
// Load data from environment file
|
||||
envFilename := utils.GetFilePath(".env")
|
||||
err := godotenv.Load(envFilename)
|
||||
@@ -53,7 +106,7 @@ func main() {
|
||||
models.ConnectDatabase()
|
||||
|
||||
// Set secrets key from .env file
|
||||
keyString = os.Getenv("SECRETS_KEY")
|
||||
keyString := os.Getenv("SECRETS_KEY")
|
||||
|
||||
if keyString != "" {
|
||||
// Key was defined in environment variable, let the models package know our secrets key
|
||||
@@ -83,10 +136,12 @@ func main() {
|
||||
// Recovery middleware recovers from any panics and writes a 500 if there was one.
|
||||
router.Use(gin.Recovery())
|
||||
|
||||
// TODO - think of a better default landing page
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, fmt.Sprintf("SMT Built on %s from sha1 %s\n", buildTime, sha1ver))
|
||||
})
|
||||
/*
|
||||
// TODO - think of a better default landing page
|
||||
router.GET("/", func(c *gin.Context) {
|
||||
c.String(http.StatusOK, fmt.Sprintf("SMT Built on %s from sha1 %s\n", buildTime, sha1ver))
|
||||
})
|
||||
*/
|
||||
|
||||
// Set some options for TLS
|
||||
tlsConfig := &tls.Config{
|
||||
@@ -144,6 +199,13 @@ func main() {
|
||||
TLSConfig: tlsConfig,
|
||||
}
|
||||
|
||||
// Set the default readme page
|
||||
//router.Use(EmbedReact("/", "static_files", staticDir))
|
||||
//router.Use(static.Serve("/", static.LocalFile("./static_files", true)))
|
||||
|
||||
// Serve the embedded HTML file if no other routes match
|
||||
router.NoRoute(staticFileServer(staticContent))
|
||||
|
||||
// Register our routes
|
||||
public := router.Group("/api")
|
||||
public.POST("/login", controllers.Login)
|
||||
|
Reference in New Issue
Block a user