ginsvelte Svelte Themes

Ginsvelte

A Gin + Svelte starter template for fullstack web app

🚀 Gin + Svelte (TS & Vite)

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.

Prerequisites

  • Machine with MacOS or Linux
  • Go lang installed
  • Docker installed
  • Docker compose installed

Steps to reproduce

Step 1: Initiate the go project

go mod init

Step 2: Initiate the server

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

Step 3: Add Client

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")
}

Top categories

Loading Svelte Themes