A starter template to start a new fullstack web application with Gin web framework as a backend and Svelte as a frontend. Prepared as a monorepo.
go mod init
touch server.go
Then add this line:
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"ack": time.Now(),
})
})
r.Run(":8080")
}
Then run:
go mod tidy
Give it a try:
go run server.go
# then open http://localhost:8080 in the browser
mkdir client
cd client
npm init vite
Let Gin serve the client's static files:
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.LoadHTMLGlob("./client/**/*.html")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.Static("/assets", "./client/dist/assets")
r.StaticFile("/vite.svg", "./client/dist/vite.svg")
...
r.GET("/api/random", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"random": time.Now().UnixNano(),
})
})
r.Run(":8080")
}