-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (99 loc) · 3.17 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/joho/godotenv"
"github.com/mbretter/go-mmcli-svr/api"
"github.com/mbretter/go-mmcli-svr/backend/mmcli"
_ "github.com/mbretter/go-mmcli-svr/docs"
"github.com/mbretter/go-mmcli-svr/middleware"
httpSwagger "github.com/swaggo/http-swagger"
"log/slog"
"net/http"
"os"
)
const (
AppModeTest string = "tst"
AppModeProd string = "prod"
AppModeDev string = "dev"
)
// @title mmcli server
// @version 1.0
// @description a http server in front of mmcli
//
// @license.name bsd
// @host 127.0.0.1:8743
//
// @securityDefinitions.apiKey JWT
// @in header
// @name Authorization
func main() {
_ = godotenv.Load(".env")
_ = godotenv.Overload(".env.local")
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
log.Debug("started")
backend := mmcli.Provide()
commandLine := NewCommandLine(log, backend)
err := commandLine.Parse()
if err != nil {
panic(err)
}
commandLine.Activate()
r := chi.NewRouter()
r.Use(chiMiddleware.RealIP)
r.Use(chiMiddleware.RequestID)
r.Use(chiMiddleware.Logger)
r.Use(chiMiddleware.Recoverer)
r.Use(chiMiddleware.NoCache)
r.Use(middleware.HttpLoggerMiddleware(log, chiMiddleware.RequestIDKey))
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "PUT", "POST", "DELETE", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Origin", "Content-Type", "Authorization", "Accept-Language"},
ExposedHeaders: []string{"Content-Length", "X-Message"},
AllowCredentials: true,
MaxAge: 7200,
Debug: false,
}))
r.Use(middleware.HttpModemMiddleware(log))
r.With(middleware.LogRoute).Route("/", func(r chi.Router) {
handlers := api.Provide(backend)
registerModemRoutes(r, handlers)
registerLocationRoutes(r, handlers)
registerSmsRoutes(r, handlers)
utilsApi := api.ProvideUtilsApi()
r.Get("/ping", utilsApi.Ping)
r.Get("/swagger/*", httpSwagger.Handler(
httpSwagger.URL("/swagger/doc.json"),
))
})
log.Info("Listening", "address", commandLine.Listen)
//goland:noinspection ALL
http.ListenAndServe(commandLine.Listen, r)
}
type modemHandlersInterface interface {
ModemList(w http.ResponseWriter, r *http.Request)
ModemDetail(w http.ResponseWriter, r *http.Request)
}
func registerModemRoutes(r chi.Router, handlers modemHandlersInterface) {
r.Get("/modem/", handlers.ModemList)
r.Get("/modem/{id:[a-zA-Z0-9%/]+}", handlers.ModemDetail)
}
type locationHandlersInterface interface {
LocationGet(w http.ResponseWriter, r *http.Request)
}
func registerLocationRoutes(r chi.Router, handlers locationHandlersInterface) {
r.Get("/location", handlers.LocationGet)
}
type smsHandlersInterface interface {
SmsGet(w http.ResponseWriter, r *http.Request)
SmsSend(w http.ResponseWriter, r *http.Request)
SmsDelete(w http.ResponseWriter, r *http.Request)
}
func registerSmsRoutes(r chi.Router, handlers smsHandlersInterface) {
r.Get("/sms/", handlers.SmsGet)
r.Get("/sms/{id:[a-zA-Z0-9%/]+}", handlers.SmsGet)
r.Post("/sms", handlers.SmsSend)
r.Delete("/sms/{id:[a-zA-Z0-9%/]+}", handlers.SmsDelete)
}