75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"git.postblue.info/chris/alaska/models"
|
||
|
"github.com/gin-gonic/contrib/static"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/jinzhu/gorm"
|
||
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||
|
"github.com/prometheus/common/log"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
// database
|
||
|
db, err := gorm.Open("sqlite3", "certs.db")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
db.AutoMigrate(&models.Certificate{})
|
||
|
defer db.Close()
|
||
|
|
||
|
// router
|
||
|
router := gin.Default()
|
||
|
router.Use(static.Serve("/", static.LocalFile("./views", true)))
|
||
|
api := router.Group("/api")
|
||
|
{
|
||
|
api.GET("/", func(c *gin.Context) {
|
||
|
c.JSON(http.StatusOK, gin.H{
|
||
|
"message": "pong",
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
api.GET("/certs", CertHandler)
|
||
|
api.GET("/certs/:name", GetCert)
|
||
|
router.Run(":3000")
|
||
|
}
|
||
|
|
||
|
func getCerts() []models.Certificate {
|
||
|
db, err := gorm.Open("sqlite3", "certs.db")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer db.Close()
|
||
|
res := []models.Certificate{}
|
||
|
if err := db.Limit(100).Find(&res).Error; err != nil {
|
||
|
log.Error(err)
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func searchCert(search string) []models.Certificate {
|
||
|
db, err := gorm.Open("sqlite3", "certs.db")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer db.Close()
|
||
|
res := []models.Certificate{}
|
||
|
if err := db.Where(&models.Certificate{CN: search}).Or(&models.Certificate{Fingerprint: search}).Find(&res).Error; err != nil {
|
||
|
log.Warn(err)
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func CertHandler(c *gin.Context) {
|
||
|
getCerts()
|
||
|
c.Header("Content-Type", "application/json")
|
||
|
c.JSON(http.StatusOK, getCerts())
|
||
|
}
|
||
|
|
||
|
func GetCert(c *gin.Context) {
|
||
|
c.Header("Content-Type", "application/json")
|
||
|
c.JSON(http.StatusOK, searchCert(c.Param("name")))
|
||
|
}
|