Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
111 lines (89 loc) · 2.41 KB
/
Copy pathserver.go
File metadata and controls
111 lines (89 loc) · 2.41 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package flint
import (
"os"
"path/filepath"
"fmt"
"net/http"
"strings"
"github.com/fatih/color"
)
type Server struct {
router *Router
notFound HandlerFunc
}
// Create a new flint server
func NewServer() *Server {
s := &Server{
router: NewRouter(),
}
s.notFound = func(ctx *Context) {
ctx.Default404()
}
s.router.notFound = s.notFound
return s
}
// Universal handler
func (s *Server) Handle(path string, handler HandlerFunc) {
s.router.Handle(http.MethodGet, path, handler)
}
// GET
func (s *Server) Get(path string, handler HandlerFunc) {
s.router.Handle(http.MethodGet, path, handler)
}
// POST
func (s *Server) Post(path string, handler HandlerFunc) {
s.router.Handle(http.MethodPost, path, handler)
}
// PUT
func (s *Server) Put(path string, handler HandlerFunc) {
s.router.Handle(http.MethodPut, path, handler)
}
// DELETE
func (s *Server) Delete(path string, handler HandlerFunc) {
s.router.Handle(http.MethodDelete, path, handler)
}
// static file server
func (s *Server) Static(routePath, dir string) {
// routePath sonunda / yoksa ekle
if !strings.HasSuffix(routePath, "/") {
routePath += "/"
}
s.router.Handle(http.MethodGet, routePath+"*", func(ctx *Context) {
// İstek yolu
reqPath := ctx.Request.URL.Path
// routePath’i kırp
relPath := strings.TrimPrefix(reqPath, routePath)
// Güvenli dosya yolu: dir + relPath
fullPath := filepath.Join(dir, relPath)
// Dosya yoksa 404 dön
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
http.NotFound(ctx.Writer, ctx.Request)
return
}
// ServeFile ile gönder
http.ServeFile(ctx.Writer, ctx.Request, fullPath)
})
}
// NotFound override
func (s *Server) SetNotFound(handler HandlerFunc) {
s.notFound = handler
s.router.notFound = handler
}
// Run Server
func (s *Server) Run(addr ...string) error {
port := ":8080"
if len(addr) > 0 && strings.TrimSpace(addr[0]) != "" {
port = addr[0]
}
// Gösterim için host seçimi
host := "localhost"
if strings.Contains(port, ":") && !strings.HasPrefix(port, ":") {
// Eğer sadece port değil, ip de varsa
host = ""
}
displayAddr := host + port
color.New(color.FgWhite, color.Bold).Println("Flint Server is Starting...")
fmt.Println("---------------------------")
color.Red("Listening on http://%s", displayAddr)
return http.ListenAndServe(port, s.router)
}