forked from furkansenharputlu/f-license
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (53 loc) · 1.9 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
package main
import (
"crypto/tls"
"fmt"
"github.com/furkansenharputlu/f-license/config"
"github.com/furkansenharputlu/f-license/storage"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
"log"
"net/http"
)
const Version = "0.1"
func intro() {
logrus.Info("f-license ", Version)
logrus.Info("Copyright Furkan Şenharputlu 2020")
logrus.Info("https://f-license.com")
}
func main() {
intro()
config.Global.Load("config.json")
storage.Connect()
router := GenerateRouter()
addr := fmt.Sprintf(":%d", config.Global.Port)
certFile := config.Global.ServerOptions.CertFile
keyFile := config.Global.ServerOptions.KeyFile
if config.Global.ServerOptions.EnableTLS {
srv := &http.Server{
Addr: addr,
Handler: router,
TLSConfig: &config.Global.ServerOptions.TLSConfig,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
log.Fatal(srv.ListenAndServeTLS(certFile, keyFile))
} else {
log.Fatal(http.ListenAndServe(addr, router))
}
}
func GenerateRouter() *mux.Router {
r := mux.NewRouter()
// Endpoints called by product owners
adminRouter := r.PathPrefix("/admin").Subrouter()
adminRouter.Use(AuthenticationMiddleware)
adminRouter.HandleFunc("/licenses", GetAllLicenses).Methods(http.MethodGet)
adminRouter.HandleFunc("/licenses", GenerateLicense).Methods(http.MethodPost)
adminRouter.HandleFunc("/licenses/{id}", GetLicense).Methods(http.MethodGet)
adminRouter.HandleFunc("/licenses/{id}/activate", ChangeLicenseActiveness).Methods(http.MethodPut)
adminRouter.HandleFunc("/licenses/{id}/inactivate", ChangeLicenseActiveness).Methods(http.MethodPut)
adminRouter.HandleFunc("/licenses/{id}/delete", DeleteLicense).Methods(http.MethodDelete)
// Endpoints called by product instances having license
r.HandleFunc("/license/verify", VerifyLicense).Methods(http.MethodPost)
r.HandleFunc("/license/ping", Ping).Methods(http.MethodPost)
return r
}