justlog/api/server.go

57 lines
1.4 KiB
Go
Raw Normal View History

2017-03-08 21:38:01 +01:00
package api
import (
2017-10-07 13:49:09 +02:00
"fmt"
2017-03-08 21:38:01 +01:00
"net/http"
2017-09-12 19:47:30 +02:00
"github.com/labstack/echo"
2018-03-02 20:37:34 +01:00
"github.com/labstack/echo/middleware"
2017-03-08 21:38:01 +01:00
)
2017-09-12 19:47:30 +02:00
// Server api server
2017-03-08 21:38:01 +01:00
type Server struct {
2018-03-06 22:02:20 +01:00
port string
logPath string
channels []string
2017-03-08 21:38:01 +01:00
}
2017-09-12 19:47:30 +02:00
// NewServer create Server
2018-12-01 17:46:24 +01:00
func NewServer(logPath string) Server {
2017-03-08 21:38:01 +01:00
return Server{
2018-12-01 17:46:24 +01:00
logPath: logPath,
2018-03-06 22:02:20 +01:00
channels: []string{},
}
}
// AddChannel to in-memory store to serve joined channels
func (s *Server) AddChannel(channel string) {
s.channels = append(s.channels, channel)
}
2017-09-12 19:47:30 +02:00
// Init api server
2017-03-08 21:38:01 +01:00
func (s *Server) Init() {
e := echo.New()
2017-10-07 13:49:09 +02:00
e.HideBanner = true
2018-03-02 20:48:27 +01:00
DefaultCORSConfig := middleware.CORSConfig{
Skipper: middleware.DefaultSkipper,
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}
e.Use(middleware.CORSWithConfig(DefaultCORSConfig))
2017-03-08 21:38:01 +01:00
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
2017-03-11 11:33:50 +01:00
e.GET("/channel/:channel/user/:username", s.getCurrentUserLogs)
2018-03-02 20:18:12 +01:00
e.GET("/channel", s.getAllChannels)
2017-03-11 11:33:50 +01:00
e.GET("/channel/:channel", s.getCurrentChannelLogs)
e.GET("/channel/:channel/:year/:month/:day", s.getDatedChannelLogs)
2017-03-11 11:33:50 +01:00
e.GET("/channel/:channel/user/:username/:year/:month", s.getDatedUserLogs)
2017-03-08 21:38:01 +01:00
e.GET("/channel/:channel/user/:username/random", s.getRandomQuote)
2017-10-07 13:49:09 +02:00
fmt.Println("starting API on port :8025")
2017-09-13 21:12:11 +02:00
e.Logger.Fatal(e.Start(":8025"))
2017-03-11 21:51:08 +01:00
}