-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
36 lines (30 loc) · 846 Bytes
/
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
package api
import (
"errors"
"net/http"
"github.com/fabiante/persurl/app"
"github.com/gin-gonic/gin"
)
type Server struct {
resolver app.ResolveServiceInterface
admin app.AdminServiceInterface
user app.UserServiceInterface
}
func NewServer(resolver app.ResolveServiceInterface, admin app.AdminServiceInterface, user app.UserServiceInterface) *Server {
return &Server{resolver: resolver, admin: admin, user: user}
}
func (s *Server) Resolve(ctx *gin.Context) {
domain := ctx.Param("domain")
name := ctx.Param("name")
target, err := s.resolver.Resolve(domain, name)
switch true {
case err == nil:
ctx.Redirect(http.StatusFound, target)
return
case errors.Is(err, app.ErrNotFound):
respondWithError(ctx, http.StatusNotFound, err)
return
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}
}