-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
42 lines (33 loc) · 1.21 KB
/
server.go
File metadata and controls
42 lines (33 loc) · 1.21 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
package main
import (
"html/template"
"log"
"net/http"
"github.com/nhdewitt/proofpoint-url-decoder/internal/config"
)
// Decoder defines an interface for decoding encoded URLs.
// Implementations should return the decoded URL or an error.
type Decoder interface {
Decode(string) (string, error)
}
// runServer starts an HTTP server that provides a web interface for decoding
// Proofpoint URL Defense links.
//
// It serves templates from the `templates/` directory and uses the provided
// Decoder implementation to process input URLs.
func runServer(d Decoder, c config.Config) {
tmpl := template.Must(template.ParseGlob("templates/*.html"))
mux := http.NewServeMux()
fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))
mux.Handle("/static/", FileServer(fileServer))
mux.Handle("/", MobileRedirectHandler(http.HandlerFunc(FormHandler(tmpl))))
mux.HandleFunc("/api/decode", APIDecodeHandler(d))
mux.Handle("/decode", MobileRedirectHandler(http.HandlerFunc(DecodeFormHandler(d, tmpl))))
mux.HandleFunc("/m", MobileFormHandler(d, tmpl))
server := &http.Server{
Handler: mux,
Addr: ":" + c.Port,
}
log.Printf("Listening on port: %s\n", c.Port)
log.Fatal(server.ListenAndServe())
}