-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
62 lines (51 loc) · 1.32 KB
/
server.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"path/filepath"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func newHTTPListener(ctx context.Context, distDir string) *http.Server {
r := gin.Default()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
r.Use(cors.New(config))
r.Use(static.Serve("/", static.LocalFile(distDir, true)))
api := r.Group("/api/v1")
api.GET("/list/:chat_id", func(c *gin.Context) {
chatID := c.Param("chat_id")
nls, err := storage.List(ctx, chatID)
if err != nil {
log.Printf("List: %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
type respItem struct {
NomadLocation
ProfileURL string `json:"profile_url"`
}
respItems := make([]respItem, 0)
for _, l := range nls {
respItems = append(respItems, respItem{
NomadLocation: l,
ProfileURL: fmt.Sprintf("https://t.me/%s", l.Username),
})
}
response, err := json.Marshal(respItems)
if err != nil {
log.Printf("Marshal %s", err)
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.String(http.StatusOK, string(response))
})
r.NoRoute(func(c *gin.Context) {
c.File(filepath.Join(distDir, "index.html"))
})
return &http.Server{Addr: "0.0.0.0:" + CONFIG.APIPort, Handler: r}
}