diff options
| author | crupest <crupest@outlook.com> | 2022-11-26 20:47:11 +0800 | 
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-11-26 20:47:11 +0800 | 
| commit | e6c4b5249c7055fe323896041044bd7d515490ca (patch) | |
| tree | 562780591efc00f223e7603247fb48be7679afc3 /docker | |
| parent | 80bb178ab3234fb7b883c6489ff3c5e2631e208e (diff) | |
| download | crupest-e6c4b5249c7055fe323896041044bd7d515490ca.tar.gz crupest-e6c4b5249c7055fe323896041044bd7d515490ca.tar.bz2 crupest-e6c4b5249c7055fe323896041044bd7d515490ca.zip | |
Complete crupest-api.
Diffstat (limited to 'docker')
| -rw-r--r-- | docker/crupest-api/CrupestApi/Program.cs | 181 | ||||
| -rw-r--r-- | docker/crupest-nginx/sites/www/src/main.js | 62 | 
2 files changed, 102 insertions, 141 deletions
| diff --git a/docker/crupest-api/CrupestApi/Program.cs b/docker/crupest-api/CrupestApi/Program.cs index c62bf4d..7fd6675 100644 --- a/docker/crupest-api/CrupestApi/Program.cs +++ b/docker/crupest-api/CrupestApi/Program.cs @@ -9,47 +9,40 @@ using Microsoft.AspNetCore.Builder;  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.Logging;
 -using Microsoft.Extensions.Logging.Console;
  using Microsoft.AspNetCore.Http;
  using CrupestApi.Config;
 -public class TodoItem
 +namespace CrupestApi
  {
 -    public string Status { get; set; } = default!;
 -    public string Title { get; set; } = default!;
 -}
 +    public class TodoItem
 +    {
 +        public string Status { get; set; } = default!;
 +        public string Title { get; set; } = default!;
 +        public bool Closed { get; set; }
 +        public string color { get; set; } = default!;
 +    }
 -internal class Program
 -{
 -    private static void Main(string[] args)
 +    internal class Program
      {
 -        using var httpClient = new HttpClient();
 +        private static void Main(string[] args)
 +        {
 +            using var httpClient = new HttpClient();
 -        var builder = WebApplication.CreateBuilder(args);
 +            var builder = WebApplication.CreateBuilder(args);
 -        string configFilePath = Environment.GetEnvironmentVariable("CRUPEST_API_CONFIG_FILE") ?? "/config.json";
 +            string configFilePath = Environment.GetEnvironmentVariable("CRUPEST_API_CONFIG_FILE") ?? "/config.json";
 -        builder.Configuration.AddJsonFile(configFilePath, optional: false, reloadOnChange: true);
 +            builder.Configuration.AddJsonFile(configFilePath, optional: false, reloadOnChange: true);
 -        string? logFilePath = Environment.GetEnvironmentVariable("CRUPEST_API_LOG_FILE");
 -        if (logFilePath is not null)
 -        {
 -            // TODO: Log to file.
 -            builder.Logging.AddSimpleConsole(logger =>
 -            {
 -                logger.ColorBehavior = LoggerColorBehavior.Disabled;
 -            });
 -        }
 +            var app = builder.Build();
 -        var app = builder.Build();
 -
 -        app.MapGet("/api/todos", async ([FromServices] IConfiguration configuration, [FromServices] ILoggerFactory loggerFactory) =>
 -        {
 -            var logger = loggerFactory.CreateLogger("CrupestApi.Todos");
 -
 -            static string CreateGraphQLQuery(TodoConfiguration todoConfiguration)
 +            app.MapGet("/api/todos", async ([FromServices] IConfiguration configuration, [FromServices] ILoggerFactory loggerFactory) =>
              {
 -                return $$"""
 +                var logger = loggerFactory.CreateLogger("CrupestApi.Todos");
 +
 +                static string CreateGraphQLQuery(TodoConfiguration todoConfiguration)
 +                {
 +                    return $$"""
  {
      user(login: "{{todoConfiguration.Username}}") {
          projectV2(number: {{todoConfiguration.ProjectNumber}}) {
 @@ -76,85 +69,89 @@ internal class Program        }
      }
  """;
 -            }
 +                }
 -            var todoConfiguration = configuration.GetSection("Todos").Get<TodoConfiguration>();
 -            if (todoConfiguration is null)
 -            {
 -                throw new Exception("Fail to get todos configuration.");
 -            }
 +                var todoConfiguration = configuration.GetSection("Todos").Get<TodoConfiguration>();
 +                if (todoConfiguration is null)
 +                {
 +                    throw new Exception("Fail to get todos configuration.");
 +                }
 -            using var requestContent = new StringContent(JsonSerializer.Serialize(new
 -            {
 -                query = CreateGraphQLQuery(todoConfiguration)
 -            }));
 -            requestContent.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Json, Encoding.UTF8.WebName);
 +                using var requestContent = new StringContent(JsonSerializer.Serialize(new
 +                {
 +                    query = CreateGraphQLQuery(todoConfiguration)
 +                }));
 +                requestContent.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Json, Encoding.UTF8.WebName);
 -            using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.github.com/graphql");
 -            request.Content = requestContent;
 -            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", todoConfiguration.Token);
 +                using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.github.com/graphql");
 +                request.Content = requestContent;
 +                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", todoConfiguration.Token);
 +                request.Headers.TryAddWithoutValidation("User-Agent", "crupest");
 -            using var response = await httpClient.SendAsync(request);
 -            var responseBody = await response.Content.ReadAsStringAsync();
 -            logger.LogInformation(response.StatusCode.ToString());
 -            logger.LogInformation(responseBody);
 +                using var response = await httpClient.SendAsync(request);
 +                var responseBody = await response.Content.ReadAsStringAsync();
 +                logger.LogInformation(response.StatusCode.ToString());
 +                logger.LogInformation(responseBody);
 -            if (response.IsSuccessStatusCode)
 -            {
 -                using var responseJson = JsonSerializer.Deserialize<JsonDocument>(responseBody);
 -                if (responseJson is null)
 +                if (response.IsSuccessStatusCode)
                  {
 -                    throw new Exception("Fail to deserialize response body.");
 -                }
 +                    using var responseJson = JsonSerializer.Deserialize<JsonDocument>(responseBody);
 +                    if (responseJson is null)
 +                    {
 +                        throw new Exception("Fail to deserialize response body.");
 +                    }
 -                var nodes = responseJson.RootElement.GetProperty("data").GetProperty("user").GetProperty("projectV2").GetProperty("items").GetProperty("nodes").EnumerateArray();
 +                    var nodes = responseJson.RootElement.GetProperty("data").GetProperty("user").GetProperty("projectV2").GetProperty("items").GetProperty("nodes").EnumerateArray();
 -                var result = new List<TodoItem>();
 +                    var result = new List<TodoItem>();
 -                foreach (var node in nodes)
 -                {
 -                    var content = node.GetProperty("content");
 -                    var title = content.GetProperty("title").GetString();
 -                    if (title is null)
 -                    {
 -                        throw new Exception("Fail to get title.");
 -                    }
 -                    JsonElement closedElement;
 -                    bool closed;
 -                    if (content.TryGetProperty("closed", out closedElement))
 -                    {
 -                        closed = closedElement.GetBoolean();
 -                    }
 -                    else
 +                    foreach (var node in nodes)
                      {
 -                        closed = false;
 +                        var content = node.GetProperty("content");
 +                        var title = content.GetProperty("title").GetString();
 +                        if (title is null)
 +                        {
 +                            throw new Exception("Fail to get title.");
 +                        }
 +                        JsonElement closedElement;
 +                        bool closed;
 +                        if (content.TryGetProperty("closed", out closedElement))
 +                        {
 +                            closed = closedElement.GetBoolean();
 +                        }
 +                        else
 +                        {
 +                            closed = false;
 +                        }
 +
 +                        result.Add(new TodoItem
 +                        {
 +                            Title = title,
 +                            Status = closed ? "Done" : "Todo",
 +                            Closed = closed,
 +                            color = closed ? "green" : "blue"
 +                        });
                      }
 -                    result.Add(new TodoItem
 +                    return Results.Json(result, new JsonSerializerOptions
                      {
 -                        Title = title,
 -                        Status = closed ? "Done" : "Todo"
 -                    });
 +                        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
 +                    }, statusCode: 200);
                  }
 -
 -                return Results.Json(result, new JsonSerializerOptions
 +                else
                  {
 -                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
 -                }, statusCode: 200);
 -            }
 -            else
 -            {
 -                const string message = "Fail to get todos from GitHub.";
 -                logger.LogError(message);
 +                    const string message = "Fail to get todos from GitHub.";
 +                    logger.LogError(message);
 -                return Results.Json(new
 -                {
 -                    message
 -                }, statusCode: StatusCodes.Status503ServiceUnavailable);
 -            }
 -        });
 +                    return Results.Json(new
 +                    {
 +                        message
 +                    }, statusCode: StatusCodes.Status503ServiceUnavailable);
 +                }
 +            });
 -        app.Run();
 +            app.Run();
 +        }
      }
 -}
\ No newline at end of file +}
 diff --git a/docker/crupest-nginx/sites/www/src/main.js b/docker/crupest-nginx/sites/www/src/main.js index bbdecab..111b0bd 100644 --- a/docker/crupest-nginx/sites/www/src/main.js +++ b/docker/crupest-nginx/sites/www/src/main.js @@ -1,4 +1,3 @@ -import { Octokit } from "https://cdn.skypack.dev/octokit";  import "./style.css";  const colorStripContainer = document.getElementById("color-strip-container"); @@ -21,63 +20,28 @@ document.addEventListener("DOMContentLoaded", async () => {    const todoMessage = document.getElementById("todo-message");    const todoContainer = document.getElementById("todo-container"); -  // TODO: we need another way to do this!  -  const octokit = new Octokit({ -    auth: "xxx", -  }); +  const res = await fetch("/api/todos"); +  const body = res.json(); -  try { -    const res = await octokit.graphql( -      ` -    { -      user(login: "crupest") { -        projectV2(number: 2) { -          items(last: 100) { -            nodes { -              __typename -              fieldValueByName(name: "Status") -              content { -                __typename -                ... on Issue { -                  title -                  closed -                } -                ... on PullRequest { -                  title -                  closed -                } -                ... on DraftIssue { -                  title -                } -              } -            } -          } -        } -      } -    } -    ` +  if (res.status !== 200) { +    todoMessage.style.color = "red"; +    todoMessage.textContent = +      "Failed to fetch TODOs. (Maybe due to rate limit. Please try later.)"; +    console.log( +      `Failed to get GitHub project info. Status: ${res.status}. Body: ${body}`      ); - -    const items = res.user.projectV2.items.nodes.map((node) => node.content); - -    items.forEach((item) => { -      if (item.__typename == "DraftIssue") { -        item.closed = false; -      } -      const { title, closed } = item; +  } else { +    body.forEach((item) => { +      const { status, title, color } = item;        const li = document.createElement("li");        const span = document.createElement("span"); -      span.textContent = closed ? "Done:" : "Todo:"; -      span.style.color = closed ? "green" : "blue"; +      span.textContent = status; +      span.style.color = color;        li.appendChild(span);        li.append(title);        todoContainer.appendChild(li);      });      todoMessage.parentElement.removeChild(todoMessage); -  } catch (e) { -    todoMessage.style.color = "red"; -    todoMessage.textContent = "Failed to fetch TODOs."; -    console.log("Failed to get GitHub project info.", e);    }  }); | 
