• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dhontecillas / hfw / 15376179796

01 Jun 2025 02:31PM UTC coverage: 9.202%. First build
15376179796

Pull #10

dhontecillas
remove deprecated library dependency
Pull Request #10: Improved config loader

94 of 530 new or added lines in 20 files covered. (17.74%)

842 of 9150 relevant lines covered (9.2%)

0.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/examples/web_example/main.go
1
/**
2

3
Example of a basic web application using the **HFW** framework
4

5
*/
6

7
package main
8

9
import (
10
        "fmt"
11
        "net/http"
12
        "os"
13

14
        "github.com/gin-gonic/gin"
15

16
        "github.com/dhontecillas/hfw/pkg/bundler"
17
        "github.com/dhontecillas/hfw/pkg/config"
18
        "github.com/dhontecillas/hfw/pkg/db"
19
        "github.com/dhontecillas/hfw/pkg/ginfw"
20
        ginfwconfig "github.com/dhontecillas/hfw/pkg/ginfw/config"
21
        "github.com/dhontecillas/hfw/pkg/ginfw/web"
22
        "github.com/dhontecillas/hfw/pkg/ginfw/web/session"
23
        "github.com/dhontecillas/hfw/pkg/ginfw/web/wusers"
24
        "github.com/dhontecillas/hfw/pkg/obs/metrics"
25
)
26

27
const (
28
        // ConfAppPrefix has the value of the prefix to be prepended
29
        // to all HFW config vars, so a project can have its own prefix
30
        // for everything and avoid clashing names with other apps running
31
        // in the same evironment.
32
        ConfAppPrefix string = "WEBEXAMPLE_"
33

34
        // ConfStaticPath holds the directory where we store static assets.
35
        // This is not HFW specific, but some config var that we want for
36
        // our app.
37
        ConfStaticPath string = "WEB_STATICPATH"
38
)
39

40
// WebConfig contains the configuration to find
41
// the file assets for the application: here we
42
// put the app specific configuration.
43
type WebConfig struct {
44
        staticPath string
45
}
46

47
func readWebConfig() *WebConfig {
×
48
        var wc WebConfig
×
49
        wc.staticPath = os.Getenv(ConfStaticPath)
×
50
        return &wc
×
51
}
×
52

53
func main() {
×
NEW
54
        cldr, err := config.InitConfig(ConfAppPrefix)
×
NEW
55
        if err != nil {
×
56
                panic(err.Error())
×
57
        }
58
        router := gin.Default()
×
59

×
60
        // ReadInsightsConfig reads the configuration about where to
×
61
        // send logs and metrics.
×
NEW
62
        insConfig := config.ReadInsightsConfig(cldr)
×
63

×
64
        // From the config, we can create a function to instatiate the
×
65
        // insights object to send metrics and logs, and also a function
×
66
        // to have a clean shutdown of the reporting (that is sending
×
67
        // pending metrics and logs before closing the app)
×
68
        insBuilder, insFlush := config.CreateInsightsBuilder(insConfig,
×
69
                metrics.MetricDefinitionList{})
×
NEW
70
        depsBuilder := config.BuildExternalServices(cldr, insBuilder, insFlush)
×
71
        defer depsBuilder.Shutdown()
×
72

×
NEW
73
        bundlerMigrationsConfLoader, err := cldr.Section([]string{"bundler", "migrations"})
×
NEW
74
        if err != nil {
×
NEW
75
                panic("cannot find bundler configuration")
×
76
        }
NEW
77
        dbConfLoader, err := cldr.Section([]string{"db", "sql", "master"})
×
NEW
78
        if err != nil {
×
NEW
79
                panic("cannot find db configuration")
×
80
        }
NEW
81
        var bundlerMigrationsConf config.BundlerMigrationsConfig
×
NEW
82
        if err := bundlerMigrationsConfLoader.Parse(&bundlerMigrationsConf); err != nil {
×
NEW
83
                panic("cannot load bundler config")
×
84
        }
NEW
85
        var dbConf db.Config
×
NEW
86
        if err := dbConfLoader.Parse(&dbConf); err != nil {
×
NEW
87
                panic("cannot load db config")
×
88
        }
89

90
        // Apply the db migrations that will create the required tables
91
        // to register users.
92
        ins := depsBuilder.ExtServices().Ins
×
NEW
93
        if err := bundler.ApplyMigrationsFromConfig(&bundlerMigrationsConf, &dbConf, ins.L); err != nil {
×
94
                panic(err)
×
95
        }
96

97
        router.Use(ginfw.ExtServicesMiddleware(depsBuilder),
×
98
                ginfw.ObsMiddleware())
×
99

×
100
        // set the web dependecies:
×
NEW
101
        redisConf := config.ReadRedisConfig(cldr)
×
NEW
102
        sessionConf, err := ginfwconfig.ReadSessionConf(ins, cldr, redisConf)
×
103
        if err != nil {
×
104
                panic(err)
×
105
        }
106
        session.Use(router, sessionConf)
×
107

×
108
        // read the specific configuration for aur app, in this case
×
109
        // just the static assets folder.
×
110
        wbcfg := readWebConfig()
×
111
        router.Static("/static", wbcfg.staticPath)
×
112

×
113
        router.GET("/", home)
×
114

×
115
        // configure the routes for user registration
×
116
        actionPaths := wusers.ActionPaths{
×
117
                BasePath:          "/users/",
×
118
                ActivationPath:    "/users/activate/",
×
119
                ResetPasswordPath: "/users/resetpassword",
×
120
        }
×
121
        wusers.Routes(router.Group("/users"), actionPaths)
×
122

×
123
        // to use it in production:
×
124
        // router.HTMLRender = web.NewMultiRenderEngineFromDirs(
×
125
        //        "../../pkg/ginfw/web/wusers/", "./")
×
126
        router.HTMLRender = web.NewHTMLRender(
×
127
                "../../pkg/ginfw/web/wusers/", "./")
×
128
        err = router.Run()
×
129
        if err != nil {
×
130
                fmt.Printf("error running router: %s\n", err.Error())
×
131
        }
×
132
}
133

134
func home(c *gin.Context) {
×
135
        userID := session.GetUserID(c)
×
136
        c.HTML(http.StatusOK, "landing.html",
×
137
                gin.H{
×
138
                        "wuser_registration_url": "/users/register",
×
139
                        "foo":                    "bar",
×
140
                        "userID":                 userID,
×
141
                })
×
142
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc