-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (61 loc) · 1.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"net/http"
"github.com/csg33k/golug/services"
"github.com/csg33k/golug/config"
"github.com/csg33k/golug/www"
log "github.com/sirupsen/logrus"
httpSwagger "github.com/swaggo/http-swagger"
"github.com/go-chi/chi"
"github.com/go-chi/chi/v5/middleware"
)
// @title Golug Demo Code
// @version 1.0
// @BasePath /
//
//go:generate rm -fr dbmodels
//go:generate sqlc generate
//go:generate swag init --parseDependency -o ./api -g main.go
var (
appConfig *config.ServerConfig
)
func main() {
wwwSrv := services.InitializeServices(appConfig.Server.DatabaseUri)
defer wwwSrv.(*services.MagicService).CleaUp()
ctx := context.Background()
context.WithValue(ctx, "db", wwwSrv)
r := chi.NewRouter()
// A good base middleware stack
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
//User Route
r.Route("/api/v1/users", func(r chi.Router) {
r.Get("/list", www.ListUsers)
r.Get("/{id}", www.GetUser)
r.Delete("/{id}", www.DeleteUser)
r.Post("/", www.CreateUser)
r.Put("/{id}", www.UpdateUser)
})
r.Route("/api/v1/distro", func(r chi.Router) {
r.Get("/list", www.ListDistros)
r.Get("/count", www.ListDistroCount)
})
r.Get("/", www.Redirect("/swaggerui/"))
//Static Mount generated files
fs := http.FileServer(http.Dir("api"))
r.Handle("/swagger/*", http.StripPrefix("/swagger/", fs))
//Loads Swagger UI
r.Get("/swaggerui/*", httpSwagger.Handler(
//httpSwagger.URL(fmt.Sprintf("/swagger/swagger.json", appConfig.Server.Address)), //The url pointing to API definition
httpSwagger.URL("/swagger/swagger.json"), //The url pointing to API definition
))
log.Infof("Starting server on: '%s'", appConfig.Server.Address)
http.ListenAndServe(appConfig.Server.Address, r)
}
func init() {
config.NewConfig()
appConfig = config.LoadConfig("golug")
}