50 lines
833 B
Go
50 lines
833 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// Server base struct
|
|
type Server struct {
|
|
port string
|
|
server http.Server
|
|
}
|
|
|
|
func (s *Server) serve() {
|
|
sm := mux.NewRouter()
|
|
sm.Handle("/", s.handle())
|
|
sm.Handle("/articles", getAllArticles())
|
|
|
|
serv := &http.Server{
|
|
Addr: s.port,
|
|
Handler: sm,
|
|
IdleTimeout: 120 * time.Second,
|
|
ReadTimeout: 1 * time.Second,
|
|
WriteTimeout: 1 * time.Second,
|
|
}
|
|
s.server = *serv
|
|
log.Print("Listening on", s.port)
|
|
|
|
err := serv.ListenAndServe()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func (s *Server) handle() http.Handler {
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte(string("Hello World")))
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
}
|
|
|
|
func (s *Server) shutdown(tc context.Context) {
|
|
s.server.Shutdown(tc)
|
|
}
|