diff options
| author | jackyzha0 <[email protected]> | 2020-05-09 20:15:59 -0700 |
|---|---|---|
| committer | jackyzha0 <[email protected]> | 2020-05-09 20:15:59 -0700 |
| commit | dedabf41a18820527aed9e77b75564e69c9030ce (patch) | |
| tree | 9258832576f9c1bcad6126cf76ef8f7f6a128e82 /backend/api | |
| parent | Merge pull request #3 from jackyzha0/doc-expiry (diff) | |
| download | ctrl-v-dedabf41a18820527aed9e77b75564e69c9030ce.tar.xz ctrl-v-dedabf41a18820527aed9e77b75564e69c9030ce.zip | |
folder refactor
Diffstat (limited to 'backend/api')
| -rw-r--r-- | backend/api/api.go | 57 | ||||
| -rw-r--r-- | backend/api/ip.go | 37 | ||||
| -rw-r--r-- | backend/api/routes.go | 58 |
3 files changed, 152 insertions, 0 deletions
diff --git a/backend/api/api.go b/backend/api/api.go new file mode 100644 index 0000000..c197774 --- /dev/null +++ b/backend/api/api.go @@ -0,0 +1,57 @@ +package api + +import ( + "net/http" + "os" + "os/signal" + "strconv" + "syscall" + "time" + + mux "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" +) + +func cleanup() { + log.Print("Shutting down server...") +} + +// Define router and start server +func Serve(port int) { + // Sigint trapper + c := make(chan os.Signal) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + cleanup() + os.Exit(0) + }() + + // Define Mux Router + r := mux.NewRouter() + r.HandleFunc("/health", healthCheckFunc) + r.HandleFunc("/api", insertFunc).Methods("POST") + r.HandleFunc("/api/{hash}", getHashFunc).Methods("GET") + + http.Handle("/", r) + + // Start HTTP server + server := newServer(":"+strconv.Itoa(port), r) + log.Printf("Starting server on %d", port) + + defer cleanup() + err := server.ListenAndServe() + if err != nil { + log.Fatal(err) + } +} + +// Function to create new HTTP server +func newServer(addr string, router http.Handler) *http.Server { + return &http.Server{ + Addr: addr, + Handler: router, + ReadTimeout: time.Second * 30, + WriteTimeout: time.Second * 30, + } +} diff --git a/backend/api/ip.go b/backend/api/ip.go new file mode 100644 index 0000000..0d135b3 --- /dev/null +++ b/backend/api/ip.go @@ -0,0 +1,37 @@ +package api + +import ( + "net" + "net/http" + "strings" +) + +func getIP(r *http.Request) (s string) { + // Get IP from the X-REAL-IP header + ip := r.Header.Get("X-REAL-IP") + netIP := net.ParseIP(ip) + if netIP != nil { + return ip + } + + // Get IP from X-FORWARDED-FOR header + ips := r.Header.Get("X-FORWARDED-FOR") + splitIps := strings.Split(ips, ",") + for _, ip := range splitIps { + netIP := net.ParseIP(ip) + if netIP != nil { + return ip + } + } + + // Get IP from RemoteAddr + ip, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return + } + netIP = net.ParseIP(ip) + if netIP != nil { + return + } + return +} diff --git a/backend/api/routes.go b/backend/api/routes.go new file mode 100644 index 0000000..760ee35 --- /dev/null +++ b/backend/api/routes.go @@ -0,0 +1,58 @@ +package api + +import ( + "encoding/json" + "fmt" + "net/http" + "time" + + "github.com/gorilla/mux" + "github.com/jackyzha0/ctrl-v/cache" + "github.com/jackyzha0/ctrl-v/db" + + log "github.com/sirupsen/logrus" +) + +func healthCheckFunc(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "status ok") +} + +func insertFunc(w http.ResponseWriter, r *http.Request) { + // get content + _ = r.ParseMultipartForm(0) + expiry := r.FormValue("expiry") + content := r.FormValue("content") + + // get ip + ip := getIP(r) + + log.Infof("got content '%s' and ip '%s'", content, ip) + + // insert content + err := db.New(ip, content, expiry) + if err != nil { + fmt.Fprintf(w, "got err: %s", err.Error()) + } +} + +func getHashFunc(w http.ResponseWriter, r *http.Request) { + hash := mux.Vars(r)["hash"] + paste, err := cache.C.Get(hash) + + // if hash was not found + if err != nil { + w.WriteHeader(http.StatusNotFound) + fmt.Fprintf(w, "got err: %s", err.Error()) + return + } + + // otherwise, return paste content and current time + w.Header().Set("Content-Type", "application/json") + pasteMap := map[string]interface{}{ + "timestamp": time.Now(), + "content": paste.Content, + } + + jsonData, _ := json.Marshal(pasteMap) + fmt.Fprintf(w, "%+v", string(jsonData)) +} |