-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (40 loc) · 1.16 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"regexp"
"time"
"github.com/gin-gonic/gin"
"github.com/graphiteisaac/go-mcapi/cache"
"github.com/graphiteisaac/go-mcapi/routes"
)
func main() {
port := *flag.Uint("port", 3333, "the port to run the server on")
enableCache := *flag.Bool("cache", false, "enable caching")
cacheExpiry := *flag.Duration("expiry", time.Second*3, "cache expiry time")
redisURI := *flag.String("redis-uri", "", "the full redis URI (eg redis://:pass@localhost:6379/0)")
flag.Parse()
// create a new Gin router
router := gin.Default()
ch, err := cache.New(enableCache, redisURI, cacheExpiry)
if err != nil {
log.Fatal(err)
}
// Register routes
version := "1.1.0"
f, err := os.ReadFile("README.md")
if err != nil {
fmt.Println("could not open readme, defaulting to version 1.0.0")
}
version = regexp.MustCompile("(Version `(\\d\\.\\d\\.\\d))`").FindStringSubmatch(string(f))[2]
routes.ServerAPIV1(router, ch, version)
routes.RegisterDocs(router, version)
// fallback redirect
router.NoRoute(func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, "/docs")
})
router.Run(fmt.Sprintf(":%d", port))
}