You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
_ "git.samuelpua.com/telboon/webhook-everything/backend/docs"
|
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/common"
|
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/telegrampackage"
|
|
"git.samuelpua.com/telboon/webhook-everything/backend/internal/webhookeverything"
|
|
"github.com/go-chi/chi"
|
|
"github.com/joho/godotenv"
|
|
httpSwagger "github.com/swaggo/http-swagger"
|
|
)
|
|
|
|
// Root Handler - Test
|
|
// @Summary This is test
|
|
// @Description Description
|
|
// @Tags Base
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} string
|
|
// @Failure 404 {object} string
|
|
// @Router / [get]
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("hello world"))
|
|
}
|
|
|
|
// @title Eyes of Twitterverse API
|
|
// @version 1.0
|
|
// @description API for frontend - built on Go-chi
|
|
// @BasePath /
|
|
// @contact.name Samuel Pua
|
|
// @contact.url https://git.samuelpua.com/telboon
|
|
func main() {
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
log.Fatal("Error loading .env file")
|
|
}
|
|
environment := os.Getenv("ENVIRONMENT")
|
|
|
|
db := common.InitDB()
|
|
// CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
|
db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";")
|
|
db.AutoMigrate(&webhookeverything.WebhookRoute{})
|
|
db.AutoMigrate(&telegrampackage.ChatIDMap{})
|
|
|
|
// Setup telegram
|
|
telegramAPIKey := os.Getenv("TELEGRAM_API_KEY")
|
|
telegramEnv := telegrampackage.NewEnv(db, telegramAPIKey, nil)
|
|
|
|
// Get Host URL
|
|
hostURL := os.Getenv("HOST_URL")
|
|
|
|
r := chi.NewRouter()
|
|
|
|
if environment == "dev" {
|
|
r.Mount("/docs", httpSwagger.WrapHandler)
|
|
}
|
|
|
|
r.Get("/", rootHandler)
|
|
r.Get("/health", healthHandler)
|
|
|
|
r.Mount("/webhook", webhookeverything.WebhookEverythingRoutes(db, telegramEnv, hostURL))
|
|
|
|
server_str := ":8000"
|
|
log.Printf("Starting server at %s\n", server_str)
|
|
http.ListenAndServe(server_str, r)
|
|
}
|