Skip to content

Commit

Permalink
fork from go-webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
UtopistMan committed Feb 2, 2022
1 parent 62bebd9 commit 8165d23
Show file tree
Hide file tree
Showing 13 changed files with 290 additions and 0 deletions.
10 changes: 10 additions & 0 deletions assets/css/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body {
background-color: #f2f2f2;
min-height: 100vh;
}

.success-block {
background-color: white;
border-radius: 15px;
padding: 20px;
}
Binary file added assets/img/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

const (
configJSONPath = "config.json"
)
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module widgets

go 1.16

require github.com/gin-gonic/gin v1.7.2
54 changes: 54 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA=
github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
32 changes: 32 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"encoding/json"
"errors"
"io/ioutil"
"os"
)

func (sol *solution) parseConfig() error {
// parse config file
if _, err := os.Stat(configJSONPath); os.IsNotExist(err) {
return errors.New("failed to find config file")
}
jsonBytes, err := ioutil.ReadFile(configJSONPath)
if err != nil {
return err
}
return json.Unmarshal(jsonBytes, &sol.Config)
}

type errorFunc func() error

func checkErrors(errChecks ...errorFunc) error {
for _, errFunc := range errChecks {
err := errFunc()
if err != nil {
return err
}
}
return nil
}
38 changes: 38 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"log"

"github.com/gin-gonic/gin"
)

func main() {
app := newSolution()
err := app.run()
if err != nil {
log.Fatalln(err)
}
}

func newSolution() *solution {
return &solution{}
}

func (sol *solution) run() error {
err := sol.setupRouter()
if err != nil {
return err
}
return nil
}

func (sol *solution) setupRouter() error {
router := router{
Gin: gin.Default(),
}
err := router.setupRoutes()
if err != nil {
return err
}
return router.Gin.Run()
}
51 changes: 51 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"net/http"

"github.com/gin-gonic/gin"
)

type router struct {
Gin *gin.Engine
}

func (r *router) setupRoutes() error {
r.loadTemplates("./templates/*")
r.Gin.Static("/assets", "./assets")

r.Gin.GET("/", func(c *gin.Context) {
r.renderTemplate(
c,
http.StatusOK,
"home.html",
gin.H{},
)
})
r.Gin.GET("/showcase", func(c *gin.Context) {
r.renderTemplate(
c,
http.StatusOK,
"showcase.html",
gin.H{},
)
})
r.Gin.NoRoute(func(c *gin.Context) {
r.renderTemplate(
c,
http.StatusNotFound,
"404.html",
gin.H{},
)
})

return nil
}

func (r *router) loadTemplates(pattern string) {
r.Gin.LoadHTMLGlob(pattern)
}

func (r *router) renderTemplate(c *gin.Context, code int, name string, obj interface{}) {
c.HTML(code, name, obj)
}
7 changes: 7 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type solution struct {
Config appConfig
}

type appConfig struct{}
18 changes: 18 additions & 0 deletions templates/404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
{{block "head" .}} {{end}}
<body>
<br/>
<div class="uk-container uk-width-xlarge wrap uk-padding uk-border-rounded">
<div class="uk-text-center success-block uk-box-shadow-medium">
<img src="/assets/img/ufo.svg" style="max-width: 256px;" />
<h2>404: page not found</h2>
<p>the requested data was not found</p>

<a href="/" class="uk-button uk-button-primary uk-border-rounded">Home</a>
<div>
</div>

{{block "scripts" .}} {{end}}
</body>
</html>
31 changes: 31 additions & 0 deletions templates/_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{define "head"}}
<head>
<meta charset="utf-8" />
<title>Webapp Template</title>
<meta name="description" content="page description" />
<meta name="keywords" content="app,blank,keywords" />

<meta property="og:type" content="website" />
<meta property="og:site_name" content="Webapp Template" />
<meta property="og:title" content="Webapp Template" />
<meta property="og:description" content="page description" />
<meta property="og:url" content="https://example.com" />
<meta property="og:locale" content="en_EN" />
<meta property="og:image" content="img/preview.png" />
<meta property="og:image:width" content="968" />
<meta property="og:image:height" content="504" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<!-- UIkit CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/uikit@3.2.6/dist/css/uikit.min.css" />
<!-- Custom CSS -->
<link rel="stylesheet" type="text/css" href="/assets/css/custom.css?v=1.1" />

<link rel="icon" type="image/png" href="/assets/img/favicon.png" />
</head>
{{end}}

{{define "scripts"}}
<script src="https://cdn.jsdelivr.net/npm/uikit@3.2.6/dist/js/uikit.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/uikit@3.2.6/dist/js/uikit-icons.min.js"></script>
{{end}}
18 changes: 18 additions & 0 deletions templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
{{block "head" .}} {{end}}
<body>
<br/>
<div class="uk-container uk-width-xlarge wrap uk-padding uk-border-rounded">
<div class="uk-text-center success-block uk-box-shadow-medium">
<img src="/assets/img/success.png" />
<h2>Everything is working</h2>
<p>Your first Go web app</p>

<a href="/showcase" class="uk-button uk-button-primary uk-border-rounded">Showcase</a>
<div>
</div>

{{block "scripts" .}} {{end}}
</body>
</html>
21 changes: 21 additions & 0 deletions templates/showcase.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
{{block "head" .}} {{end}}
<body>
<br/>
<div class="uk-container uk-width-xlarge wrap uk-padding uk-border-rounded">
<div class="uk-text-center success-block uk-box-shadow-medium">
<h2>Showcase</h2>

<a href="/" class="uk-button uk-button-primary uk-border-rounded">register</a>
<a href="/" class="uk-button uk-button-primary uk-border-rounded">login</a>

<hr/>

<a href="/" class="uk-button uk-button-primary uk-border-rounded">Home</a>
<div>
</div>

{{block "scripts" .}} {{end}}
</body>
</html>

0 comments on commit 8165d23

Please sign in to comment.