mirror of
https://github.com/wahyd4/badger.git
synced 2026-08-09 05:15:58 +10:00
119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/getsentry/sentry-go"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
"github.com/prometheus/client_golang/api"
|
|
"github.com/wahyd4/badger/handlers"
|
|
|
|
sentrygin "github.com/getsentry/sentry-go/gin"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/wahyd4/badger/config"
|
|
"github.com/wahyd4/badger/models"
|
|
)
|
|
|
|
type ColorScheme string
|
|
|
|
type queryParam struct {
|
|
Color ColorScheme
|
|
Query string
|
|
ValueField string // nullable
|
|
Label string // when ValueField is null, then the program will use this field as the label
|
|
}
|
|
|
|
func main() {
|
|
if err := sentry.Init(sentry.ClientOptions{
|
|
Dsn: assertNotNullENV("SENTRY_DSN"),
|
|
}); err != nil {
|
|
log.Panicf("Sentry initialization failed: %v\n", err)
|
|
}
|
|
defer sentry.Flush(2 * time.Second)
|
|
|
|
dbConfig := config.Config{
|
|
Host: getEnvWithDefault("DB_HOST", "home.toozhao.com"),
|
|
Port: getEnvWithDefault("DB_PORT", "0000"),
|
|
DBName: getEnvWithDefault("DB_NAME", "badges_dev"),
|
|
Username: getEnvWithDefault("DB_USERNAME", "someuser"),
|
|
Password: getEnvWithDefault("DB_PASSWORD", "hahah"),
|
|
}
|
|
connStr := dbConfig.GetConnectionString()
|
|
|
|
db, err := gorm.Open("postgres", connStr)
|
|
if err != nil {
|
|
sentry.CaptureException(err)
|
|
panic(fmt.Sprintf("failed to connect database %s: %v", connStr, err))
|
|
}
|
|
defer db.Close()
|
|
|
|
// Migrate the schema
|
|
db.AutoMigrate(&models.Badge{}, &models.Token{}, &models.User{})
|
|
|
|
client, err := api.NewClient(api.Config{
|
|
Address: "https://prometheus-api.home.toozhao.com",
|
|
})
|
|
if err != nil {
|
|
sentry.CaptureException(err)
|
|
logrus.Errorf("Error creating client: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
handler := &handlers.BadgesHandler{DB: db, PrometheusAPIClient: client}
|
|
|
|
userHandler := &handlers.UsersHandler{DB: db}
|
|
|
|
r := gin.Default()
|
|
|
|
r.Use(sentrygin.New(sentrygin.Options{
|
|
Repanic: true,
|
|
}))
|
|
|
|
r.GET("/ping", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{"msg": "Hello!"})
|
|
})
|
|
|
|
r.GET("/svg/:name", handler.GetBadge)
|
|
|
|
r.GET("/badges/:name/:color", handler.GetBadgeWithColorScheme)
|
|
|
|
api := r.Group("/api")
|
|
|
|
api.POST("/auth", userHandler.Authenticate)
|
|
api.POST("/badges", handler.CreateBadge)
|
|
|
|
go func() {
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
if err := http.ListenAndServe(":9090", nil); err != nil {
|
|
sentry.CaptureException(err)
|
|
logrus.Error(err)
|
|
os.Exit(1)
|
|
}
|
|
}()
|
|
|
|
log.Fatal(r.Run())
|
|
}
|
|
|
|
func getEnvWithDefault(key, defaultValue string) string {
|
|
val := os.Getenv(key)
|
|
if val != "" {
|
|
return val
|
|
}
|
|
return defaultValue
|
|
}
|
|
|
|
func assertNotNullENV(key string) string {
|
|
val := os.Getenv(key)
|
|
if val != "" {
|
|
return val
|
|
}
|
|
panic(fmt.Sprintf("env %s must be set", key))
|
|
}
|