-
Notifications
You must be signed in to change notification settings - Fork 0
Web API #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Web API #14
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import T4.Data | ||
| import T4.Storage | ||
| import qualified Data.Set as S | ||
| import Control.Monad.IO.Class | ||
| import Control.Lens | ||
| import Data.Aeson | ||
| import Servant | ||
| import Data.OpenApi hiding (Tag, Server, server) | ||
| import Servant.OpenApi | ||
| import Servant.Swagger.UI | ||
| import Network.Wai.Handler.Warp | ||
| import qualified Paths_terminal_time_tracking_tool as Paths | ||
|
|
||
| instance ToSchema Clock where | ||
| declareNamedSchema _ = do | ||
| return $ NamedSchema (Just "Clock") $ mempty | ||
| & type_ ?~ OpenApiObject | ||
| & example ?~ toJSON clock | ||
| where clock = In (SLT (read "2025-11-04 09:15:00")) | ||
| (Just "T4 programming") | ||
| (S.fromList ["backend", "api", "haskell"]) | ||
|
|
||
| t4OpenApi :: OpenApi | ||
| t4OpenApi = toOpenApi (Proxy :: Proxy T4API) | ||
| & info . title .~ "T4: Terminal Time Tracking Tool API" | ||
| & info . description ?~ "Read-only API for T4 time tracking" | ||
|
|
||
| type API = T4API | ||
| :<|> "openapi.json" :> Get '[JSON] OpenApi | ||
| :<|> SwaggerSchemaUI "swagger-ui" "swagger.json" | ||
| :<|> Raw | ||
|
|
||
| type T4API = Get '[JSON] (Maybe Clock) | ||
| :<|> "clocks" :> Get '[JSON] [Clock] | ||
| :<|> "categories" :> Get '[JSON] [Category] | ||
| :<|> "tags" :> Get '[JSON] [Tag] | ||
|
|
||
| server :: FilePath -> Server API | ||
| server staticDir = apiServer | ||
| :<|> pure t4OpenApi | ||
| :<|> swaggerSchemaUIServer t4OpenApi | ||
| :<|> serveDirectoryFileServer staticDir | ||
|
|
||
| apiServer :: Server T4API | ||
| apiServer = getStatus | ||
| :<|> getClocks | ||
| :<|> getCategories | ||
| :<|> getTags | ||
|
|
||
| where getStatus = findMax <$> clocks | ||
| getClocks = S.toList <$> clocks | ||
| getCategories = S.toList . allCategories <$> clocks | ||
| getTags = S.toList . allTags <$> clocks | ||
| clocks = do dir <- liftIO getStorageDirectory | ||
| liftIO $ loadDataFromDir dir | ||
| findMax = fmap fst . S.maxView | ||
|
|
||
| main :: IO () | ||
| main = do | ||
| staticDir <- Paths.getDataFileName "exe-t4-api/static" | ||
| putStrLn "T4 API Server starting..." | ||
| putStrLn " OpenAPI spec at: http://localhost:8080/openapi.json" | ||
| putStrLn " Swagger UI at: http://localhost:8080/swagger-ui" | ||
| putStrLn " API endpoints: http://localhost:8080/*" | ||
| run 8080 $ serve (Proxy :: Proxy API) $ server staticDir | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>t4 Report</title> | ||
| </head> | ||
| <body> | ||
| <p>t4 report placeholder</p> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.