-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (74 loc) · 2.3 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
package main
import (
"embed"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/ilyakaznacheev/cleanenv"
"open-go-shorten.eu/config"
"open-go-shorten.eu/handlers"
"open-go-shorten.eu/middleware"
"open-go-shorten.eu/utils"
)
//go:embed all:static
var staticFs embed.FS
var version = "develop"
var appName = "OpenGoShorten"
type Args struct {
ConfigPath string
}
var cfg config.Config
func main() {
args := ProcessArgs(&cfg)
// read configuration from the file and environment variables
if _, err := os.Stat(args.ConfigPath); errors.Is(err, os.ErrNotExist) {
if err := cleanenv.ReadEnv(&cfg); err != nil {
fmt.Println(err)
os.Exit(2)
}
} else {
if err := cleanenv.ReadConfig(args.ConfigPath, &cfg); err != nil {
fmt.Println(err)
os.Exit(2)
}
}
utils.InitRedis(&cfg)
middleware.InitJwt(&cfg)
handlers.InitAuth(&cfg)
router := mux.NewRouter()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
utils.SetResponseHeaders(w)
http.ServeFileFS(w, r, staticFs, "static/index.html")
}).Methods("GET")
router.PathPrefix("/static/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, staticFs, strings.TrimLeft(r.RequestURI, "/"))
}).Methods("GET")
router.HandleFunc("/login", handlers.Login).Methods("POST")
router.Handle("/shorten", middleware.JWTMiddleware(http.HandlerFunc(handlers.ShortenURL))).Methods("POST")
router.Handle("/urls", middleware.JWTMiddleware(http.HandlerFunc(handlers.GetURLs))).Methods("GET")
router.Handle("/{shortURL}", middleware.JWTMiddleware(http.HandlerFunc(handlers.DeleteUrl))).Methods("DELETE")
router.HandleFunc("/{shortURL}", handlers.RedirectURL).Methods("GET")
log.Printf("Running server %s", cfg.Server.Host+":"+strconv.Itoa(cfg.Server.Port))
log.Printf("%s running on version %s", appName, version)
http.ListenAndServe(cfg.Server.Host+":"+strconv.Itoa(cfg.Server.Port), router)
}
func ProcessArgs(cfg interface{}) Args {
var a Args
f := flag.NewFlagSet("OpenGoShorten", 1)
f.StringVar(&a.ConfigPath, "c", "config.yaml", "Path to configuration file")
fu := f.Usage
f.Usage = func() {
fu()
envHelp, _ := cleanenv.GetDescription(cfg, nil)
fmt.Fprintln(f.Output())
fmt.Fprintln(f.Output(), envHelp)
}
f.Parse(os.Args[1:])
return a
}