Skip to content

Instantly share code, notes, and snippets.

@parserpro
Last active December 29, 2021 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save parserpro/8a464ad2d84de88e96609f9093407674 to your computer and use it in GitHub Desktop.
Save parserpro/8a464ad2d84de88e96609f9093407674 to your computer and use it in GitHub Desktop.
gin example
package main
import (
"net/http"
"regexp"
"strconv"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"gitlab.srv.pv.km/counter/logger"
"gitlab.srv.pv.km/counter/stage-site/config"
"golang.org/x/sync/errgroup"
)
// Site -
type Site struct {
Token string `json:"token"`
}
var (
git_commit string
git_branch string
g errgroup.Group
sites = map[string]Site{"127.0.0.1:8080": {
Token: "test_token",
}}
tokenRegexp = regexp.MustCompile(`sputnik_([a-zA-Z0-9]+)\.txt`)
)
func handleTest(c *gin.Context) {
c.String(200, spew.Sdump(c.Request))
switch {
case c.Request.URL.String() == "/":
host := c.Request.Host
if token, ok := sites[host]; ok {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"name": host,
"token": token.Token,
})
} else {
c.HTML(http.StatusNotFound, "404.tmpl", gin.H{})
}
case tokenRegexp.MatchString(c.Request.URL.String()):
c.String(200, spew.Sdump(c.Request))
}
}
func apiRouter() http.Handler {
r := gin.New()
r.Use(gin.Recovery())
v1 := r.Group("/api/v1")
{
v1.GET("/site", func(c *gin.Context) {
c.JSON(200, sites)
})
v1.GET("/site/:site", func(c *gin.Context) {
siteName := c.Param("site")
if site, ok := sites[siteName]; ok {
c.JSON(http.StatusOK, site)
} else {
c.JSON(http.StatusNotFound, gin.H{"error": "Site not found", "status": "error"})
}
})
v1.POST("/site/:site", func(c *gin.Context) {
var json Site
if err := c.ShouldBindJSON(&json); err != nil {
siteName := c.Param("site")
sites[siteName] = json
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
})
v1.DELETE("/site/:site", func(c *gin.Context) {
siteName := c.Param("site")
if site, ok := sites[siteName]; ok {
delete(sites, siteName)
c.JSON(http.StatusOK, site)
} else {
c.JSON(http.StatusNotFound, gin.H{"error": "Site not found", "status": "error"})
}
})
}
return r
}
func testRouter() http.Handler {
r := gin.New()
r.Use(gin.Recovery())
r.LoadHTMLGlob("templates/*")
r.GET("/", handleTest)
r.GET("/sputnik*/", handleTest)
return r
}
func main() {
log.Infof("branch: %s, commit: %s\n", git_branch, git_commit)
var (
apiPort = config.GetIntOrDefault("API_PORT", 8080)
testPort = config.GetIntOrDefault("API_PORT", 80)
)
apiServer := &http.Server{
Addr: ":" + strconv.Itoa(apiPort),
Handler: apiRouter(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
testServer := &http.Server{
Addr: ":" + strconv.Itoa(testPort),
Handler: testRouter(),
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
g.Go(func() error {
return apiServer.ListenAndServe()
})
g.Go(func() error {
return testServer.ListenAndServe()
})
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment